批处理文件包含变量的外部文件

时间:2010-05-04 08:41:26

标签: batch-file

我有一个批处理文件,我想要包含一些包含一些变量的外部文件(比如配置变量)。有可能吗?

9 个答案:

答案 0 :(得分:111)

注意:我假设Windows批处理文件,因为大多数人似乎没有意识到存在显着差异,只是盲目地用黑色背景DOS上的灰色文本调用所有内容。尽管如此,第一个变体也应该在DOS中工作。

可执行配置

最简单的方法是将变量放在批处理文件中,每个文件都有自己的set语句:

set var1=value1
set var2=value2
...

并在您的主要批次中:

call config.cmd

当然,这也可以有条件地创建变量或者根据系统的方面创建变量,因此它非常通用。但是,任意代码都可以在那里运行,如果存在语法错误,那么您的主批也将退出。在UNIX世界中,这似乎相当普遍,特别是对于shell。如果你仔细想想,autoexec.bat就不算什么。

键/值对

另一种方式是配置文件中的某种var=value对:

var1=value1
var2=value2
...

然后,您可以使用以下代码段加载它们:

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

这利用了与以前类似的技巧,即在每一行上使用set。引号可以逃避<>&|等内容。但是,当输入中使用引号时,它们本身会中断。此外,在进一步处理存储有此类字符的变量中的数据时,您始终需要小心。

通常,对我来说,自动转义任意输入不会导致批处理文件中的麻烦或问题。至少我还没有找到办法。当然,对于第一个解决方案,您将责任推给编写配置文件的人。

答案 1 :(得分:28)

如果外部配置文件也是有效的批处理文件,您可以使用:

call externalconfig.bat

在你的脚本里面。尝试创建以下a.bat:

@echo off
call b.bat
echo %MYVAR%

和b.bat:

set MYVAR=test

运行a.bat应该生成输出:

test

答案 2 :(得分:2)

Batch使用小于和大于括号作为输入和输出管道。

>file.ext

仅使用上述一个输出括号将覆盖该文件中的所有信息。

>>file.ext

使用双右括号将下一行添加到文件中。

(
echo
echo
)<file.ext

这将根据文件的行执行参数。在这种情况下,我们使用两行将使用“echo”键入。左括号触及右括号括号表示该文件中的信息将通过管道传输到这些行中。

我编译了一个仅示例的读/写文件。下面是分解为各部分的文件,以解释每个部分的作用。

@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: 

现在,我们需要将变量写入文件。

(echo %input%)> settings.cdb
(echo %input2%)>> settings.cdb
pause

我使用.cdb作为“命令数据库”的简短形式。您可以使用任何扩展名。 下一节是从头开始测试代码。我们不想使用在文件开头运行的set变量,我们实际上希望它们从我们刚写的settings.cdb中加载。

:read
(
set /p input=
set /p input2=
)<settings.cdb

所以,我们只是管道你在文件开头写的前两行信息(您可以选择跳过设置行检查以确保它正常工作)来设置输入和输入2的变量

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通过管道输入括号时设置的信息。作为额外的好工作激励者,按Enter键并设置我们之前设置为“1”的默认值将返回一个好的工作消息。 使用支架管道是双向的,并且比设置“FOR”材料容易得多。 :)

答案 3 :(得分:1)

所以你必须这样做吗?:

@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

答案 4 :(得分:0)

:: 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

上述技术还可用于在多个批处理文件之间共享变量。

来源:http://www.incodesystems.com/products/batchfi1.htm

答案 5 :(得分:0)

Kinda的旧主题,但几天前我也有同样的问题,我想出了另一个主意(也许有人会觉得有用)

例如,您可以使用不同的主题(家庭,大小,颜色,动物)制作config.bat,并以任意顺序将其单独应用到批处理脚本中的任何位置:

@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

此后,您可以在任何地方使用它,方法是使用'call config.bat all'完全调用或仅调用其中的一部分(请参见下面的示例) 这里的想法是,当您可以选择不立即调用所有内容时,有时会更方便。有些变量可能您还不想被调用,所以您以后可以调用它们。

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

希望它可以帮助其他人在这里为我提供解答。

上述内容的简短版本:

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

美好的一天。

答案 6 :(得分:0)

对我来说,最好的选择是拥有键/值对文件,因为它可以从其他脚本语言读取。

另一件事是,我希望在值文件中有一个注释选项-使用eol命令中的for /f选项可以轻松实现。

这是例子

值文件:

;;;;;; file with example values ;;;;;;;;

;; Will be processed by a .bat file
;; ';' can be used for commenting a line

First_Value=value001

;;Do not let spaces arround the equal sign
;; As this makes the processing much easier
;; and reliable

Second_Value=%First_Value%_test

;;as call set will be used in reading script
;; refering another variables will be possible.

Third_Value=Something

;;; end

阅读脚本:

@echo off

:::::::::::::::::::::::::::::
set "VALUES_FILE=E:\scripts\example.values"
:::::::::::::::::::::::::::::

FOR /F "usebackq eol=; tokens=* delims=" %%# in (
    "%VALUES_FILE%"
) do (
    call set "%%#"
)

echo %First_Value% -- %Second_Value% -- %Third_Value%

答案 7 :(得分:-2)

让我们不要忘记好的旧参数。 启动* .bat或* .cmd文件时,您可以在命令文件名后添加最多九个参数:

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:\>

答案 8 :(得分:-3)

尝试使用具有可删除配置的方法时 我注意到它可能有效或可能无效 取决于脚本所在的位置:

调用config.cmd

我知道它没有任何感觉,但对我来说这是事实。 当&#34;调用config.cmd&#34;位于顶部 脚本,它的工作原理,但如果在脚本中它没有进一步说明。

由于没有工作,我的意思是变量没有设置为调用脚本。

非常奇怪!!!!