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

前端 未结 9 1736
情话喂你
情话喂你 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 20:54

    My gosh people; You're overthinking this! :)

    OP's CMD args are all in the form "[VarName]=[Something]"

    So the 1st line could simply be FOR %%_ IN (%*) DO SET "%~_"

    or in nicer looking code

    FOR %%_ IN (%*) DO SET "%~_"
    

    That said here is a more complete script example where you could just put all of your script within the :Main function

    @( Setlocal
      ECHO OFF
      FOR %%_ IN (%*) DO SET "%~_"
    )
    
    CALL :Main
    
    ( ENDLOCAL
      EXIT /B )
    
    :Main
      REM Your code goes here.
      REM Your code goes here.
      REM Your code goes here.
      REM Your code goes here.
    GOTO :EOF
    

提交回复
热议问题