|
Although REN can be used with wildcards to rename a group of files, they often do not work quite the way that is expected. To illustrate this, let us say that the current directory contains three files:
aa.txt
aa1.txt
aa2.txt
If you should wish to rename the files as "b.txt", "b1.txt", and "b2.txt", one might expect to use the command
REN aa*.txt b*.txt
but this would not work. The files would be renamed to:
ba.txt
ba1.txt
ba2.txt
The first character would be simply be changed from "a" to "b" - not what was wanted at all.
Trying to rename files with longer names is even worse. If one wished to rename the files as ccc.txt, ccc1,txt, ccc2.txt using the command
REN aa*.txt ccc*.txt
then aa.txt is renamed ccc.txt, but aa1.txt will also be named ccc.txt causing a "Duplicate file name or file in use" error.
So, what seems to be happening is...
- REN selects all files in the current/specified directory matching the first parameter (aa*.txt in this example);
- For each selected file, the first character of the name is switched to the first character in the second parameter, the second character is switched, and so on.
- In general terms, when the nth character:
| in the 1st parameter... |
and in the 2nd parameter... | then the character... |
is specified (ie. a standard filename character) | is specified | is changed |
| is a wildcard | is not changed |
| none | is removed |
| is a wildcard | is specified | is changed |
| is a wildcard | is not changed |
| none | is removed |
| none | is specified | is added |
| is a wildcard | is ignored |
- Note: what is NOT happening is a "search & replace" operation where "aa" is replaced by "b" or "ccc".
- Recognise also that the first parameter has two functions:
- To specify the group of files that will be renamed, and
- To define which characters will be subject to being changed.
Bearing in mind the above, some wildcard operation do work just fine:
- To change the ".txt" extensions of all these files to ".doc", you can use:
REN aa*.txt *.doc
and the files are renamed exactly as expected.
- Similarly, if a group of files have the same name but different extensions:
REN aa.* b.* and
REN aa.* ccc.*
will both work.
- Changing specific characters is also straightforward:
REN aa*.* x*.*
will replace the first character of all files whose names begin "aa" with an "x" - leaving the rest of the name just as it was.
- To add characters (dd) as a suffix to all file names beginning with "aa":
REN aa*.txt ???dd.txt
where the number of ?s must equal (or exceed) the longest filename in the group.
Other renaming scenarios require alternative approaches:
- To add characters as a prefix to a set of file names, use the FOR command:
FOR %v IN (aa*.txt) DO REN %v b%v
What this does is:
- Select all files meeting the specification aa*.txt (the "set");
- Cycle through each filename in the set, assigning the variable %v to that filename;
- Carry out the command following "DO".
eg. in the first cycle, the variable %v would be aa.txt, and the command would be:
REN aa.txt baa.txt
Note: to retain long filenames using FOR, it is necessary to set LFNFOR to ON.
|