Sign up ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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


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


  1. Is this the best way to centralize values and if not, what is a better way?
  2. Do I need to specify setlocal enableextensions enabledelayedexpansion in all the existing batch files?
  3. Do all the batch files have to have @echo off or is it sufficient for just the config.bat file?
  4. Is start /wait the best way to call multiple files? Can I pass values from one batch file to another using the said command?
  5. 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"
share|improve this question

closed as not constructive by Nifle, Indrek, 8088, Mokubai, Renan Sep 2 '12 at 17:09

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.If this question can be reworded to fit the rules in the help center, please edit the question.

i would recommend you to extend beyond batch files using WScript...this will enable to you to incorporate much advanced stuff into the script –  tumchaaditya Aug 29 '12 at 6:52

1 Answer 1

up vote 1 down vote accepted
  1. It’s not bad, but if the external batch-files are simple, you could just put their code in the main batch-file as subroutines and just call their labels. For example:

    These four files:
    @echo off
    call compress.bat …
    call validate.bat …
    call delete.bat …
    @echo off
    7z a -tzip -mx9 -blah -blah
    @echo off
    7z t
    @echo off
    delete "%1"
    Become one:
    @echo off
      call compress …
      call validate …
      call delete …
    goto :eof
      7z a -tzip -mx9 -blah -blah
    goto :eof
      7z t
    goto :eof
      delete "%1"
    goto :eof
  2. You only need enableextensions and/or enabledelayedexpansion if you are going to be using them. I don’t know what you have in your batch files, but if you’re not, then you can leave them out.

  3. You don’t have to use them in the external batch-files if you can them from another one that turns echo off, but it’s just a good habit to include it as a standard header, particularly for batch-files that you could call by themselves (e.g. calling compress.bat from the command-prompt manually). There’s very few scenarios where you would specifically want/need to not include it.

  4. You can use start /wait, but simply calling an external file automatically waits for it to return by default, so it should not be necessary. You usually want to use it when calling a GUI program from a batch file. Further, calling start complicates passing arguments to the target program more so than simply calling them.

  5. More than likely, yes. The loop parameters are local to the for loop, so the other batch-file will not see them unless they happen to have a for loop, and even then, only in the loop itself:

    C:\t>type baz.bat
    @for %%a in (1) do call foobar.bat %%a
    C:\t>type foobar.bat
    @echo off
    echo %1
    echo %%a
    for %%a in (1) do echo %%a
    echo %%a
    C:\t>call foobar.bat 1
share|improve this answer
Thanks for the detailed response. Been attempting to understand when you can pass %1. My understanding is that this is the value inputted by the user. Haven't seen too many examples explaining this clearly. Secondly again I don't have a detailed understand of enableextensions and enabledelayedexpansion. I had a read of the examples you cited before I posted my question but I only included them because I came across a post noting to use them if calling variables within a file. –  PeanutsMonkey Aug 29 '12 at 6:08
The reason I used start /wait was because of my issue at… –  PeanutsMonkey Aug 29 '12 at 6:15
What do you mean by unless they happen to have a for loop? I would almost all of my batch files have for loops in them so I take it that I could run into issues using the same variable names. How do overcome an issue like that if you have numerous for loops. –  PeanutsMonkey Aug 29 '12 at 6:23
Re: %1, You’re close; %1 is just the batch-file’s first argument (argv[1] if you know C). It can be passed on the command-line by the user, or in a call by another batch-file. You don’t pass “%1” itself; you call the target batch-file and add the data you want to pass to the call, then in the target batch-file you use %1 to access that data. You could pass %1 from one batch-file to another if you specifically wanted to pass the first file’s (first) argument to the second batch-file, but then in the second one, it may not map to %1, it may be %2 or something else. –  Synetech Aug 29 '12 at 14:22
Re: start, You have to use start to call winscp because that’s a separate program that continues to run but returns, so the batch-file continues. By using start, the batch-file that called it waits for the process to terminate (including any windows it creates). You need to use start in the batch file that calls winscp, but not in the batch-file that calls the batch-file that calls winscp because the first batch-file waits for the second one to finish automatically, so if you use start to force the second one to wait for winscp to finish, then the first file is automatically paused. –  Synetech Aug 29 '12 at 14:25

Not the answer you're looking for? Browse other questions tagged or ask your own question.