Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file 拖放批处理文件-展开此代码问题的变量_Batch File - Fatal编程技术网

Batch file 拖放批处理文件-展开此代码问题的变量

Batch file 拖放批处理文件-展开此代码问题的变量,batch-file,Batch File,这是一个很好的答案,它不能解决我的问题 请参见我试图解决此问题的第22行。 @echo off setlocal ENABLEDELAYEDEXPANSION rem *** Take the cmd-line, remove all until the first parameter rem *** Copy cmdcmdline without any modifications, as cmdcmdline has some strange behaviour set "params=!c

这是一个很好的答案,它不能解决我的问题

请参见我试图解决此问题的第22行。

@echo off
setlocal ENABLEDELAYEDEXPANSION
rem *** Take the cmd-line, remove all until the first parameter
rem *** Copy cmdcmdline without any modifications, as cmdcmdline has some strange behaviour
set "params=!cmdcmdline!"
set "params=!params:~0,-1!"
set "params=!params:*" =!"
set count=0
rem Split the parameters on spaces but respect the quotes
for %%G IN (!params!) do (
  set /a count+=1
  set "item_!count!=%%~G"
  rem echo !count! %%~G
  )
rem list the parameters
for /L %%n in (1,1,!count!) DO (
  rem Original line, works fine
  rem echo %%n #!item_%%n!#
  rem This works fine with !item_%%~n!
  echo !item_%%~n!
  rem This doesn't when I try to expand n to the path.
  echo !item_%%~pn!
)
pause
REM ** The exit is important, so the cmd.exe doesn't try to execute commands after ampersands
exit
理想情况下,我想扩展一下
!物品%%n变量到
!物品%%~pn获取路径,但我不知道如何修改该代码来实现这一点

我认为
变量保持路径,然后
n
变量通过它们计数


我希望能够像普通人一样扩展到其他人:

%%~A         &rem expands %%A removing any surrounding quotes (")
%%~dA        &rem expands %%A to a drive letter only
%%~fA        &rem expands %%A to a fully qualified path name
%%~pA        &rem expands %%A to a path only
%%~nA        &rem expands %%A to a file name only
%%~xA        &rem expands %%A to a file extension only
%%~sA        &rem expanded path contains short names only
%%~aA        &rem expands %%A to file attributes of file
%%~tA        &rem expands %%A to date/time of file
%%~zA        &rem expands %%A to size of file
%%~dpA       &rem expands %%A to a drive letter and path only
%%~nxA       &rem expands %%A to a file name and extension only
%%~fsA       &rem expands %%A to a full path name with short names only
%%~dp$dir:A  &rem searches the directories listed in the 'dir' environment variable and expands %%A 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
%%~ftzaA     &rem expands %%A to a DIR like output line

的扩展!物品%%n以另一种方式工作

首先,
%%n
将扩展为一个数字。
然后是
!项目!将被展开

如果要使用参数语法,必须首先将结果移动到参数中

echo !item_%%~n!
FOR /F "delims=" %%I in ("!item_%%n!") DO ECHO path: %%~dpI

的扩展!物品%%n以另一种方式工作

首先,
%%n
将扩展为一个数字。
然后是
!项目!将被展开

如果要使用参数语法,必须首先将结果移动到参数中

echo !item_%%~n!
FOR /F "delims=" %%I in ("!item_%%n!") DO ECHO path: %%~dpI

实际上,您使用了FOR变量,而不是参数。两者都有相同的修饰语语法。显然,如果
call:routine,可能会使用一个参数"!物品%%n
被用来代替/F.@dbenham Nitpicker,但显然你是对的:-)你得到了我的加号:-)当文件名有
时会出现问题在开始处。有没有办法在这个答案中解决这个问题?@Ste这里的问题是
拖放本身。带有分号的文件不会自动被引用,因此分号在(!params!)中的%%G的
中作为分隔符处理。
。您可以通过一个功能齐全的行解析器来处理这一问题,或者通过一点magic替换,实际上您使用的是FOR变量,而不是参数。两者都有相同的修饰语语法。显然,如果
call:routine,可能会使用一个参数"!物品%%n
被用来代替/F.@dbenham Nitpicker,但显然你是对的:-)你得到了我的加号:-)当文件名有
时会出现问题在开始处。有没有办法在这个答案中解决这个问题?@Ste这里的问题是
拖放本身。带有分号的文件不会自动被引用,因此分号在(!params!)中的%%G的
中作为分隔符处理。
。您可以通过一个功能齐全的行解析器或一点替换魔法来处理这个问题