how to run a cmd.exe batch file in a sub shell

I have a batch file that I usually invoke like this:

 longjob.cmd >result.txt 2>&1 

This works fine, but the script changes directory during its execution leaving my shell in that directory - which is a nuisance.

Is there a way to run the command within a sub-shell - while still allowing the output to be captured ?

I have tried

cmd longjob.cmd >result.txt 2>&1 

which just sits waiting for an exit command.

Also I tried

start longjob.cmd >result.txt 2>&1 

which does run the script, but in a new window and all output is sent to that window instead of the file.

2

2 Answers

Try

CMD /C longjob.cmd >result.txt 2>&1 

Not sure how it'll deal with the redirection, but CMD /C lets you tell CMD what to run and that it should exit when done. (CMD /K lets you tell it to run something but stick around when done.) It will re-use the existing console window if run within one.

2

The call command might be what you want.

i.e.

call longjob.cmd >result.txt 2>&1 
2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like