0

I'm facing one issue when passing surrounding quoted parameter with space in the batch script argument. Here is my code and the output, please let me know what is missing.

Here is my script:

:mainFunction
:loop   
    ECHO key %1 and value %2
    IF NOT %1=="" (
        ECHO User has provided parameter.
        IF "%1"=="-installDir" (
            IF "%2"=="" (
                ECHO Invalid value.
                EXIT /B 0
            ) ELSE (
                ECHO Valid value.
                SHIFT
            )
        )
        REM Check other parameters and parse them.
        SHIFT
        GOTO :loop
    )
EXIT /B %ERRORLEVEL%

and here is the output (with key -installDir and value "D:\Test\New Folder"):

C:\Users\Test\Desktop\BatchPro>installer.bat -installDir="D:\Test\New Folder" -addToDesktop="true"
Folder""=="" was unexpected at this time.
C:\Users\Sinha\Desktop\BatchPro>

So please tell me why this error is coming even after providing parameter inside double quotes.

6
  • try with IF NOT "%~1"=="" . Mind that = is a delimiter in the batch files and -installDir and "D:\Test\New Folder" will be taken for two different parameters.
    – npocmaka
    Nov 12, 2018 at 11:49
  • You should use the following syntax, If "%~1"=="-installDir" ( and If "%~2"=="" (. The ~ expands removing any surrounding doublequotes.
    – Compo
    Nov 12, 2018 at 11:52
  • @npocmaka I tried with "%~1"=="", it didn't work.
    – Dams
    Nov 12, 2018 at 11:56
  • @ Gerhard Barnard It did not reach till that line where it checks for %2=="". If i'm getting correct value with %1 and %2, then why this error is coming.
    – Dams
    Nov 12, 2018 at 11:58
  • @Compo If "%~1"=="-installDir" ( and If "%~2"=="" also not working. Still getting same error.
    – Dams
    Nov 12, 2018 at 12:02

1 Answer 1

0

I took what was said in the comments and created an answer. Please give this a try:

:mainFunction
@echo off
:loop   
    ECHO key %1 and value %2
    IF NOT "%~1"=="" (
        ECHO User has provided parameter.
        IF "%~1"=="-installDir" (
            IF "%~2"=="" (
                ECHO Invalid value.
                EXIT /B 0
            ) ELSE (
                ECHO Valid value.
                SHIFT
            )
        )
        REM Check other parameters and parse them.
        SHIFT
        GOTO :loop
    )
EXIT /B %ERRORLEVEL%

You can run for /? from cmd terminal and read under the "variable substitution" section:

%~I         - expands %I removing any surrounding quotes (")
0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.