Batch file 如何";包括「;批处理脚本中的数据文件?

Batch file 如何";包括「;批处理脚本中的数据文件?,batch-file,Batch File,我想在批处理脚本中“包括”一个数据文件。让我向您解释一下我将如何在Unix shell脚本中实现它,这样就不必怀疑我试图在批处理脚本中实现什么 #!/bin/bash . data.txt # Load a data file. # Sourcing a file (dot-command) imports code into the script, appending to the script # same effect as the #include directive in a C p

我想在批处理脚本中“包括”一个数据文件。让我向您解释一下我将如何在Unix shell脚本中实现它,这样就不必怀疑我试图在批处理脚本中实现什么

#!/bin/bash
. data.txt # Load a data file.

# Sourcing a file (dot-command) imports code into the script, appending to the script
# same effect as the #include directive in a C program).
# The net result is the same as if the "sourced" lines of code were physically present in the body of the script.
# This is useful in situations when multiple scripts use a common data file or function library.

# Now, reference some data from that file.
echo "variable1 (from data.txt) = $variable1"
echo "variable3 (from data.txt) = $variable3"
这是data.txt:

# This is a data file loaded by a script.
# Files of this type may contain variables, functions, etc.
# It may be loaded with a 'source' or '.' command by a shell script.
# Let's initialize some variables.
variable1=22
variable2=474
variable3=5
variable4=97
message1="Hello, how are you?"
message2="Enough for now. Goodbye."

在批处理脚本中,我的目的是在data.txt中设置环境变量,并在每个批处理脚本中设置该文件的“source”,我将在后面创建这些脚本。这也将帮助我修改环境变量,只修改一个文件(data.txt),而不是修改多个批处理脚本。有什么想法吗

在DOS批处理文件中没有自动执行此操作的方法。您必须在循环中标记该文件。比如:

 for /f "tokens=1,2 delims==" %i in (data.txt) do set %i=%j

当然,这一行代码并不能解释示例data.txt文件中的注释行。

在DOS批处理文件中没有自动的方法。您必须在循环中标记该文件。比如:

 for /f "tokens=1,2 delims==" %i in (data.txt) do set %i=%j

当然,这行代码并不能解释示例data.txt文件中的注释行。

最简单的方法是在data.bat文件中存储多个SET命令,然后任何批处理脚本都可以调用这些命令。例如,这是data.bat:

rem This is a data file loaded by a script.
rem Files of this type may contain variables and macros.
rem It may be loaded with a CALL THISFILE command by a Batch script.
rem Let's initialize some variables.
set variable1=22
set variable2=474
set variable3=5
set variable4=97
set message1="Hello, how are you?"
set message2="Enough for now. Goodbye."
要在任何脚本中“源”此数据文件,请使用:

call data.bat
Adenddum:使用函数“包含”辅助(库)文件的方法

“包含”文件的使用函数(子例程)不像变量那样直接,但可以这样做。要在批处理中执行此操作,需要将data.bat文件物理插入原始批处理文件中。当然,这可以通过文本编辑器来完成!但它也可以通过一个名为source.bat的非常简单的批处理文件以自动化方式实现:

@echo off
rem Combine the Batch file given in first param with the library file
copy %1+data.bat "%~N1_FULL.bat"
rem And run the full version
%~N1_FULL.bat%
例如,BASE.BAT:

@echo off
call :Initialize
echo variable1 (from data.bat) = %variable1%
echo variable3 (from data.bat) = %variable%
call :Display
rem IMPORTANT! This file MUST end with GOTO :EOF!
goto :EOF
DATA.BAT:

:Initialize
set variable1=22
set variable2=474
set variable3=5
set variable4=97
exit /B

:display
echo Hello, how are you?
echo Enough for now. Goodbye.
exit /B
您甚至可以更复杂地执行source.bat,因此它会检查基本文件和库文件的修改日期,并仅在需要时创建_完整版本


我希望它能有所帮助……

最简单的方法是将多个SET命令存储在data.bat文件中,然后可以由任何批处理脚本调用。例如,这是data.bat:

rem This is a data file loaded by a script.
rem Files of this type may contain variables and macros.
rem It may be loaded with a CALL THISFILE command by a Batch script.
rem Let's initialize some variables.
set variable1=22
set variable2=474
set variable3=5
set variable4=97
set message1="Hello, how are you?"
set message2="Enough for now. Goodbye."
要在任何脚本中“源”此数据文件,请使用:

call data.bat
Adenddum:使用函数“包含”辅助(库)文件的方法

“包含”文件的使用函数(子例程)不像变量那样直接,但可以这样做。要在批处理中执行此操作,需要将data.bat文件物理插入原始批处理文件中。当然,这可以通过文本编辑器来完成!但它也可以通过一个名为source.bat的非常简单的批处理文件以自动化方式实现:

@echo off
rem Combine the Batch file given in first param with the library file
copy %1+data.bat "%~N1_FULL.bat"
rem And run the full version
%~N1_FULL.bat%
例如,BASE.BAT:

@echo off
call :Initialize
echo variable1 (from data.bat) = %variable1%
echo variable3 (from data.bat) = %variable%
call :Display
rem IMPORTANT! This file MUST end with GOTO :EOF!
goto :EOF
DATA.BAT:

:Initialize
set variable1=22
set variable2=474
set variable3=5
set variable4=97
exit /B

:display
echo Hello, how are you?
echo Enough for now. Goodbye.
exit /B
您甚至可以更复杂地执行source.bat,因此它会检查基本文件和库文件的修改日期,并仅在需要时创建_完整版本


我希望它能帮助……

+1。谢谢是否可以在data.bat中定义函数,以便调用data.bat的脚本可以使用这些函数?+1。谢谢是否可以在data.bat中定义函数,以便调用data.bat的脚本可以使用这些函数?