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.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
:)
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.
:)
Thanks Paul.
So, corrected, that would be:
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
~bp
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.
:)
May not be a problem but worth a mention.
:)
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
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
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:
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!"
)
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
Open in new window