How can I pass arguments to a batch file?

后端 未结 18 2092
陌清茗
陌清茗 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:25

    Make a new batch file (example: openclass.bat) and write this line in the file:

    java %~n1
    

    Then place the batch file in, let's say, the system32 folder, go to your Java class file, right click, Properties, Open with..., then find your batch file, select it and that's that...

    It works for me.

    PS: I can't find a way to close the cmd window when I close the Java class. For now...

    0 讨论(0)
  • 2020-11-22 03:26

    Let's keep this simple.

    Here is the .cmd file.

    @echo off
    rem this file is named echo_3params.cmd
    echo %1
    echo %2
    echo %3
    set v1=%1
    set v2=%2
    set v3=%3
    echo v1 equals %v1%
    echo v2 equals %v2%
    echo v3 equals %v3%
    

    Here are 3 calls from the command line.

    C:\Users\joeco>echo_3params 1abc 2 def  3 ghi
    1abc
    2
    def
    v1 equals 1abc
    v2 equals 2
    v3 equals def
    
    C:\Users\joeco>echo_3params 1abc "2 def"  "3 ghi"
    1abc
    "2 def"
    "3 ghi"
    v1 equals 1abc
    v2 equals "2 def"
    v3 equals "3 ghi"
    
    C:\Users\joeco>echo_3params 1abc '2 def'  "3 ghi"
    1abc
    '2
    def'
    v1 equals 1abc
    v2 equals '2
    v3 equals def'
    
    C:\Users\joeco>
    
    0 讨论(0)
  • 2020-11-22 03:28

    If you want to intelligently handle missing parameters you can do something like:

    IF %1.==. GOTO No1
    IF %2.==. GOTO No2
    ... do stuff...
    GOTO End1
    
    :No1
      ECHO No param 1
    GOTO End1
    :No2
      ECHO No param 2
    GOTO End1
    
    :End1
    
    0 讨论(0)
  • 2020-11-22 03:30

    Accessing batch parameters can be simple with %1, %2, ... %9 or also %*,
    but only if the content is simple.

    There is no simple way for complex contents like "&"^&, as it's not possible to access %1 without producing an error.

    set  var=%1
    set "var=%1"
    set  var=%~1
    set "var=%~1"
    

    The lines expand to

    set  var="&"&
    set "var="&"&"
    set  var="&"&
    set "var="&"&"
    

    And each line fails, as one of the & is outside of the quotes.

    It can be solved with reading from a temporary file a remarked version of the parameter.

    @echo off
    SETLOCAL DisableDelayedExpansion
    
    SETLOCAL
    for %%a in (1) do (
        set "prompt="
        echo on
        for %%b in (1) do rem * #%1#
        @echo off
    ) > param.txt
    ENDLOCAL
    
    for /F "delims=" %%L in (param.txt) do (
      set "param1=%%L"
    )
    SETLOCAL EnableDelayedExpansion
    set "param1=!param1:*#=!"
    set "param1=!param1:~0,-2!"
    echo %%1 is '!param1!'
    

    The trick is to enable echo on and expand the %1 after a rem statement (works also with %2 .. %*).
    So even "&"& could be echoed without producing an error, as it is remarked.

    But to be able to redirect the output of the echo on, you need the two for-loops.

    The extra characters * # are used to be safe against contents like /? (would show the help for REM).
    Or a caret ^ at the line end could work as a multiline character, even in after a rem.

    Then reading the rem parameter output from the file, but carefully.
    The FOR /F should work with delayed expansion off, else contents with "!" would be destroyed.
    After removing the extra characters in param1, you got it.

    And to use param1 in a safe way, enable the delayed expansion.

    0 讨论(0)
  • 2020-11-22 03:30

    Yep, and just don't forget to use variables like %%1 when using if and for and the gang.

    If you forget the double %, then you will be substituting in (possibly null) command line arguments and you will receive some pretty confusing error messages.

    0 讨论(0)
  • 2020-11-22 03:30

    There is no need to complicate it. It is simply command %1 %2 parameters, for example,

    @echo off
    
    xcopy %1 %2 /D /E /C /Q /H /R /K /Y /Z
    
    echo copied %1 to %2
    
    pause
    

    The "pause" displays what the batch file has done and waits for you to hit the ANY key. Save that as xx.bat in the Windows folder.

    To use it, type, for example:

    xx c:\f\30\*.* f:\sites\30
    

    This batch file takes care of all the necessary parameters, like copying only files, that are newer, etc. I have used it since before Windows. If you like seeing the names of the files, as they are being copied, leave out the Q parameter.

    0 讨论(0)
提交回复
热议问题