DOS rename file (folder) Ren

Time:2021-3-2

The usage of Ren is very simple, and its format is: Ren old file new file name. For example, put test.txt Rewrite as abc.txt , using Ren test.txt abc.txt That’s it.

It should be noted that the old file can use either absolute path or relative path, but the new file name cannot use any path, it can only be a new file name, even if the path is the current directory. For example, you need to modify D: test\ abc.txt The name of this file is xyz.txt If the current path is in D:: test, the command can be written as: Ren abc.txt xyz.txt 、ren d:\test\ abc.txt xyz.txt However, it must not be written as rend:: test\ abc.txt d:\test\ abc.txt This is the format. The reason for this strange rule may be that once the path is written into another directory, Ren has the function of “move file + rename file“, which is inconsistent with its location.

Although the usage of Ren is very simple, its function is very powerful. This is mainly due to Ren’s support for * and? Wildcards: * for any number of characters and? For one character. For example, if you want to change all files with. TXT as the suffix to. Doc as the suffix, you can use Ren *. TXT *. Doc. A simple statement will change a large number of files in an instant. The simple and powerful features of CMD command are fully reflected here. For another example, if you want to rename a 5-character TXT file that starts with an ABC string to start with a XYZ string, you can write this: Ren ABC??. TXT XYZ??. TXT.

What needs to be paid great attention to is that when Ren uses wildcards, its matching rules are very strange and too complex, which is not suitable for novices to understand. I will not explain them in detail here. For details, please refer to this post: is this a Ren bug? http://www.cn-dos.net/forum/viewthread.php?tid=29538 . If you really need to use wildcards, I suggest you read this post well; if it’s really difficult to understand, you should come back to the second place, and only use wildcards to modify the suffix, so you don’t need to go into these complex rules.

Ren also has a very strange attribute: when a file has hidden attributes or system attributes, Ren will not work. You need to remove the hidden or system attributes of the file before you can rename it. For files with read-only attributes, Ren can successfully rename it. This often causes us a lot of trouble: when you don’t know whether the file to be renamed has hidden or system attributes, it’s very difficult to directly re; if you want to keep the original file attributes when renaming a file, it’s even more painful.

What to do?

If you don’t mind whether the attributes of the file have been changed, the safe way is to use the attrib – S – H command to remove the system attributes or hidden attributes that may exist in the file, and then re it. If the renamed file is required to maintain the original attributes, the efficient way is to use% ~ AI in the for statement first Expand to get the attributes of the original file, save them to the variable after processing, and then use attrib – S – h to remove the system attributes or hidden attributes that may exist in the file. After Ren, read the attribute values retained in the variable, and use attrib to restore the original file attributes.

For folders, Ren also works.