【发布时间】:2020-01-24 01:02:25
【问题描述】:
这是来自这个很好的答案的some code,它不能解决我的情况。
请参阅我试图解决此问题的 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
理想情况下,我希望将 !item_%%n! 变量扩展为 !item_%%~pn! 以获取路径,但我不知道如何修改该代码来执行此操作。
我认为item 变量保存了路径,然后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
【问题讨论】:
标签: batch-file