Link to home
Start Free TrialLog in
Avatar of rutgermons
rutgermons

asked on

change directory and run cmd file

folks

I have 1 cmd file  called "test.cmd" in path c:\temp\test
 the other cmd called "count.cmd" in path c:\sync\count

how from path c:\temp do I run a batch to:

a) change directory and call the "test.cmd" in path c:\temp\test
 
when this is done run

b) change directory and call the ""count.cmd" in path c:\sync\count


all help will do

r
Avatar of becraig
becraig
Flag of United States of America image

Simple

In your batch file

pushd %pathvariable
do something
To get back to where you started add popd after the action is complete that will put you back in the original working directory.
Avatar of rutgermons
rutgermons

ASKER

could u provide the code pls?
More info on pushd

http://ss64.com/nt/pushd.html


a) change directory and call the "test.cmd" in path c:\temp\test
pushd  c:\temp\test
Run command

b) change directory and call the ""count.cmd" in path c:\sync\count
pushd c:\sync\count
Run command


popd then will take you back to the directory you started in
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Qlemo, I think is is linked to another question the OP already asked where call is used to run a cmd file.


I think both might be linked, as well I should have indicated I would have put the popd after each invocation of pushd:
a) change directory and call the "test.cmd" in path c:\temp\test
pushd  c:\temp\test
Run command
popd

Open in new window


b) change directory and call the ""count.cmd" in path c:\sync\count
pushd c:\sync\count
Run command 
popd

Open in new window

As good practice I would suggest the following within a batch file too.  This makes sure the current directory is that of the batch file, i.e.

Your main batch file

@echo off
call C:\temp\test\test.cmd
call C:\sync\count\count.cmd

Open in new window


test.cmd and count.cmd:

@echo off
pushd "%~dp0"
  echo Now in directory %cd% for %~nx0
  echo Do your stuff here, no need for any of these echo or pause if you don't want it...
  pause
popd

Open in new window


So then no matter where you put test.cmd and run it and whatever the properties of a shortcut says for working directory etc. it will work on it's own directory, even if you run it from a network drive as \\server\share\count.cmd it will map a temporary drive to \\server\share.

The "%~dp0" means get the filename of the batch file (%0) and get the (d)rive and (p)ath from it and then use that with pushd to change to the directory.

Bit overkill here but can save some strange errors when people decide to run your batch file from a shortcut or network drive etc. and it doesn't run as expected.

Steve
Agree to both points made. For bigger projects it is good style to put the pushd/popd into the called batch file (or subroutine, if used), so you do not have to care about proper setting of the current folder when calling.