I want to “include” a data file in the batch script. Let me explain you as to how I would do it in Unix shell script so that there are no doubts as to what I am trying to achieve in batch script.
#!/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"
And this is the 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."
In the batch script, my intention is to set the environment variables in data.txt and “source” that file in each of the batch script that I will create latter on. This will also help me make changes to the environment variables by just modifying one file (data.txt) instead of modifying multiple batch scripts. Any ideas?
The easiest way to do that is by storing multiple SET commands in a data.bat file that then can be called by any batch script. For example, this is data.bat:
To “source” this data file in any script, use:
Adenddum: Way to “include” an auxiliary (library) file with functions.
Use functions (subroutines) of the “included” file is not so direct as variables, but may be done. To do that in Batch, you need to physically insert the data.bat file into the original Batch file. Of course, this may be done with a text editor! But it may also be achieved in an automated way with the aid of a very simple Batch file called source.bat:
For example, BASE.BAT:
DATA.BAT:
You may even do source.bat more sophisticated, so it check the modification dates of the base and library files and create the _FULL version only when needed.
I hope it helps…