REN – Windows CMD Command

Rename a file or files.

Both the SourceMask and TargetMask can contain * and/or ? wildcards.
As described below, the behavior of these wildcards is slightly different for a source or a target mask.

REN can also be used to rename directories, but this behaviour is undocumented.
A simple REN folderA folderB will work, but wildcard operations are not supported for directories, if you issue a wildcard rename that matches both files and directories, the file(s) will be renamed and the folders will be ignored. If you issue a wildcard rename that matches only directories then a syntax error will be thrown.

You cannot specify a different drive or path for TargetMask – use the MOVE command instead.

RENAME is a synonym for REN

Rename files within subdirectories

By default REN with a wildcard will only rename the files in a single folder, to recurse down into sub folders use a FOR /R command, after first changing to the top level directory.

e.g. A batch file to rename all .LOG files to .TXT in the ‘C:\demo\’ folder and all sub-folders:

CD C:\demo\
For /R %%G in (*.LOG) do Echo REN “%%G” “%%~dpnG.TXT”
Remove the echo to run this for real

SourceMask

The sourceMask works as a filter to determine which files are renamed. The wildcards work here the same as with any other command that filters file names.

? Matches any 0 or 1 character except . This wildcard is greedy – it always consumes the next character if it is not a . However it will match nothing without failure if at name end or if the next character is a .

* Matches any 0 or more characters including . (with one exception below). This wildcard is not greedy. It will match as little or as much as is needed to enable subsequent characters to match.

All non-wildcard characters must match themselves, with a few special case exceptions.

. Matches itself or it can match the end of name (nothing) if no more characters remain. (Note – a valid Windows name cannot end with .)

{space} Matches itself or it can match the end of name (nothing) if no more characters remain. (Note – a valid Windows name cannot end with {space})

*. at the end, Matches any 0 or more characters except . The terminating . can actually be any combination of . and {space} as long as the very last character in the mask is . this is the one and only exception where * does not simply match any set of characters.

The above rules are not that complex. But there is one more very important rule that makes the situation confusing: The SourceMask is compared against both the long name and the short 8.3 name (if it exists). This last rule can make interpretation of the results very tricky, because it is not always obvious when the mask is matching via the short name.

It is possible to use FSUTIL to disable the generation of short 8.3 names on NTFS volumes, at which point interpretation of file mask results is much more straight forward. Any short names that were generated before disabling short names will remain. New installations of Windows server now default to disabling 8.3 names.

TargetMask

The TargetMask specifies the new name. It is always applied to the full long name; The TargetMask is never applied to the short 8.3 name, even if the SourceMask matched the short 8.3 name.

The presence or absence of wildcards in the SourceMask has no impact on how wildcards are processed in the TargetMask.

In the following discussion c represents any character that is not *, ?, or .

The TargetMask is processed against the source name strictly from left to right with no back-tracking.

c Advances the position within the source name as long as the next character is not . and appends c to the target name. (Replaces the character that was in source with c, but never replaces .)

? Matches the next character from the source long name and appends it to the target name as long as the next character is not . If the next character is . or if at the end of the source name then no character is added to the result and the current position within the source name is unchanged.

* At end of sourceMask – Appends all remaining characters from source to the target. If already at the end of source, then does nothing.

*c Matches all source characters from current position through the last occurance of c (case sensitive greedy match) and appends the matched set of characters to the target name. If c is not found, then all remaining characters from source are appended, followed by c This is the only situation I am aware of where Windows file pattern matching is case sensitive.

*. Matches all source characters from current position through the last occurance of . (greedy match) and appends the matched set of characters to the target name. If . is not found, then all remaining characters from source are appended, followed by .

*? Appends all remaining characters from source to the target. Any additional characters after the *? in sourceMask will be appended to target. If already at end of source then does nothing.

. without * in front – Advances the position in source through the first occurance of . without copying any characters, and appends . to the target name. If . is not found in the source, then advances to the end of source and appends . to the target name.

After the TargetMask has been exhausted, any trailing . and {space} are trimmed off the end of the resulting target name because Windows file names cannot end with . or {space}

It appears these same rules also work for the target name of the COPY commmand.

8.3 Filename bug – a single command can rename the same file twice!

When 8.3 filenames exist then the SourceMask will first look for a match against the long file name, and then against the short file name. If the result of the first rename operation still matches the SourceMask then the same file can be renamed twice. For example:

If 8.3 name generation is enabled, the SourceMask matches the original long name, the initial rename generates a short name that still matches SourceMask and sorts later in the alphabet, then this bug might be triggered.

If 8.3 name generation is disabled then RENAME always gives the expected result.

Errorlevels

If the File(s) were successfully renamed %ERRORLEVEL% = 0
If the File was not found, could not be renamed or bad parameters %ERRORLEVEL% = 1

Credits:
Dave Benham – List of How does the Windows RENAME command interpret wildcards? from SuperUser Dec 2012

REN is an internal command.

You may also like...

9 Responses

  1. WWW.XMC.PL says:

    This is some trustworthy material. It took me a while to unearth this web page but it was worth the time. I noticed this post was hidden in yahoo and not the number one spot. This web publication has a ton of helpful stuff and it doesnt deserve to be burried in the searches like that. By the way Im going to add this site to my list of favorites.

  2. Pianino Strojenie says:

    Im a huge fan already, man. Youve done a brilliant job making sure that people understand where youre coming from. And let me tell you, I get it. great stuff and I cant wait to check out more of your blogs. What youve got to say is important and needs to be check out.

  3. frolep rotrem says:

    Would love to always get updated great web site! .

  4. www.xmc.pl says:

    Wonderful job right here. I seriously enjoyed what you had to say. Keep going because you undoubtedly bring a new voice to this subject. Not many people would say what youve said and still make it interesting. Properly, at least Im interested. Cant wait to see far more of this from you.

  5. Stories Links says:

    Thought I would comment and say great theme, did you make it yourself? Looks great!

  6. Pianino CD says:

    Im extremely impressed along with your writing abilities and also with the structure for your blog. Is that this a paid theme or did you customize it yourself? Anyway keep up the nice high quality writing, it’s rare to look a nice blog like this one these days..

  7. Loan Center says:

    Awesome post! I’ve been trying to come up with a good internet business idea for a while. Instead, I have come up with a great non-internet business idea, which kind of sucks in a way because this idea requires a lot of money, as opposed to a full internet-based business that might not require that much to get it started. I’m not giving up though. I recently started a blog where I plan to document my steps for starting my company. I figured it would keep me motivated and force me to keep at it. I also hope it will motivate the readers to keep trying to accomplish their goals. By the way, I love your site. I’ll be adding it to my list of interesting blogs.

  8. Pianino Informacje says:

    Is it alright to post some of this on my website if I include a reference to this page?

  9. Podatki Portal says:

    It is really rare to discover a professional in whom you will surely have some trust. In the world in the present day, nobody truly cares about showing others the solution in this subjecttopic. How fortuitous I am to have actually found a wonderful web site as this. It is really people like you exactly who make a genuine difference in this world through the thoughts they discuss.

Leave a Reply

Your email address will not be published. Required fields are marked *