REN – Windows CMD Command


Notice: A non well formed numeric value encountered in /home/future4tech/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /home/future4tech/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /home/future4tech/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /home/future4tech/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /home/future4tech/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /home/future4tech/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

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...

23 Responses

  1. prednisonezer says:

    I needs to spend some time learning more or understanding more.

  2. lasixginty says:

    I am not sure where you’re getting your info, but great topic. I needs to spend some time learning more or understanding more.
    Thanks for magnificent information I was looking for this information for my mission.

  3. houston junk car says:

    What i don’t realize is in reality how you are not really much more smartly-preferred than you might be right now.
    You’re very intelligent. You recognize thus considerably when it comes to
    this topic, produced me in my view believe it from numerous varied
    angles. Its like men and women are not fascinated except it’s one thing to
    accomplish with Lady gaga! Your personal stuffs great.

    Always deal with it up!

  4. zoritoler imol says:

    F*ckin’ amazing things here. I’m very glad to see your article. Thanks a lot and i’m looking forward to contact you. Will you kindly drop me a mail?

  5. ‏asus rog gl753 says:

    I really liked your blog post.Really looking forward to read more. Keep writing.

  6. zoritoler imol says:

    Yay google is my king assisted me to find this great website ! .

  7. Jasper says:

    This website was… how do you say it? Relevant!! Finally I’ve found something which helped me.
    Kudos!

  8. Curt says:

    What i do not realize is in fact how you are no longer really much
    more neatly-appreciated than you may be right now.

    You are so intelligent. You recognize thus considerably in terms of this subject, produced me personally
    imagine it from numerous varied angles. Its like men and
    women aren’t interested unless it’s one thing to do
    with Woman gaga! Your own stuffs great. At all times handle it up!

  9. Florentina says:

    Hi there, yup this paragraph is really good and I have learned lot of things from it
    regarding blogging. thanks.

  10. Muriel says:

    Asking questions are genuinely pleasant thing if you are not understanding anything totally, except this paragraph gives
    fastidious understanding even.

  11. Ekonomia Definicja says:

    I just got up from sleep and I am already reading your blog. It means something! Really useful ideas. Thnx!

  12. www.Links.M106.COM says:

    Hello!I am following your blog for some days now. I have to say that it is very easy to check out . It is added in my favourite list and i will try to track it recurrently. Thanks for the nice posts . On top of that, i actually like your web template and the way you have structured your page . Is it possible to tell me the name of your template ? Thanks

  13. Boost SEO Links says:

    My bro saved this internet site for me and I have been reading through it for the past couple hrs. This is really going to assist me and my classmates for our class project. By the way, I enjoy the way you write.

  14. Add Link says:

    Great stuff.Id like to suggest taking a look at such as something like cheeseburger. What exactly are you looking for though?

  15. 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.

  16. Pianino Informacje says:

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

  17. 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.

  18. 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..

  19. Stories Links says:

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

  20. 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.

  21. frolep rotrem says:

    Would love to always get updated great web site! .

  22. 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.

  23. 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.

Leave a Reply

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