6

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?

  • How are you switching directories? Are you using the /d option with chdir? – Aditya Sehgal Aug 22 '12 at 16:46
  • I am. chdir /d %OLDDIR% – Lusitanian Aug 22 '12 at 16:53
  • Why not simply allow the scripts to change the current directory and call them by an absolute path instead, possibly containing the starting directory as a variable, i.e.: "%mydir%\foo.bat > %mydir%\bar.txt"? – Marcks Thomas Aug 22 '12 at 17:17
5

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
1

Another Solution:

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

or

@ECHO OFF
call clearwebtemp.bat > clearwebtemp_out.txt
%~d0
cd %~p0
call clearblahtemp.bat > clearblahtemp_out.txt
%~d0
cd %~p0
call clearblihtemp.bat > clearblihtemp_out.txt

You can ignore the %~d0 if your bats :) stay on the same drive.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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