Is there a way to pass parameters “by name” (and not by order) to a batch .bat file?

前端 未结 9 1722
情话喂你
情话喂你 2021-01-30 20:35

I need to be able to pass parameters to a windows batch file BY NAME (and NOT by order). My purpose here is to give end user the flexibility to pass parameters in any order, and

相关标签:
9条回答
  • 2021-01-30 21:12

    Yeah you could do something like that though I don't think you can use "=" as a token delimiter. You could use say a colon ":", somebatchfile.bat "SOURC:originalFile.txt" "TARGET:newFile.txt". Here is an example of how you might split the tokens:

    @echo off
    
    set foo=%1
    echo input: %foo%
    
    for /f "tokens=1,2 delims=:" %%a in ("%foo%") do set name=%%a & set val=%%b
    
    echo name:  %name%
    echo value: %val%
    

    Running this would produce this:

    C:\>test.bat SOURC:originalFile.txt
    input: SOURC:originalFile.txt
    name:  SOURC
    value: originalFile.txt
    

    [Edit]

    Ok, maybe it was too close to bed time for me last night but looking again this morning, you can do this:

    @echo off
    
    set %1
    set %2
    
    echo source: %SOURCE%
    echo target: %TARGET%
    

    Which would produce this (note that I reversed the source and target on the command line to show they are set and retrieved correctly):

    C:\>test.bat "TARGET=newFile.txt" "SOURCE=originalFile.txt"
    source: originalFile.txt
    target: newFile.txt
    

    Note that %1 and %2 are evaluated before the set so these do get set as environment variables. They must however be quoted on the command line.

    0 讨论(0)
  • 2021-01-30 21:16

    Take this attempt (pure batch):

    set PARAM_0=0
    :parameters_parse
    
    set parameter=%~1
    if "%parameter%"=="" goto parameters_parse_done
    if "%parameter:~0,1%"=="-" (
        set ARG_%parameter:~1%="%~2"
        shift
        shift
        goto parameters_parse
    )
    if "%parameter:~0,1%"=="/" (
        set ARG_%parameter:~1%="%~2"
        shift
        shift
        goto parameters_parse
    )
    set /a PARAM_0=%PARAM_0%+1
    set PARAM_%PARAM_0%="%~1"
    shift
    goto parameters_parse
    
    :parameters_parse_done
    
    rem Insert your script here
    

    and some tests:

    call args.bat /bar="Hello World" Test1 -baz "Test test test" /foo=Test1 Test2
    
    echo foo=%ARG_foo%
    echo bar=%ARG_bar%
    echo baz=%ARG_baz%
    echo count=%PARAM_0%
    echo 1=%PARAM_1%
    echo 2=%PARAM_2%
    

    Outputs:

    foo="Test1"
    bar="Hello World"
    baz="Test test test"
    count=2
    1="Test1"
    2="Test2"
    
    0 讨论(0)
  • 2021-01-30 21:17

    I wanted to see the possibility of reading named parameter supplied to a batch program. For example :

    myBatch.bat arg1  arg2 
    

    Reading parameter can be done as follows :

    %~1 would be arg1
    %~2 would be arg2
    

    Above argument supplied to batch program is easy to read but then each time you execute it, you have to maintain order and remember what arg1 is supposed to have. In order to overcome it, parameter can be supplied as key:value format. And command would look like below :

    mybatch.bar key1:value1 key2:value2
    

    Though there is no straight forward way to parse such argument list. I wrote following nested for loop in batch script which will basically parse and create environment variable with key1 as name, and value1 assigned to it. This way you can read all argument supplied using straight forward way of reading environment variable.

    @echo off
    FOR %%A IN (%*) DO (
       FOR /f "tokens=1,2 delims=:" %%G IN ("%%A") DO setLocal %%G=%%H
    )
    

    Afterwards, you can use %key1% format to read all argument being supplied.

    HTH

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