7

Is it possible to update or replace a command line parameter (like %1) inside of a batch file?

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
Veener
  • 4,771
  • 2
  • 29
  • 37
  • Programmatically? You can always edit the batch file in NotePad, though I doubt that's what you're asking. Be a little more specific :) – apparatix Oct 18 '13 at 17:30
  • 3
    No. `%1` specifically refers to the first parameter passed on the command line that started the batch file. Without exiting the batch file and starting it over with a different parameter, this is not possible (and there's no valid reason to want to do so - if you need a different value, assign it to a new variable inside the batch file and then change that new variable). – Ken White Oct 18 '13 at 17:41

2 Answers2

5

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. 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
MC ND
  • 69,615
  • 8
  • 84
  • 126
  • Thanks, I changed it to load the parameter into a temporary variable and can modify the temporary variable. – Veener Oct 18 '13 at 19:02
2

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
ElektroStudios
  • 19,105
  • 33
  • 200
  • 417