Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.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
Windows 更新批处理文件中的命令行参数_Windows_Batch File_Cmd - Fatal编程技术网

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

Windows 更新批处理文件中的命令行参数,windows,batch-file,cmd,Windows,Batch File,Cmd,是否可以在批处理文件中更新或替换命令行参数(如%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 不,你所尝试的是不可能的 可以模拟将原始批处理参数

是否可以在批处理文件中更新或替换命令行参数(如%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

以编程方式?你总是可以在记事本中编辑批处理文件,尽管我怀疑你问的是这个问题。具体一点:)否。
%1
具体指启动批处理文件的命令行上传递的第一个参数。如果不退出批处理文件并使用其他参数重新启动它,这是不可能的(并且没有有效的理由这样做-如果需要其他值,请将其分配给批处理文件中的新变量,然后更改该新变量)。谢谢,我将其更改为将参数加载到临时变量中,并可以修改临时变量。