6

How would you change the directory in a batch file then run the command in the new directory? I have got the batch file to change the directory, but will not run the command. It is not an exe, it is a file with -options. So basically I need to change the directory, then run a command that is saved in a string.

2
  • 2
    post the batch file you have written. Commented Sep 11, 2012 at 6:09
  • What is the command you are unable to run after changing directory? Better post the batch file as said by basiljames. Commented Sep 11, 2012 at 6:15

3 Answers 3

20

Try using

pushd yourdir
filetorun -options
Sign up to request clarification or add additional context in comments.

4 Comments

This is exactly what I needed. Worked perfectly. Thanks, Dan
No worries, if this answered your question you should click on the tick to mark this as accepted.
Normally preferable to "cd", since one can use popd to restore the original directory.
Why is pushd preferred over cd ?
2

I have given a sample code below to change the directory and run a command after that.

cd C:\    #Will change the directory to C:
ipconfig  #Will return IP address details(any command can be used here)
pause     #Will prevent command prompt from closing and waits for a keypress

Save this as a batch file filename.bat and you will get desired output. But make sure that the command entered is correct.

1 Comment

also, if you need to change drive as well as directory, use CD /D, as just CD will change the directory for that drive, but not change to that drive
1

"cd" stands for "ChangeDirectory". With "cd" command you can change the directory.

See also: http://en.wikipedia.org/wiki/Cd_(command)

Cheers

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.