Search code examples
windowscommand-linebatch-rename

Windows 10 Command Prompt rename files overwrites file names instead of prepending


I have these files in a folder:

aaa.txt
bbb.txt
ccc.txt

I want to prepend all the filenames with 1_, so I try to write this in a Windows command prompt:

rename * 1_*

Doing so I want to get this result:

1_aaa.txt
1_bbb.txt
1_ccc.txt

But instead i get this:

1_a.txt
1_b.txt
1_c.txt

Instead of prepending it is just overwriting the names from the start. According to this (https://www.computerhope.com/renamehl.htm) article that is indeed the intended behavior.

But in this (https://www.windowscentral.com/how-rename-multiple-files-bulk-windows-10) article they show an example where they are increasing the length of the first part of the filename like this:

ren nyc_*.* newYork_*.*

So that seems to be similar to what I want to do, but when I try that exact example it does not work like that. Again, it just overwrites the first part the name without adding anything, and then I end up with nyc_(1).jpg becoming newYork_.jpg (the unique number is overwritten).

Is the second article plain wrong? How do I simply prepend something to a bunch of files with a batch line?


Solution

  • One method is to knock up a temporary batch file to do your renaming. First, dump your directory to a text file with formatting turned off:

    dir *.txt /B > list.bat
    

    Open it in Notepad and then copy that bare listing into the likes of Excel (or if you have a text editor with powerful features you can use it.) You can then create a formula, like:

    ="rename " & A1 & " 1_" & A1
    

    Which will build you a list of individual rename commands changing each file one at a time, like this:

    A         B
    aaa.txt   rename aaa.txt 1_aaa.txt
    bbb.txt   rename bbb.txt 1_bbb.txt
    ccc.txt   rename ccc.txt 1_ccc.txt
    

    Copy that new column back into notepad and save it. Run and discard your new batchfile and everything will be renamed over.

    For your nyc to NewYork, you need a bit more work...

    ="rename " & A1 & " NewYork" & MID(A1,4,99)
    

    ...will strip the left three characters from the name replace them to give you:

    rename nyc_(1).jpg NewYork_(1).jpg