简体   繁体   English

更新批处理文件中的命令行参数

[英]Updating a command line parameter in a batch file

Is it possible to update or replace a command line parameter (like %1) inside of a batch file? 是否可以更新或替换批处理文件中的命令行参数(如%1)?

Sample code: 示例代码:

rem test.cmd
@echo off
echo Before %1
IF "%1" == "123" (
    set %%1 = "12345678"
)
echo After %1

Desired Result: 期望的结果:

C:/>Test 123
Before 123
After 12345678

Actual Result: 实际结果:

C:/>Test 123
Before 123
After 123

No. What you are trying is not possible. 不,你正在尝试的是不可能的。

Can be simulated passing original batch parameters to subrutine, or call the same cmd recursively with modified parameters, which again get %1, %2, ... the parameters provided in the call. 可以模拟将原始批处理参数传递给subrutine,或者使用修改后的参数递归调用相同的cmd,这再次获得%1,%2,......调用中提供的参数。 But this is not what you ask. 但这不是你要求的。

rem test.cmd
@echo off
echo Before %1

if "%~1"=="123" (
    call :test %1234
) else (
    call :test %1
)

goto :EOF

:test

echo After %1

Argument variables are reserved, protected variables, you can't modify the content of one of those variables by yourself. 参数变量是保留的,受保护的变量,您不能自己修改其中一个变量的内容。

I suggest you to store the argument in a local variable then you can do all operations you want: 我建议你将参数存储在局部变量中,然后就可以进行所需的所有操作:

@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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM