Batch file include external file for variables

后端 未结 9 1057
遥遥无期
遥遥无期 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 09:10

    Let's not forget good old parameters. When starting your *.bat or *.cmd file you can add up to nine parameters after the command file name:

    call myscript.bat \\path\to\my\file.ext type
    call myscript.bat \\path\to\my\file.ext "Del /F"
    

    Example script

    The myscript.bat could be something like this:

    @Echo Off
    Echo The path of this scriptfile %~0
    Echo The name of this scriptfile %~n0
    Echo The extension of this scriptfile %~x0
    Echo.
    If "%~2"=="" (
       Echo Parameter missing, quitting.
       GoTo :EOF
    )
    If Not Exist "%~1" (
       Echo File does not exist, quitting.
       GoTo :EOF
    )
    Echo Going to %~2 this file: %~1
    %~2 "%~1"
    If %errorlevel%  NEQ 0 (
       Echo Failed to %~2 the %~1.
    )
    @Echo On
    

    Example output

    c:\>c:\bats\myscript.bat \\server\path\x.txt type
    The path of this scriptfile c:\bats\myscript.bat
    The name of this scriptfile myscript
    The extension of this scriptfile .bat
    
    Going to type this file: \\server\path\x.txt
    This is the content of the file:
    Some alphabets: ABCDEFG abcdefg
    Some numbers: 1234567890
    
    c:\>c:\bats\myscript.bat \\server\path\x.txt "del /f "
    The path of this scriptfile c:\bats\myscript.bat
    The name of this scriptfile myscript
    The extension of this scriptfile .bat
    
    Going to del /f  this file: \\server\path\x.txt
    
    c:\>
    

提交回复
热议问题