Rob van der Woude's Scripting Pages

Rename Files

Ever since the earliest DOS versions the RENAME and its "twin" (or alias?) REN have been around to allow us to change file names:

REN or RENAME

Renames a file or files.

RENAME [drive:][path]filename1 filename2.
REN [drive:][path]filename1 filename2.

Note that you cannot specify a new drive or path for your destination file.

You can even use wildcards in filename1 (and filename2) to rename, say, all your (very) old MS-DOS (ASCII) help files from *.doc to *.txt:

REN *.doc *.txt

If I remember correctly, in the old MS-DOS days, it was possible to append characters to the file name using the command:

REN *.txt *1.txt

Try that in Windows (XP) and you'll get *.txt1.txt files.

The proper way to do this in NT is:

FOR %%A IN (*.txt) DO REN "%%~fA" "%%~nA1.*"

For each *.txt file, "%%~fA" resolves to the (doublequoted) fully qualified path, and %%~nA1 to the original file name only, with a 1 appended, and .* to leave the extension unaltered.

Undocumented REN

Justin taught me an undocumented REN feature to chop off everything from a file name after the last occurrence of a specified character:

REN testfile.txt *s

will rename testfile.txt to tes.

REN testfile.txt *t

will not change the name at all (remember: the last occurrence...?).

REN testfile.txt *st

will rename testfile.txt to test.
Does that mean chop off everything after the last occurrence of st? No it doesn't:

REN testfile.txt *sa

will rename testfile.txt to tesa, so it seems to mean chop off everything after the last s and then append an a.

Finally:

REN testfile.txt *a

will rename testfile.txt to testfile.txta.

Rename Folders

You can rename folders with the MOVE command:

MOVE d:\path\folder1 folder2

where folder2 is a folder name only (not a fully qualified path).
Make sure folder2 does not exist, neither in d:\path nor in the current folder, or the previous command will really move folder2 into folder1 (making folder2 a subfolder of folder1).

Likewise, MOVE can be used to rename and move files all in one go:

MOVE d:\path1\oldfilename1 e:\path2\newfilename2

page last uploaded: 2015-08-24, 21:09