?
Solved

change directory and run cmd file

Posted on 2014-04-27
7
Medium Priority
?
664 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 70

Accepted Solution

by:
Qlemo earned 2000 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 70

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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Learn about cloud computing and its benefits for small business owners.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

801 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