Solved

change directory and run cmd file

Posted on 2014-04-27
7
661 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
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

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

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

Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
A quick step-by-step overview of installing and configuring Carbonite Server Backup.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

733 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