Solved

change directory and run cmd file

Posted on 2014-04-27
7
657 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 28

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 28

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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 68

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 28

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 68

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

706 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now