2

I just want to leave a reminder about the %~ symbol combination, but my running my batch file outputs this:

C:\Users\batch\batch>add_one2The following usage of the path operator in batch-parameter
substitution is invalid: %~n' is passed argument
For valid formats type CALL /? or FOR /?
C:\Users\batch\batch>

Here's the script:

@echo off
REM '%~n' is passed argument
goto :main

:add_one
setlocal
    echo  Running 'add_one'...
endlocal & set /a %~1=%~2+1
goto :eof

:main
setlocal
    set /a x=1
    set /a y=50
    
    echo Created variable X and set it to %x%
    echo.
    call :add_one y %y%
    echo.
    echo The value of X is now %x%
endlocal
goto :eof

I tried ::, REM, and escaping in quotes, but it didn't work either.

4
  • Try to enclose the problematic part in quotes, not apostrphes: REM "%~n" is passed argument Commented Jun 19 at 19:15
  • Unfortunately it doesn't work Commented Jun 19 at 19:38
  • Change it to a number. %~1. Apparently regardless of the line being a REM, the cmd parser still executes phase 1 which is percent expansion. %~n isn't a valid variable in a batch file. Commented Jun 19 at 19:52
  • Please read the DosTips forum topic: ECHO. FAILS to give text or blank line - Instead use ECHO/ There should be never used echo. to output a blank line as that results always in a file system access by cmd.exe searching in the current directory for a file with name echo. See Different echo?... variants with a very good and detailed information what is much better for the output of a blank line than echo. like echo/ or echo( and what are their pros and cons. Commented Jun 20 at 17:23

1 Answer 1

3

Parameter expansion happens before other commands (see How does the Windows Command Interpreter (CMD.EXE) parse scripts? for much more detail), so the interpreter is trying to expand %~n before the REM command tells it that that line is a comment. Because argument-style variables must contain a digit, you will always get an error when you only use a letter.

For better information, use the actual variables in the function header documentation:

@echo off
goto :main

REM ===================================================
REM Arguments: %1 - The name of the variable to store
REM                 the value in
REM            %2 - The value to add
REM ===================================================
:add_one
setlocal
    echo  Running 'add_one'...
endlocal & set /a %~1=%~2+1
goto :eof

:main
setlocal
    set /a x=1
    set /a y=50
    
    echo Created variable X and set it to %x%
    echo.
    call :add_one y %y%
    echo.
    echo The value of X is now %x%
endlocal
goto :eof
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.