Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
199 views
in Technique[技术] by (71.8m points)

How to rename files in Windows CMD (command prompt)

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:	emp> 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:	emp> 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:	emp> 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:	emp> 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...