batch file包含variables的外部文件
我有一个batch file,我想包括包含一些variables(比如configurationvariables)的外部文件。 可能吗?
注:我假设Windowsbatch file,因为大多数人似乎没有意识到有显着的差异,只是盲目地调用黑色背景DOS上的灰色文本的一切。 不过,第一个变体也应该在DOS下工作。
可执行configuration
最简单的方法是将variables放在一个batch file中,每个都有自己的set语句: 
 set var1=value1 set var2=value2 ... 
并在你的主要批次:
 call config.cmd 
 当然,这也可以使variables有条件地创build,也可以根据系统的不同而有所不同,所以它是非常通用的。 但是,任意代码可以在那里运行,如果有语法错误,那么你的主批也会退出。 在UNIX世界中,这似乎相当普遍,特别是对于shell。 如果你考虑一下, autoexec.bat是没有别的。 
键/值对
 另一种方式是configuration文件中的某种var=value对: 
 var1=value1 var2=value2 ... 
然后你可以使用下面的代码来加载它们:
 for /f "delims=" %%x in (config.txt) do (set "%%x") 
 这和以前一样使用类似的技巧,就是在每一行上使用set 。 引号是为了避免像< , > , & , |  。 但是,当input中使用引号时,它们本身会中断。 在进一步处理用这些字符存储的variables中的数据时,你总是需要小心。 
一般来说,自动转义任意input,以避免在batch file中的头痛或问题,这似乎是不可能的。 至less我还没有find办法。 当然,第一个解决scheme是将这个责任推给编写configuration文件的人。
如果外部configuration文件也是有效的batch file,则可以使用:
 call externalconfig.bat 
在脚本里面 尝试创build以下a.bat:
 @echo off call b.bat echo %MYVAR% 
和b.bat:
 set MYVAR=test 
运行a.bat应该会生成输出:
 test 
批量使用小于和大于括号作为input和输出pipe道。
 >file.ext 
只使用上面的一个输出括号将覆盖该文件中的所有信息。
 >>file.ext 
使用双右括号将下一行添加到文件。
 ( echo echo )<file.ext 
这将根据文件的行执行参数。 在这种情况下,我们使用两行将使用“回声”键入的行。 左括号接触右括号表示来自该文件的信息将被传送到这些行中。
我已经编译了一个例子只读/写文件。 以下是将这些文件分成几部分来解释每个部分的function。
 @echo off echo TEST R/W set SRU=0 
在这个例子中SRU可以是任何东西。 如果您按Enter键太快,我们实际上将其设置为防止崩溃。
 set /p SRU=Skip Save? (y): if %SRU%==y goto read set input=1 set input2=2 set /p input=INPUT: set /p input2=INPUT2: 
现在,我们需要将variables写入文件。
 (echo %input%)> settings.cdb (echo %input2%)>> settings.cdb pause 
我使用.cdb作为“命令数据库”的简短格式。 您可以使用任何扩展名。 下一节将从头开始testing代码。 我们不想使用在文件开始处运行的设置variables,我们实际上希望它们从我们刚刚写的settings.cdb中加载。
 :read ( set /p input= set /p input2= )<settings.cdb 
所以,我们只是把你在文件开头写的前两行信息(你可以跳过设置行来检查以确保它正在工作)来设置input和input2的variables。
 echo %input% echo %input2% pause if %input%==1 goto newecho pause exit :newecho echo If you can see this, good job! pause exit 
这将显示settings.cdb被传送到括号中时设置的信息。 作为一个额外的好工作动力,按回车并设置我们之前设置为“1”的默认值将返回一个好的工作信息。 使用支架pipe道是双向的,比设置“FOR”的东西容易得多。 🙂
所以你只需要做对吧?
 @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 
我们不要忘记旧的参数。 启动* .bat或* .cmd文件时,最多可以在命令文件名后面添加9个参数:
 call myscript.bat \\path\to\my\file.ext type call myscript.bat \\path\to\my\file.ext "Del /F" 
示例脚本
myscript.bat可能是这样的:
 @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 
输出示例
 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:\> 
 :: 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 
输出:
$ RunCount = 1
$ RunCount = 2
$ RunCount = 3
上面概述的技术也可以用来在多个batch file之间共享variables。
资料来源: http : //www.incodesystems.com/products/batchfi1.htm