简体   繁体   中英

Mass renaming in batchfile: skip specific files by name

I want a batch script for renaming files. It should work from the directory and check all files in the subfolder and rename the files.

forfiles -p "." -s -m . -c "cmd /c ren @file @file.mp3"

But using the .bat more then once makes it complicated. I get .mp3.mp3 files and renames my .bat to .bat.mp3 (off curse...). How can I skip files with specific file ending, for me .mp3 and .bat? I am sure there is a elegant solution.

To stop the batch file from renaming itself you could place it "up one directory" from the location of the files to be renamed. When you run the forfiles command pass the /p parameter as "./name_of_folder" and use /s for subfolders.

The forfiles command does not have an parameter for excluding files. The /m parameter will include files based on extension so if you if you passed the ext name like /m "*.zip" it will only rename zip files and it will no longer rename your batch file.

If you truly want to exclude certain files based on extension you would have to do something a little fancier possibly containing multiple batch files or goto statements. You would not use forfiles. you would run a command to output the names of files you would like and then run an action over each of those files. something like

for /f %%a in ('dir /b *') do @if "%%~xa" neq ".mp3" echo %%a

This is very simple using JREN.BAT - a hybrid JScript/batch utility that can bulk rename files via regular expressions. JREN.BAT is pure script that runs natively on any Windows machine from XP onward.

Assuming JREN.BAT is somewhere within your PATH, then the following simple one liner works directly from the command line - no batch script required (other than the JREN.BAT utility itself)

jren $ .mp3 /s /fx "*.mp3|*.bat"

Full documentation is built into the script, and can be accessed from the command line via jren /? , or jren /?|more if you want to see only one page at a time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM