Avatar of Software Engineer
Software Engineer
 asked on

Editing a .bat file to point to a different directory

Hello:

The syntax in our macro's .bat file contains a line, referencing a directory that is on our machine's C drive.

We need to change this line to point to a directory, on the machine's D drive, instead.

The following is the line: cd\Program Files (x86)\Microsoft ...\...

How can we modify that line to not point to the C drive but to point to the D drive?

Thank you!

John
Windows Server 2008Windows Batch

Avatar of undefined
Last Comment
Bill Prew

8/22/2022 - Mon
aikimark

If the directory exists on the D: drive then just add
D:

Open in new window

to the line before the CD line
Software Engineer

ASKER
Do you mean like this:


D:
cd\Program Files (x86)\Microsoft ... \...

Or, like this:

D:cd\Program Files (x86)\Microsoft ... \...

John
ASKER CERTIFIED SOLUTION
NVIT

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
aikimark

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Bill Prew

Another technique that can be used in batch script is PUSHD and POPD.  They work very similar to the CD except PUSHD remembers what the current directory was before it went to a new one, and then POPD goes back to that.  Like most scripting and programming there are often multiple ways to accomplish the same result, some people prefer different flavors.  I often like the PUSHD / POPD approach so that I end up back where I started after I'm done and leave things as much like they were before the BAT ran.

So you could do the below.  In all cases (CD or PUSHD) I would strongly suggest you make sure you are where you expect to be after this.  CD will display an error for example if the folder does not exist, but your script will go happily along its way and you could act on things in a different folder than you expected.

pushd d:\Program Files (x86)\Microsoft . . .
if "%CD%" NEQ "d:\Program Files (x86)\Microsoft . . ." (
    echo Unable to change to folder d:\Program Files (x86)\Microsoft . . ., exiting.
    exit /b
)

rem perform your logic here...

popd

Open in new window

~bp
Your help has saved me hundreds of hours of internet surfing.
fblack61