Solved

change directory and run cmd file

Posted on 2014-04-27
7
659 Views
Last Modified: 2014-04-27
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
0
Comment
Question by:rutgermons
7 Comments
 
LVL 29

Expert Comment

by:becraig
ID: 40026217
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.
0
 

Author Comment

by:rutgermons
ID: 40026218
could u provide the code pls?
0
 
LVL 29

Expert Comment

by:becraig
ID: 40026219
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
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 69

Accepted Solution

by:
Qlemo earned 500 total points
ID: 40026228
Not exactly. Two pushd require two popd ;-). But I wouldn't use a second pushd in the same batch unless I need to make sure to get back to the directory I started with.
More, the call of a batch file really needs the call statement, else the called batch replaces the calling one completely, and execution will never continue in it:
pushd c:\temp\test
call test.cmd
cd c:\sync\count
call count.cmd
popd

Open in new window

0
 
LVL 29

Expert Comment

by:becraig
ID: 40026232
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

0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 40026247
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
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 40026307
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.
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Show hidden user account 7 59
Script to write to a log file 18 30
MS Endpoint Protection 2 22
vbscript to installing all steps of softwares automatically 7 18
This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question