Batch file 批处理文件包括变量的外部文件

Batch file 批处理文件包括变量的外部文件,batch-file,transclusion,Batch File,Transclusion,我有一个批处理文件,我想包含一个包含一些变量(比如配置变量)的外部文件。有可能吗?注意:我假设Windows批处理文件,因为大多数人似乎不知道它们之间存在显著差异,只是盲目地在黑色背景DOS上用灰色文本调用所有文件。然而,第一个变体也应该在DOS中工作 可执行配置 最简单的方法是将变量本身放入批处理文件中,每个变量都有自己的set语句: set var1=value1 set var2=value2 ... 在您的主要批次中: call config.cmd 当然,这也允许有条件地创建变量或

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

注意:我假设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
。引号用于转义
&
|
。但是,当在输入中使用引号时,它们本身将中断。此外,在进一步处理使用此类字符存储的变量中的数据时,始终需要小心


一般来说,自动转义任意输入以避免批处理文件中出现问题对我来说是不可能的。至少我还没有找到这样做的方法。当然,对于第一个解决方案,您将责任推给编写配置文件的人。

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

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

@echo off
call b.bat
echo %MYVAR%
b.蝙蝠:

set MYVAR=test
运行a.bat应生成以下输出:

test

我们不要忘记好的旧参数。 启动*.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
示例输出
批处理使用小于和大于括号作为输入和输出管道

>file.ext
仅使用上面的一个输出括号将覆盖该文件中的所有信息

>>file.ext
使用右括号将下一行添加到文件中

(
echo
echo
)<file.ext
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
这将显示将settings.cdb导入括号时设置的信息。作为一个额外的好工作激励因素,按enter键并设置我们之前设置为“1”的默认值将返回一条好工作消息。
使用支架管道是双向的,比设置“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
@echo关闭
回声文本屏蔽
回声。
回声暂停^>nul(按enter键)
暂停>nul
REM写入文件
(
回声XD
回音LOL
)>设置.cdb
cls
REM从文件中设置变量
(
设置/p输入=
设置/p输入2=
)
输出:

$RunCount=1

$RunCount=2

$RunCount=3

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


来源:

尝试使用具有可执行配置的方法时 我注意到它可能起作用,也可能不起作用 根据脚本中调用的位置:

调用config.cmd

我知道这没有任何意义,但对我来说这是事实。 当“call config.cmd”位于 脚本,它工作,但如果在脚本中进一步,它不会

我的意思是,在调用脚本中没有设置变量


非常非常奇怪

有点老了,但几天前我有同样的问题,我想出了另一个想法(也许有人会发现它仍然有用)

例如,您可以使用不同的主题(家庭、大小、颜色、动物)制作一个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
测试蝙蝠

@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

您好。

我认为最好的选择是使用键/值对文件,因为它可以从其他脚本语言读取

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

下面是一个例子

值文件:

;;;;;; 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%

这对我不起作用,你能检查一下吗:如果在另一个脚本中并行使用配置文件(用
call
调用它,顺便说一句,这是同步的),它会说“进程无法访问文件,因为它正在被使用”,并且无法被调用。这对我不起作用,你能检查一下:我的实现:
@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
@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
;;;;;; 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%