![](/img/trans.png)
[英]How to pass string with double quotes as command line parameter to a batch file
[英]Updating a command line parameter in a batch file
是否可以更新或替換批處理文件中的命令行參數(如%1)?
示例代碼:
rem test.cmd
@echo off
echo Before %1
IF "%1" == "123" (
set %%1 = "12345678"
)
echo After %1
期望的結果:
C:/>Test 123
Before 123
After 12345678
實際結果:
C:/>Test 123
Before 123
After 123
不,你正在嘗試的是不可能的。
可以模擬將原始批處理參數傳遞給subrutine,或者使用修改后的參數遞歸調用相同的cmd,這再次獲得%1,%2,......調用中提供的參數。 但這不是你要求的。
rem test.cmd
@echo off
echo Before %1
if "%~1"=="123" (
call :test %1234
) else (
call :test %1
)
goto :EOF
:test
echo After %1
參數變量是保留的,受保護的變量,您不能自己修改其中一個變量的內容。
我建議你將參數存儲在局部變量中,然后就可以進行所需的所有操作:
@echo off
Set "FirstArg=%~1"
Echo: Before %FirstArg%
IF "%FirstArg%" EQU "123" (
Set "FirstArg=12345678"
)
Echo: After %FirstArg%
Pause&Exit
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.