Page 1 of 1

Change Directory in Batch file

Posted: 31 May 2012 11:05
by ApRui
Hey, I'm getting started in Batch language and I'm new in this forum. So just in case I spill out any noob comment just don't be mean please xD
I wanna make a central Batch file that gives you the option to press a number and to goes to the wished file. I've already discovered that it can be done by the "Change Directory" mechanic. But I can't find the right code. I know how to open a simple program like ms paint but I can't do the same for a specific file.
Thanks in advance for all the help, guys! :)

Re: Change Directory in Batch file

Posted: 31 May 2012 11:46
by Fawers
In order to open a specific file - whose path is not listed in %PATH% -, you need either the full path for the file or the cmd prompt pointing to the verydirectory of this file - as you said, "change directory".

The command is CD.
For the command help, type CD /? in your cmd prompt.
It's also got a switch: /D. You use the /D switch when you're 'traveling' from one drive to another. If you're changing your path with a path in a variable, it is preferred to use /D.

Say you've got a README.DOCX file for a program, and say this program is under "Program Files".
You want to run this:

Code: Select all

cd "%programfiles%\[program folder]"

Quotes are optional for CD and PUSHD commands; I like to use them anyway.
Once you've got cmd pointing to the specified folder, you can run the file directly.

Code: Select all

start readme.docx
::or
readme.docx

By default, "readme.docx" (without a 'start' in the beginning) will open the file and wait for it to close to continue the program (assuming you're running from a batch file and not from the command line).