Batch file 批量编程中%2和%2之间的差异?

Batch file 批量编程中%2和%2之间的差异?,batch-file,Batch File,我正在学习批处理编程,我不能从字面上区分%2和%2。请再举一个例子。%2在第二个参数中替换~2替换第二个参数,但删除任何引号: C:\Temp>type t.cmd @echo off echo %%2 is: %2 echo %%~2 is: %~2 C:\Temp>t.cmd first second third %2 is: second %~2 is: second C:\Temp>t.cmd first "second third" %2 is: "second

我正在学习批处理编程,我不能从字面上区分%2和%2。请再举一个例子。

%2在第二个参数中替换~2替换第二个参数,但删除任何引号:

C:\Temp>type t.cmd
@echo off
echo %%2 is: %2
echo %%~2 is: %~2

C:\Temp>t.cmd first second third
%2 is: second
%~2 is: second

C:\Temp>t.cmd first "second third"
%2 is: "second third"
%~2 is: second third
在命令提示下,键入help for以查找展开变量时的选项,这些变量即使不在for命令中也能正常工作:

%第二个参数中有2个替代项。%~2替换第二个参数,但删除任何引号:

C:\Temp>type t.cmd
@echo off
echo %%2 is: %2
echo %%~2 is: %~2

C:\Temp>t.cmd first second third
%2 is: second
%~2 is: second

C:\Temp>t.cmd first "second third"
%2 is: "second third"
%~2 is: second third
在命令提示下,键入help for以查找展开变量时的选项,这些变量即使不在for命令中也能正常工作:


%2是传递给批处理文件的第二个参数

myfile.bat firstArg secondArg
因为参数通常是文件路径,所以有一些额外的语法来提取部分路径

%~2删除第二个参数上的任何值

%~1         - expands %1 removing any surrounding quotes (")
%~f1        - expands %1 to a fully qualified path name
%~d1        - expands %1 to a drive letter only
%~p1        - expands %1 to a path only
%~n1        - expands %1 to a file name only
%~x1        - expands %1 to a file extension only
%~s1        - expanded path contains short names only
%~a1        - expands %1 to file attributes
%~t1        - expands %1 to date/time of file
%~z1        - expands %1 to size of file
%~$PATH:1   - searches the directories listed in the PATH
               environment variable and expands %1 to the fully
               qualified name of the first one found.  If the
               environment variable name is not defined or the
               file is not found by the search, then this
               modifier expands to the empty string    

%~dp1       - expands %1 to a drive letter and path only
%~nx1       - expands %1 to a file name and extension only
%~dp$PATH:1 - searches the directories listed in the PATH
               environment variable for %1 and expands to the
               drive letter and path of the first one found.
%~ftza1     - expands %1 to a DIR like output line
可能重复:
%2是传递给批处理文件的第二个参数

myfile.bat firstArg secondArg
因为参数通常是文件路径,所以有一些额外的语法来提取部分路径

%~2删除第二个参数上的任何值

%~1         - expands %1 removing any surrounding quotes (")
%~f1        - expands %1 to a fully qualified path name
%~d1        - expands %1 to a drive letter only
%~p1        - expands %1 to a path only
%~n1        - expands %1 to a file name only
%~x1        - expands %1 to a file extension only
%~s1        - expanded path contains short names only
%~a1        - expands %1 to file attributes
%~t1        - expands %1 to date/time of file
%~z1        - expands %1 to size of file
%~$PATH:1   - searches the directories listed in the PATH
               environment variable and expands %1 to the fully
               qualified name of the first one found.  If the
               environment variable name is not defined or the
               file is not found by the search, then this
               modifier expands to the empty string    

%~dp1       - expands %1 to a drive letter and path only
%~nx1       - expands %1 to a file name and extension only
%~dp$PATH:1 - searches the directories listed in the PATH
               environment variable for %1 and expands to the
               drive letter and path of the first one found.
%~ftza1     - expands %1 to a DIR like output line
可能重复: 嗯,这很简单。%2是传递到bat文件的第二个参数。添加~将从变量中删除引号

考虑以下短bat文件test.bat:

@ECHO OFF
ECHO %2
ECHO %~2
Wenn使用test.bat p1 p2调用bat文件,它将输出:

p2

p2

现在试试test.bat p1 p2!这一次的输出将是:

p2

p2

因此,如果没有任何引号,输出将是相同的,如果有%2将保留它们,%2将删除它们。

很简单。%2是传递到bat文件的第二个参数。添加~将从变量中删除引号

考虑以下短bat文件test.bat:

@ECHO OFF
ECHO %2
ECHO %~2
Wenn使用test.bat p1 p2调用bat文件,它将输出:

p2

p2

现在试试test.bat p1 p2!这一次的输出将是:

p2

p2


因此,如果您没有任何引号,输出将是相同的,如果您有一些引号,则%2将保留它们,%2将删除它们。

否,%~f2将是一个扩展文件名,%~2没有字母,只是为了去掉引号。您的答案完全相同,即@Duncan after editingFirst不是。第二个问题与我在回答中提到的问题完全相同。不,%%f2将是一个扩展文件名,%%2中没有字母只是为了去掉引号。你的答案完全相同,是@Duncan after editingFirst不是。第二个问题与我在回答中提到的问题完全相同。+1答案很好,尽管你在引用整个“寻求帮助”对话时有点过火,因为只有第一个点是正确的relevent@Monacraft这不是整个帮助屏幕,只有关于变量替换的部分,我觉得其余部分提供了一些上下文。您应该像您一样引用相关部分。教育,而不是魔法,需要你去做。+1答案很好,虽然你引用了整个求助对话,但有点过火,因为只有第一个点是正确的relevent@Monacraft这不是整个帮助屏幕,只有关于变量替换的部分,我觉得其余部分提供了一些上下文。你应该,就像你做的那样,引用相关部分。教育,而不是魔法,需要你去做。