Avatar of Michael Lam
Michael Lam
 asked on

DOS command how to change file names in a folder

let's say i have a bunch of files in a folder that all starts with the name "myfile_", e.g. myfile_a and myfile_b, is there a DOS command that can rename them all at once to hisfile_a and hisfile_b?  thanks.
Microsoft DOS

Avatar of undefined
Last Comment
M A

8/22/2022 - Mon
SOLUTION
Sumesh BNR

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.
ASKER CERTIFIED SOLUTION
HaiFai

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
Paul Tomasi

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.
Paul Tomasi

Like this in a batch file:

Copy and paste the code into Notepad and save it as say, RENAMEFILES.BAT in the same folder as your files. Then, fire up a DOS session and run the batch file by entering the command:

    RENAMEFILES

@echo off
setlocal enabledelayedexpansion

for %%a in ("myfile_*") do (
  set filename=%%a
  ren "%%a" "hisfile!filename:~6!
)

Open in new window

SOLUTION
Bill Prew

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.
Paul Tomasi

mmingfeilam

billprew's code above was meant to show his FOR-variables as %%A and %%B, not %A and %B. A simple oversight. Glad it's not just me it happens to.

:)
Bill Prew

Thanks Paul.

So, corrected, that would be:

@echo off
pushd c:\temp
for /F "tokens=1* delims=_" %%A in ('dir /a-d /b "myfile_*.*"') do ren "%%A_%%B" "hisfile_%%B"
popd

Open in new window

~bp
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Paul Tomasi

bill, I'm surprised to see you favour the 'FOR /F DIR approach over the more direct 'FOR %%'. I just hate the way DIR takes forever to process lots of files before the 'FOR /F' part has time to kick in. I've noticed some files actually change during this 'idle' period on tests I conducted over a year ago as DIR did not appear to lock the files.

May not be a problem but worth a mention.

:)
Bill Prew

I figured in this case it was a little simpler concept with no delayed expansion or variables, and also lent itself ever so slightly to changes in the base part of the file name, which felt like examples.  Using the split on the underscore rather than a hard coded length felt like a tiny bit easier for the poster to adapt.

But I do typically avoid the DIR in a FOR if possible, but it seemed to make sense in this case.

I just noticed, I think your last solution is missing a double quote at the end of line 6.

~bp
Paul Tomasi

bill, it's me thanking you this time.

It's missing a double-quote but fortunately, as DOS implies a closing double-quote at the end of the line it works with or without one.

Best to include it though so I'm relising the code here:

@echo off
setlocal enabledelayedexpansion

for %%a in ("myfile_*") do (
  set filename=%%a
  ren "%%a" "hisfile!filename:~6!"
)

Open in new window

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
M A

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.