I have a bunch of files with the same prefix "prefix_filename.txt". I want to remove this prefix and underscore from all filenames, how can I do this?
Asked
Active
Viewed 4,308 times
2
-
I'd normally use some tool for this, like [Renamer](http://www.den4b.com/?x=products&product=renamer), but if you don't want to use an external tool, you can find some batch code in the accepted answer to this SU [question](http://superuser.com/questions/236820/how-do-i-remove-the-same-part-of-a-file-name-for-many-files-in-windows-7) – ho1 Nov 19 '11 at 08:14
1 Answers
4
Use the for command - no batch file needed or third party tool either
for /f "tokens=1* delims=_" %a in ('dir /b /a-d') do @if "%b" NEQ "" ren "%a_%b" "%b"
(always test first)
To break it down, the for command executes the dir command in bare (/b) format and showing files only (attributes NOT directory - /a-d). The tokens are the "columns" in the output - Column 1, then Column 2 is everything NOT in column 1, excluding the delimiter. The delimiter is the underscore. Now that everything is defined, we "do" a check to ensure there is a second column (%b) and if there is, we rename the full file name to the file name without the prefix and underscore.

quanta
- 51,413
- 19
- 159
- 217

Multiverse IT
- 1,825
- 9
- 11