6

I need to remove the first character (which is always "_") from all filenames in a folder.

I'm currently using this command: ren _*.txt *.txt But it doesn't work.

However, if I run ren _*.txt A*.txt it perfectly works, but it is not what I want.

27

again, try powershell ;)

Run this in your desired directory:

get-childitem *.txt | rename-item -newname { [string]($_.name).substring(1) }

Explanation:
- get-childitem *.txt collects all *.txt-files in the actual directory.
- rename-item -newname renames the piped results from the get-childitem command with the string that is generated in {}
- [string]($_.name).substring(1) takes the filename starting after the first character

| improve this answer | |
  • hey, it says gci is not recognized as an internal or external command – aneuryzm Apr 20 '11 at 12:30
  • 2
    hm... this should work...however. gci is per default an alias for Get-Childitem - take that instead. Did you run powershell? It doesn't work if you tried that in cmd.exe (but you can start powershell.exe from within cmd) – wullxz Apr 20 '11 at 12:32
  • all right, now it works. I still have a small issue, the first character is actually a ".", so I have 2 initial characters to remove "._filename.jpg" and not "_filename.jpg". Do you how to deal with the dots ? – aneuryzm Apr 20 '11 at 12:40
  • 1
    if you want to remove the first 2 characters, just replace the (1) after 'substring' with (2). In powershell, the whole filename is treated as a string(-object), points included. – wullxz Apr 20 '11 at 12:43
  • If you want to replace any part in the middle of the name (which is the same in every name), you can use replace instead of substring like so: get-childitem *.txt | rename-item -newname { [string]($_.name).replace("old string","new string") } – LPrc Mar 23 '17 at 12:33
7

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 use

rename "abcd*.txt" "////*.txt"

You need the same number of / as the number of initial characters you would like to remove.  So, for your specific situation, you would use

ren "_*.txt" "/*.txt"

Do use double quotes for both arguments.

| improve this answer | |
2

If you want to avoid having to write anything, try the Bulk Rename Utility. The interface can be quite scary at first but just have a play around with it and you'll see it's not actually hard to use at all.

| improve this answer | |
1

Just if anyone needs in future: I use 1-4 rename. It is fast, does not have to be installed. When you run it hit F2 for advanced mode and you can tell it to kill the first 4 characters. Below is the link to the program.

Download Link: http://www.1-4a.com/rename/

| improve this answer | |
1

in 2020 I had the same bulk rename problem: Remove fixed string of random characters for a lot of names.

Used this code in the Command Promt:

Rename "?????*.*" "*/////*.*"

(Remove any 5 characters) (You have tu use ")

Work perfet, thanks guys from de past.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.