Windows – How to launch batch files from another batch file while piping their output and retaining the current working directory

batchbatch filecommand linewindows

I have a batch file which runs as a scheduled task. For the sake of example lets call it alltasks.bat.

It looks like the following:

@ECHO OFF
clearwebtemp.bat > clearwebtemp_out.txt
clearblahtemp.bat > clearblahtemp_out.txt
clearblihtemp.bat > clearblihtemp_out.txt

This works fine when none of the called scripts change directories, but if they do then the main script fails to complete the remaining batch calls or pipe the output to the files.

I tried using the start command, but that predictably piped the output of the start command rather than that of the batch file itself.

I also tried retaining the current working directory using the %CD% variable at the top of the script and then switching back to it after each call, but the piping out to the file was done inside the directory that the script switched to, which didn't seem to make sense.

Is there an obvious (or not-so-obvious) way to achieve what I'm attempting to accomplish?

Best Answer

This should do what you're asking for:

cmd /c clearwebtemp.bat > clearwebtemp_out.txt
cmd /c clearblahtemp.bat > clearblahtemp_out.txt
cmd /c clearblihtemp.bat > clearblihtemp_out.txt
Related Question