地带盘符 [英] Strip drive letter

查看:101
本文介绍了地带盘符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过在D中使用命令从文件夹:驱动器

By using the command from a folder in D: drive

for /f "delims="  %%d in ('cd') do set pathdrv=%%d

echo %pathdrv%

我得到D:\\某个文件夹。我想编写批处理命令在驱动器的根创建autorun文件。请帮我脱光衣服驱动器字母D:,所以输出是\\某个文件夹,什么额外的变化办脱光\\

I get "d:\some folder".I want to write batch commands to create autorun file in the root of the drive. Please help me to strip the drive letter "d:" so the output is "\some folder" and what extra change do i do to strip "\".

推荐答案

简短的回答:使用子语法来自前两个字符剥离%CD%伪变量:

Short answer: Use the substring syntax to strip the first two characters from the %cd% pseudo-variable:

%cd:~2%

要删除第一个反斜杠太:

To remove the first backslash too:

%cd:~3%

这可靠,即使在控制台窗口设置为光栅字体统一code路径的作品。

This reliably works even with Unicode paths when the console window is set to raster fonts.

再回应,详细介绍了一些更多的选择(其中没有工作不够好):

Longer answer, detailing some more options (none of which work well enough):

有关参数的批处理文件,你可以使用特殊的语法 P1%,它给你给一个批处理文件的第一个参数的路径(见<一href=\"http://stackoverflow.com/questions/245395/underused-features-of-windows-batch-files/534767#534767\">this回答)。

For arguments to the batch file you can use the special syntax %p1, which gives you the path of the first argument given to a batch file (see this answer).

这不工作与环境变量的方式相同,但有两个技巧可以使用:

This doesn't work the same way with environment variables but there are two tricks you can employ:


  1. 使用子程序:

  1. Use a subroutine:

call :foo "%cd%"
...
goto :eof
:foo
set result=%~p1
goto :eof

子程序可以有参数,就像批处理文件。

Subroutines can have arguments, just like batch files.

使用

for %%d in ("%cd%") do set mypath=%%~pd


但是,这两个变种的的工作时

However, both variants don't work when


  • 控制台设置为如龙力控制台或索拉光栅字体,而不是TrueType字体。

  • 当前目录包含的Uni code字

    • 这在当前传统codePAGE没有重新presentation(对于西方文化中,中日韩是不适合一个不错的选择)。请记住,在这种情况下,你只能得到问号代替字符。

    这个问题是控制而环境变量的持有的统一code就好了,你将进入的问题,一旦你尝试的命令行其中的设置<设置/ EM >他们。被执行的命令之前,上面详细描述每一种选择依赖于某种形式的输出。这有一个统一code不是preserved但替换问题?。唯一的例外是这个答案,它保留统一code字符的路径,即使光栅字体的最开始的子变体。

    The problem with this is that whil environment variables can hold Unicode just fine you'll get into problems once you try setting up a command line which sets them. Every option detailed above relies on output of some kind before the commands are executed. This has the problem that Unicode isn't preserved but replaced by ?. The only exception is the substring variant at the very start of this answer which retains Unicode characters in the path even with raster fonts.

    这篇关于地带盘符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆