How to rename files in Windows CMD (command prompt)
Asked Answered
U

3

18

Renaming files in Windows explorer is easy but when you need to rename many files it can become quite tedious. A command prompt (terminal) makes it easier.

Unthrone answered 13/8, 2020 at 13:46 Comment(2)
The usage of shareware file manager Total Commander makes it very easy to rename lots of files (or folders) in a folder or a folder tree or spread over all drives very easy without any coding skills and being nevertheless extremely powerful with its built-in awesome Multi-rename tool.Sold
@Sold Thanks for the suggestion, that sounds like a nice tool. Personally, i prefer using command line (and i dont like installing 3rd party tools all the time :)Unthrone
S
23

In order to rename any file you need to cd into your folder directory and then just type one of these: ex,

  • Desktop> ren "My file" "Your file"
  • Desktop> rename "hello.txt" "goodbye.txt"
  • Desktop> ren "why.txt" "because.docx"

and click the Enter button

Shackle answered 29/3, 2021 at 17:34 Comment(0)
U
10

Renaming 1 file in cmd is very easy:
In this example we have a sample1.txt and we want to change its name to sample2.txt:

    in command prompt type:
    c:\temp> ren sample1.txt sample2.txt [enter]

Let's say the filename is sample1-some-unwanted-text-1234.txt and we want to change it to sample1.txt:

    in command prompt, type:
    c:\temp> ren sample1-some-unwanted-text-1234.txt sample1.txt

Renaming 1 file by replacing multiple unwanted characters using a star:
Let's say the filename is sample1-some-unwanted-text-1234.txt and we want to change it to sample1.txt without having to type the whole filename:

    in command prompt:
    c:\temp> ren sample1*.txt sample1.txt

This * basically means any characters inbetween sample1 and .txt will be replaced.

Renaming multiple files with similar names
If you want to rename multiple files, i.e. sample1 2020-08-01.txt, sample2 2020-08-05.txt, sample3 2020-08-10.txt,sample4 2020-08-13.txt, you want to keep the first 7 characters you want to get rid of the dates:

    in command prompt:
    c:\temp> ren sample?*.txt sample?.txt

In this example, you want to keep the word sample and the number X (where X can be any number or character). Using a ? will leave the number in place and * instructs the rename-command to replace any characters in between sampleX and .txt

Warning: It happens very quickly that a command prompt rename operation renames too many files and you can't undo it. So, when renaming multiple files it is advisable to make a copy of all the files you want to rename, put them in a temp folder, then run your rename commands in the temp folder, and when you're certain that it works, go back and rename the original files.

Unthrone answered 13/8, 2020 at 13:46 Comment(4)
How can this be linked to a question about renaming files with spaces in the filename? You have not used the correct syntax, which is REN "[drive:][path]filename1" "filename2", you should use doublequotes at any time where you cannot guarantee that [path]filename1 and/or filename2 does not contain a space, or any other poisonous character. For robustness/safety, always enclose paths/filenames within doublequotes.Debility
ok i removed the link to the other question. yeah i agree, using quotes is a good idea but you dont need to specify the [drive][path] if you want to simply rename a file in the current folder.Unthrone
ren command can handle very simple cases. Better throw cmd away and use powershell which allows you to do arbitrary renaming easilyJamesy
@Jamesy i totally agree, powershell is much more powerful, but I sometimes use cmd for simple stuff and i think many people still use cmd so i hope this post will help somebodyUnthrone
L
-1
for %f in (*) do (
    if %~z1 gtr 0 (
        set "content="
        for /f "delims=" %a in (%f) do set "content=!content!%a"
        set "content=!content:oldword=newword!"
        echo !content! > %f
    )
)
Latia answered 27/9, 2023 at 10:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.