0

I need to run bat(fix.bat) in cmd batch file (let's say sample.cmd on c:) The problem is that despite of using: start /WAIT cmd.exe /C D:\dev\sys\fix.bat

fix.bat runs like from parent command (c:\sample.cmd) but I need to force it to run from its path ("d:\dev\sys")

How to run bat in other bat so the second one will run like executed from it's own path where it's located?

There in example would help to enter: "cd d:\dev\sys" and then call for fix.bat but I want to avoid that.

  • Is there any particular reason why you're saying start /WAIT cmd /c "command" instead of saying just cmd /c "command"? – G-Man Says 'Reinstate Monica' Aug 27 '14 at 23:13
0

There are several options:-

  1. Create a link using explorer: locate fix.bat, right click, select Copy, right click elsewhere and select Paste Shortcut; now rename to get rid of "Shortcut to " from the name and maybe the ".bat" as well: then change your parent command to call D:\dev\sys\fix.lnk instead of D:\dev\sys\fix.bat and the link will set the directory.
  2. Create a three-line file callfix.bat containing d:, cd \dev\sys and fix.bat: then call this instead of fix.bat in your parent command.
  3. In your parent command use start /WAIT cmd.exe /C "D: & cd \dev\sys & fix.bat".

Your choice will depend on which you find easiest to maintain and extend if your requirements change.

  • You can combine D: and cd \dev\sys into cd /d D:\dev\sys. And you might want to use && instead of &, so fix.bat doesn't run (in the current directory) if the cd fails (although I guess that's less of an issue if fix.bat is in D:\dev\sys). – G-Man Says 'Reinstate Monica' Aug 27 '14 at 23:10
  • I usually use TCC instead of CMD, so I haven't kept up with all the details of CMD commands, so thanks for pointing out cd /d. My second suggestion was proposed to allow flexibility for adding error handling, though your point about && is worth making. – AFH Aug 27 '14 at 23:24
0

You only have 2 choices.

1) Change your current directory within your sample.cmd script:

pushd d:\dev\sys
start /wait cmd.exe /c fix.bat

But you have stated you don't want to do that.

2) The only other choice is to modify fix.bat to change the current directory to where it is executing. The simplest way to do this is to add the following line at (or near) the top of the script:

pushd "%~f0"

But this could change the behavior for other users that expect the script to work from the current directory when the script is called.

EDIT or adopt one of the other suggestions from AFH :-)

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.