CMD ren command to rename a file (folder) _dos/bat

Source: Internet
Author: User
The use of Ren is simple, using the format: ren old file New file name. For example, rewrite Test.txt as abc.txt and use Ren test.txt abc.txt.

Note that the old file can use an absolute path or a relative path, but the new file name cannot use any path, only a new filename, even if the path is the current directory. For example, if you need to modify the name of the D:\test\abc.txt file as Xyz.txt, if the current path is in D:\test, then the command can be written as: Ren abc.txt xyz.txt, Ren d:\test\abc.txt Xyz.txt, however, can never be written in a format such as Ren d:\test\abc.txt d:\test\abc.txt. This bizarre rule may be that once the path is written in another directory, Ren has the "Move file + Rename file" function, which does not match its positioning.

Although Ren's usage is simple, it is powerful, thanks to Ren support * and? These two wildcard characters: * For any number of character,? represents a character. For example, to change all files with the suffix name of. doc as the suffix name, use ren *.txt *.doc can, a simple statement, instantly changed a large number of files, cmd command concise and yet powerful features in the embodiment of incisively and vividly. For example, to rename a txt file with an ABC string and only 5 characters to the beginning of the XYZ string, you can write this: ren ABC??. TXT xyz??. Txt.

What needs to be paid attention to is that, when using wildcard characters, Ren's matching rules are peculiar and too complex for beginners to understand, no longer do a detailed explanation. For more information, please refer to this post: Is this a bug in Ren? http://www.cn-dos.net/forum/viewthread.php?tid=29538. If you do need to use wildcards, we recommend to read this post, if it is difficult to understand, then the second, only use wildcard characters to do the modification of suffix name, you can not delve into these complex rules.

Ren also has a very peculiar attribute: When the file has hidden attributes or system properties, Ren will not work, need to hide the file or system attributes to remove the name can be renamed, to read-only properties of the file, Ren can be successfully renamed. This has created a lot of trouble for us in many cases: direct ren is problematic when you don't know if the file you want to rename will have hidden or system attributes, and it's even more painful if you want to keep the original file properties when renaming the file.

What to do?

If you don't mind if the file's properties are changed, then, the insurance practice is to use the Attrib-s-H command to first remove the file may exist system attributes or hidden attributes, and then ren; if you want to retain the original properties of the renamed file, it is efficient to use the%%~ai in the For statement first Extended to get the properties of the original file, after processing to save to the variable, and then use Attrib-s-H to remove the file may exist system attributes or hidden properties, Ren, and then read the property values retained in the variable, attrib restore the original file properties.

For folders, the same applies to Ren.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.