Formulating a general procedure for adding a prefix to a number of files in a single directory is made complicated by the REN-bug that allows some files to be renamed more than once if the renamed files meet the specification of the files-to-be-renamed in the REN command.
RenPrefix is a neatly constructed batchfile that sets the hidden attribute of each file in the specified group as it is renamed so that it cannot be re-renamed.
This batch file is slightly modified from the example given by Tom Lavedas in Re: Renaming multiple files (Dos7) from the newsgroup comp.os.msdos.misc dated 01/11/99.
- ::RenPrefix.bat adds any number of characters
- ::to the beginning of a filename
- ::Syntax -
- ::RENPREFIX files-to-be-renamed prefix-to-be-added
- LFNFOR On
- FOR %%v IN (%1) DO CALL %0 %1 %2 %%v GOTO:Part2
- ATTRIB -h %2%1
- LFNFOR Off
- GOTO End
- REN %3 %2%3
- ATTRIB +h %2%3
|Line ||What it does|
|1 - 4||Preamble|
Replaced by the 4th arguement of the batch file. When first called there are only two arguements so this line is ignored. When the batch recalls itself (in line 7) the 4th arguement is "GOTO:Part2" and this command is executed - ie. lines 6 to 10 are skipped. Note that the usual syntax for GOTO is GOTO Label without the colon, but this would be parsed as two arguments. The colon is accepted as a separator by GOTO but not when read as an arguement.
Long filename support for the subsequent FOR command is switched on.
|7||FOR %%v IN (%1) DO CALL %0 %1 %2 %%v GOTO:Part2|
This FOR...IN...DO construction cycles through the files specified by the "files-to-be-renamed" arguement. For each file it re-CALLs the parent batch file (%0) with four arguements: %1 and %2 are carried through from the arguements supplied initially (files-to-be-renamed and prefix-to-be-added); %3 is the current value of %v (ie. the file currently being processed by FOR); and %4 is the "GOTO:Part2" string.
What happens is that for each file specified in "files-to-be-renamed" a new instance of RenPrefix.bat is run but on reaching Line 5 control skips to Line 11, runs through the remaining commands and then ends. When all files have been processed, the first instance of the batch moves onto Line 8.
|8||ATTRIB -h %2%1|
After Line 7 has been completed, all specified files (%1) are renamed (to %2%1) and hidden. This line resets the "Hidden" attribute for all the renamed files.
Resets long filename support for FOR to Off. This line is optional and depends on whether you prefer to have LFNFOR On or Off as your default.
Causes control to skip to :End
Marks the start of the commands used by the child instances of the batch called in Line 7
|12||REN %3 %2%3|
Adds the specified prefix (%2) to the current file being processed in the FOR...IN...DO construction on Line 7 (%3 (= %v))
|13||ATTRIB +h %2%3|
Sets the hidden attribute on the renamed file. This file is now invisible to REN and so cannot be renamed again.
To add "abc" to all filenames in the current folder.
RenPrefix *.* abc