How can I pass arguments to a batch file?

后端 未结 18 2386
陌清茗
陌清茗 2020-11-22 02:53

I need to pass an ID and a password to a batch file at the time of running rather than hardcoding them into the file.

Here\'s what the command line looks like:

18条回答
  •  天涯浪人
    2020-11-22 03:08

    Simple solution(even though question is old)

    Test1.bat

    echo off
    echo "Batch started"
    set arg1=%1
    echo "arg1 is %arg1%"
    echo on
    pause
    

    CallTest1.bat

    call "C:\Temp\Test1.bat" pass123
    

    output

    YourLocalPath>call "C:\Temp\test.bat" pass123
    
    YourLocalPath>echo off
    "Batch started"
    "arg1 is pass123"
    
    YourLocalPath>pause
    Press any key to continue . . .
    

    Where YourLocalPath is current directory path.

    To keep things simple store the command param in variable and use variable for comparison.

    Its not just simple to write but its simple to maintain as well so if later some other person or you read your script after long period of time, it will be easy to understand and maintain.

    To write code inline : see other answers.

提交回复
热议问题