set var="&"&set "var="&"&"set var="&"&set "var="&"&"
每一行都失败,因为其中一个&在引号之外。
可以通过从临时文件中读取说版本的参数来解决。
@echo offSETLOCAL DisableDelayedExpansion
SETLOCALfor %%a in (1) do (set "prompt="echo onfor %%b in (1) do rem * #%1#@echo off) > param.txtENDLOCAL
for /F "delims=" %%L in (param.txt) do (set "param1=%%L")SETLOCAL EnableDelayedExpansionset "param1=!param1:*#=!"set "param1=!param1:~0,-2!"echo %%1 is '!param1!'
myscript.bat some -random=43 extra -greeting="hello world" fluff
myscript.bat将能够在调用函数后使用变量:
call :read_params %*
echo %random%echo %greeting%
下面是函数:
:read_paramsif not %1/==/ (if not "%__var%"=="" (if not "%__var:~0,1%"=="-" (endlocalgoto read_params)endlocal & set %__var:~1%=%~1) else (setlocal & set __var=%~1)shiftgoto read_params)exit /B
@echo off && setlocal EnableDelayedExpansion
for %%Z in (%*)do set "_arg_=%%Z" && set/a "_cnt+=1+0" && (call set "_arg_[!_cnt!]=!_arg_!" && for /l %%l in (!_cnt! 1 !_cnt!)do echo/ The argument n:%%l is: !_arg_[%%l]!)
goto :eof
您的代码已准备好在需要的地方使用参数号做一些事情,例如…
@echo off && setlocal EnableDelayedExpansion
for %%Z in (%*)do set "_arg_=%%Z" && set/a "_cnt+=1+0" && call set "_arg_[!_cnt!]=!_arg_!"
fake-command /u !_arg_[1]! /p !_arg_[2]! > test-log.txt
@echo off
setlocal::::::::::set "VALUES_FILE=E:\scripts\values.txt":::::::::::
for /f "usebackq eol=: tokens=* delims=" %%# in ("%VALUES_FILE%") do set "%%#"
echo %key1% %key2% %some_other_key%
endlocal
和值文件是这样的:
:::: use EOL=: in the FOR loop to use it as a comment
key1=value1
key2=value2
:::: do not left spaces arround the =:::: or at the begining of the line
some_other_key=something else
and_one_more=more