How to omit characters at the beginning of a filename while renaming it in Windows cmd?
Solution 1:
I faced a similar problem like this few months ago. It turned out removing characters at the beginning of file name is a little tricky using DOS. I came across this site which had a good solution for this.
All you need to do is cd
into the directory containing the files and execute these two commands.
REN *.* " *.*"
FOR %v IN (*.*) DO REN "%v" %v
This should replace the first character in all the file names.
The idea is to replace the number of unwanted characters with spaces using the first REN
command then drop this spaces using the FOR
loop and REN
command.
Solution 2:
Forget about complicated scripts for this.
rename
is a very old and never properly completed command.
If you do not use it properly, the result might surprise you.
For example, to remove a prefix abcd
from abcd1.txt
, abcd2.txt
, abcd3.txt
, etc. in order to get 1.txt
, 2.txt
, 3.txt
, simply type
rename "abcd*.txt" "////*.txt"
You need the same number of /
as the number of initial characters you would like to remove.
Do use double quotes for both arguments.
I copied the above from someone else on another thread.
Note that this handles filenames with space(s) correctly.
Solution 3:
I'm going to expand on Andrew Ion's post. It is elegantly simple, but it doesn't completely explain the flexibility of the command.
I frequently name certain types of files with a numerical prefix (and surround the prefix with brackets to make it easy to read the number), like these three:
[#001] Aardvark.txt
[#002] Badger.txt
[#003] Civet.txt, etc.
If I want to strip the prefix (remove the first 7 characters, including the space), I can type the following:
ren "[#???] *.txt" "///////*.txt"
This will only rename files that start with [# , have a ] and a space as the 6th and 7th characters and end with a .txt. This command will rename the above files to Aardvark.txt, Badger.txt, Civet.txt, etc.
Realize that if your filenames after the numerical prefix are not unique (for example [#001] Aardvark.txt and [#002] Aardvark.txt), then the command will only rename one of the files, skip renaming the other, and respond with the error: "A duplicate file name exists, or the file cannot be found."
So make sure each file has a unique name without the prefix before you run the command.
Also, while most (good) renaming programs give you a preview of what the new file names will look like before you apply the change, using this Rename command will change the file names as soon as you press the Enter key. So I always recommend that you make copies of your files before you rename them. Then if the renaming works as you expected, you can delete the files with the original names. Otherwise, you may end up with massively wrong file names, and no way to return them to their original names (unless you have backups). YOU HAVE BEEN WARNED! :-)