Batch file include external file for variables

后端 未结 9 1037
遥遥无期
遥遥无期 2020-12-04 08:20

I have a batch file and I want to include an external file containing some variables (say configuration variables). Is it possible?

相关标签:
9条回答
  • 2020-12-04 08:54

    While trying to use the method with excutable configuration I noticed that it may work or may NOT work depending on where in the script is located the call:

    call config.cmd

    I know it doesn't make any sens, but for me it's a fact. When "call config.cmd" is located at the top of the script, it works, but if further in the script it doesn't.

    By doesn't work, I mean the variable are not set un the calling script.

    Very very strange !!!!

    0 讨论(0)
  • 2020-12-04 08:55

    If the external configuration file is also valid batch file, you can just use:

    call externalconfig.bat
    

    inside your script. Try creating following a.bat:

    @echo off
    call b.bat
    echo %MYVAR%
    

    and b.bat:

    set MYVAR=test
    

    Running a.bat should generate output:

    test
    
    0 讨论(0)
  • 2020-12-04 08:58

    Kinda old subject but I had same question a few days ago and I came up with another idea (maybe someone will still find it usefull)

    For example you can make a config.bat with different subjects (family, size, color, animals) and apply them individually in any order anywhere you want in your batch scripts:

    @echo off
    rem Empty the variable to be ready for label config_all
    set config_all_selected=
    
    rem Go to the label with the parameter you selected
    goto :config_%1
    
    REM This next line is just to go to end of file 
    REM in case that the parameter %1 is not set
    goto :end
    
    REM next label is to jump here and get all variables to be set
    :config_all
    set config_all_selected=1
    
    
    :config_family
    set mother=Mary
    set father=John
    set sister=Anna
    rem This next line is to skip going to end if config_all label was selected as parameter
    if not "%config_all_selected%"=="1" goto :end
    
    :config_test
    set "test_parameter_all=2nd set: The 'all' parameter WAS used before this echo"
    if not "%config_all_selected%"=="1" goto :end
    
    :config_size
    set width=20
    set height=40
    if not "%config_all_selected%"=="1" goto :end
    
    
    :config_color
    set first_color=blue
    set second_color=green
    if not "%config_all_selected%"=="1" goto :end
    
    
    :config_animals
    set dog=Max
    set cat=Miau
    if not "%config_all_selected%"=="1" goto :end
    
    
    :end
    

    After that, you can use it anywhere by calling fully with 'call config.bat all' or calling only parts of it (see example bellow) The idea in here is that sometimes is more handy when you have the option not to call everything at once. Some variables maybe you don't want to be called yet so you can call them later.

    Example test.bat

    @echo off
    
    rem This is added just to test the all parameter
    set "test_parameter_all=1st set: The 'all' parameter was NOT used before this echo"
    
    call config.bat size
    
    echo My birthday present had a width of %width% and a height of %height%
    
    call config.bat family
    call config.bat animals
    
    echo Yesterday %father% and %mother% surprised %sister% with a cat named %cat%
    echo Her brother wanted the dog %dog%
    
    rem This shows you if the 'all' parameter was or not used (just for testing)
    echo %test_parameter_all%
    
    call config.bat color
    
    echo His lucky color is %first_color% even if %second_color% is also nice.
    
    echo.
    pause
    

    Hope it helps the way others help me in here with their answers.

    A short version of the above:

    config.bat

    @echo off
    set config_all_selected=
    goto :config_%1
    goto :end
    
    :config_all
    set config_all_selected=1
    
    :config_family
    set mother=Mary
    set father=John
    set daughter=Anna
    if not "%config_all_selected%"=="1" goto :end
    
    :config_size
    set width=20
    set height=40
    if not "%config_all_selected%"=="1" goto :end
    
    :end
    

    test.bat

    @echo off
    
    call config.bat size
    echo My birthday present had a width of %width% and a height of %height%
    
    call config.bat family
    echo %father% and %mother% have a daughter named %daughter%
    
    echo.
    pause
    

    Good day.

    0 讨论(0)
  • 2020-12-04 09:05
    :: savevars.bat
    :: Use $ to prefix any important variable to save it for future runs.
    
    @ECHO OFF
    SETLOCAL
    
    REM Load variables
    IF EXIST config.txt FOR /F "delims=" %%A IN (config.txt) DO SET "%%A"
    
    REM Change variables
    IF NOT DEFINED $RunCount (
        SET $RunCount=1
    ) ELSE SET /A $RunCount+=1
    
    REM Display variables
    SET $
    
    REM Save variables
    SET $>config.txt
    
    ENDLOCAL
    PAUSE
    EXIT /B
    

    Output:

    $RunCount=1

    $RunCount=2

    $RunCount=3

    The technique outlined above can also be used to share variables among multiple batch files.

    Source: http://www.incodesystems.com/products/batchfi1.htm

    0 讨论(0)
  • 2020-12-04 09:07

    Note: I'm assuming Windows batch files as most people seem to be unaware that there are significant differences and just blindly call everything with grey text on black background DOS. Nevertheless, the first variant should work in DOS as well.

    Executable configuration

    The easiest way to do this is to just put the variables in a batch file themselves, each with its own set statement:

    set var1=value1
    set var2=value2
    ...
    

    and in your main batch:

    call config.cmd
    

    Of course, that also enables variables to be created conditionally or depending on aspects of the system, so it's pretty versatile. However, arbitrary code can run there and if there is a syntax error, then your main batch will exit too. In the UNIX world this seems to be fairly common, especially for shells. And if you think about it, autoexec.bat is nothing else.

    Key/value pairs

    Another way would be some kind of var=value pairs in the configuration file:

    var1=value1
    var2=value2
    ...
    

    You can then use the following snippet to load them:

    for /f "delims=" %%x in (config.txt) do (set "%%x")
    

    This utilizes a similar trick as before, namely just using set on each line. The quotes are there to escape things like <, >, &, |. However, they will themselves break when quotes are used in the input. Also you always need to be careful when further processing data in variables stored with such characters.

    Generally, automatically escaping arbitrary input to cause no headaches or problems in batch files seems pretty impossible to me. At least I didn't find a way to do so yet. Of course, with the first solution you're pushing that responsibility to the one writing the config file.

    0 讨论(0)
  • 2020-12-04 09:07

    So you just have to do this right?:

    @echo off
    echo text shizzle
    echo.
    echo pause^>nul (press enter)
    pause>nul
    
    REM writing to file
    (
    echo XD
    echo LOL
    )>settings.cdb
    cls
    
    REM setting the variables out of the file
    (
    set /p input=
    set /p input2=
    )<settings.cdb
    cls
    
    REM echo'ing the variables
    echo variables:
    echo %input%
    echo %input2%
    pause>nul
    
    if %input%==XD goto newecho
    DEL settings.cdb
    exit
    
    :newecho
    cls
    echo If you can see this, good job!
    DEL settings.cdb
    pause>nul
    exit
    
    0 讨论(0)
提交回复
热议问题