8

I would like to know how to omit the first character of a filename while renaming the file in Windows cmd.

In my case I have bunch of files like:

#test1.txt
#test2.txt
#test3.txt
#test4.txt
#test5.txt

I would like to rename all the files in cmd prompt like

test1.txt
test2.txt
test3.txt
test4.txt
test5.txt

Files are in c:\myfiles\.

6
  • Have you considered using powershell to do so ? It's a lot easier !
    – Ob1lan
    Commented Jun 25, 2015 at 6:02
  • 2
    By "DOS" you mean the command line (cmd.exe) in Microsoft Windows? (MS-DOS - Microsoft Disk Operating System is an old, simple operating system which is not in general use for about 20 years.) Commented Jun 25, 2015 at 7:28
  • 1
    If you are not familiar with command, there is another option to rename files with GUI software like ckrename
    – Bilo
    Commented Jun 25, 2015 at 8:04
  • 3
    OP specifically said DOS. Let's get clarification from the OP before assuming that is not what was meant.
    – user
    Commented Jun 25, 2015 at 9:09
  • 2
    I'd bet the OP isn't using MS-DOS (and probably never has either).
    – Karan
    Commented Jun 25, 2015 at 13:39

3 Answers 3

6

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.

8
  • Did you try this? It's been a while since I did anything with DOS, but as I recall, one of the things it didn't have was command parameter quoting. Windows 95 probably did because of its LFN support, but I don't think any earlier MS-DOS command interpreter version had it. Third-party command interpreters (such as 4DOS) might have supported something like this, though.
    – user
    Commented Jun 25, 2015 at 8:30
  • @MichaelKjörling : Yes I did tried it before posting, once on my Windows 95 virtual setup. About that LFN support yes is supported from Win 95 and that is why this worked. But the site from where I got this stated that LFNFOR On should be used before running the for construct, it must be for the older versions. Win 95 is the oldest I got, so I can't say any thing about older versions.
    – Ayan
    Commented Jun 25, 2015 at 8:42
  • I can't say for sure (I don't have access to a DOS VM right now) but I have a definite feeling that while what you are proposing might work on Windows 95, it won't work on DOS (6.22 downwards). I might be able to provision an actual MS-DOS VM tonight and try this out on the real software.
    – user
    Commented Jun 25, 2015 at 8:54
  • @MichaelKjörling : Yeah chances are there it might not work on the older versions. Since you would be checking it out on the actual software can you just take a look at that site that I have mentioned and check out that LFNFOR option. Please let me know, I am also a bit curious to find out if this works on older versions or not.
    – Ayan
    Commented Jun 25, 2015 at 9:04
  • 1
    When doing this in a batch file, remember to use 2 percent signs FOR %%v IN (*.*) DO REN "%%v" %%v See stackoverflow.com/questions/11249339/…
    – Keith
    Commented Feb 22, 2017 at 19:32
8

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.

2
  • Thanks this worked like a charm, i removed the first character (which was a 0) of a pdf file, whith this command: rename "0*.pdf" "/*.pdf"
    – cetipabo
    Commented Nov 29, 2019 at 7:25
  • BRAVO BRAVO! finalllllllllllllllllllly u know how long it took me to find an answer to this question
    – oldboy
    Commented Jul 31, 2021 at 7:44
1

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! :-)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .