Could I centralize batch files more efficiently?
Posted
by
PeanutsMonkey
on Super User
See other posts from Super User
or by PeanutsMonkey
Published on 2012-08-29T03:17:04Z
Indexed on
2012/08/29
3:41 UTC
Read the original article
Hit count: 351
I am new to the world of batch scripting so please forgive what may appear as basic questions. I am learning as I get assigned different jobs and I am a huge proponent of automation where possible. I have several batch files that perform several tasks. Each of these files had their paths hard-coded e.g. c:\temp. d:\data, etc in the batch file. Initially I moved these to a text file I could call from a batch file e.g.
for /f "tokens=1,2 delims==" %%R in (config.txt) do (
if %%R==bdata set bdata=%%S
if %%R==cdata set cdata=%%S
)
The config.txt file contains these values
bdata=c:\temp
cdata=d:\data
I realized that each time I would need to create a new variable, I would need to update the config.txt file as well the config.bat files.
I decided I would move all the values to just the config.bat file as follows
set bdata=c:\temp
set cdata=d:\data
I then updated each of the existing batch files to call the variables rather than the hard-coded paths. I also added the following lines of code to each batch file except config.bat. The only additional line added to the config.bat file is @echo off
.
@echo off
setlocal enableextensions enabledelayedexpansion
call config.bat
I then have another batch file that centralizes calling all the batch files in sequence. The name of this batch file is start.bat. The reason I am using start /wait
is because there have been instances of where the delete.bat
runs before compress.bat
has had an opportunity to finish.
start /wait compress.bat
start /wait validate.bat
start /wait delete.bat
Questions
- Is this the best way to centralize values and if not, what is a better way?
- Do I need to specify
setlocal enableextensions enabledelayedexpansion
in all the existing batch files? - Do all the batch files have to have @echo off or is it sufficient for just the config.bat file?
- Is
start /wait
the best way to call multiple files? Can I pass values from one batch file to another using the said command? - All the batch files have different functions e.g. move, delete, etc however use %%a or %%b. Is this okay?
For example
The validate.bat
file has the code
for %%a in (%bdata%\*.*) do if "%%~xa" == "" move /Y "%bdata%\%%~xa" "%bdata%\%done%"
and the delete.bat file
has the code
for %%a in (%bdata%\*.*) do if "%%~xa" == ".txt" del "%%a"
© Super User or respective owner