The Syntax
RENAME [drive:][path]filename1 filename2.REN [drive:][path]filename1 filename2.
Note that you cannot specify a new drive or path for your destination file.
For example,
ren c:\temp\test.txt abc.txt
ren *.txt abc.txt
The first parameter is the file selection criteria. It selects all files that match this criteria, then renames the files based on the second parameter.
The selected files are renamed one at a time, with character by character mode. As it works through the files, it stops as soon as an error is encountered, and the rests of the files will be left untouched.
Wildcards
Wildcards * and ? can be used with REN for matching and renaming.The * wildcard will match any sequence of characters
(0 or more, including NULL characters)
The ? wildcard will match a single character
(or a NULL at the end of a filename)
Consider Renaming the Following Files
X-test-01.txtY-test-02.txt
Z-test-03.txt
Some middle characters are fixed throughout the files while they varies at the beginning and the ending part of the names.
Renaming Middle Part, Same Number of Characters
Renaming the middle part is straightforward when the targeted result has the same number of characters.For example,
ren ?-test-0?.txt ?-good-0?.txt
ren ??test???.txt ??good???.txt
ren ??test???.txt ??good*
These rename middle part "test" to "good". i.e. they become
X-good-01.txt
Y-good-02.txt
Z-good-03.txt
Renaming Middle Part, Different Number of Characters
It is a bit tricky if you want to rename the middle part but with different number of characters. This cannot be done with a single REN command, because REN works in character by character mode.If you try these,
ren ??test???.txt ??goodjobs???.txt
"job" will not be inserted, but copied over character by character. The result will be,
X-goodjobs.txt
Y-goodjobs.txt
Z-goodjobs.txt
For example, you can rename the following files,
X-test-a.txt
Y-test-a.txt
Z-test-a.txt
to
X-goodjob-a.txt
Y-goodjob-a.txt
Z-goodjob-a.txt
ren ??test??.txt ??goodjob-a.txt
Returning to the original set of files, the following batch script works:
for %%i in (??test???.txt) do (set fname=%%i) & call:rename
goto finish
:rename
ren %fname% %fname:~0,2%goodjob%fname:~-7%
goto :eof
:finish
%fname:~0,2% selects the first two characters from the filename.
%fname:~-7% selects the last seven characters from the filename.
Note:
Use LFNFOR to turn on long file name support when processing for command.
No comments:
Post a Comment