Jin
Jin

Reputation: 165

Renaming file paths in batch files

I need to execute several rename commands in a batch file and wish to store the batch file in a directory different from the directory storing the files that need to be renamed.

The following is a sample file

ren C:\test\old1.txt new1.txt 

ren C:\test\old2.txt new2.txt 

I would ideally like to not have to copy the directory path everytime, since I might need to change the folder in which these files reside. I tried the following but it does not work. Probably I am not understanding the set command correctly.

set dirpath=C:\test\

ren %dirpath%old1.txt new1.txt 

ren %dirpath%old2.txt new2.txt 

Any ideas how I might achieve this effect.

Upvotes: 2

Views: 8624

Answers (1)

xxbbcc
xxbbcc

Reputation: 17327

Your batch file seems to be correct. Just to make it play well with path names that contain embedded spaces, put double quotes around the name, like so:

set dirpath=C:\test\

ren "%dirpath%old.txt" new.txt

Obviously, if you have a hardcoded path (C:\test\) you shouldn't even need that. I tried your batch file locally and worked fine.

Upvotes: 4

Related Questions