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

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
    
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
up vote 4 down vote accepted

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
share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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