1.

Solve : Adding/removing a prefix to/from file names?

Answer»

I'd like automate a bulk RENAMING task with just one click or with a command. I suppose I need command line utility or a batch file that allows me to do this. The changes in all couples are the same: adding a prefix "DM-" to the files NAMES in one case, and removing the prefix in the other so as to restore the original names. Could you help me out with this? Thanx.

ADD PREFIX DM- TO CTF-*.* = DM-CTF-*.*
REMOVE PREFIX DM- FROM DM-CTF-*.* = CTF-*.*

ADD PREFIX DM- TO BR-*.* = DM-BR-*.*
REMOVE PREFIX DM- FROM DM-BR-*.* = BR-*.*

ADD PREFIX DM- TO AS-*.* = DM-AS-*.*
REMOVE PREFIX DM- FROM DM-AS-*.* = AS-*.*

ADD PREFIX DM- TO DOM-*.* = DM-DOM-*.*
REMOVE PREFIX DM- FROM DM-DOM-*.* = DOM-*.*

ADD PREFIX DM- TO ONS-*.* = DM-ONS-*.*
REMOVE PREFIX DM- FROM DM-ONS-*.* = ONS-*.*

ADD PREFIX DM- TO VCTF-*.* = DM-VCTF-*.*
REMOVE PREFIX DM- FROM DM-VCTF-*.* = VCTF-*.*Renaming files with wildcards can be tricky. You can end up with mangled files names that are imposssible to decipher.

There seems to be a pattern with your file names so this may not be so bad.

Quote

ADD PREFIX DM- TO CTF-*.* = DM-CTF-*.*

Code: [Select]
for /f "tokens=1-2 delims=." %%a in ('dir /b CTF-*.*') do ren %%a.%%b DM-%%a.%%b


Quote
REMOVE PREFIX DM- FROM DM-CTF-*.* = CTF-*.*

Code: [Select]
for /f "tokens=1-4 delims=-." %%a in ('dir /b DM-CTF-*.*') do ren %%a-%%b-%%c.%%d %%b-%%c.%%d


You should be able to work out the rest. You may want to consider making a test run in a test directory before you blast away at real files.

Good luck. Thanks for the information. It works as you say, however, I have batch of files with long names and space between. Can you please advice how I can add prefix to multiple files in one go?
For example:

"run dmc - walk this way.mp3" rename to: "2013 run dmc - walk this way.mp3"
"NWA - Hello.mp3" rename to: "2013 NWA - Hello.mp3"

Regards,
FSamie (a rookie)for /f "delims=" %%A in ('dir /b /a:d') do rename "%%A" "2013 %%A"

Is how I would do it, but I'm sure there are multiple ways that work.I think you meant /a-dTo support long filenames with spaces etc you just need the QUOTES in the ren portions.

Quote from: SIDEWINDER on December 03, 2005, 04:10:03 PM
Code: [Select]for /f "tokens=1-2 delims=." %%a in ('dir /b CTF-*.*') do ren "%%a.%%b" "DM-%%a.%%b"
Code: [Select]for /f "tokens=1-4 delims=-." %%a in ('dir /b DM-CTF-*.*') do ren "%%a-%%b-%%c.%%d" "%%b-%%c.%%d"


But it can be simplified too:

Code: [Select]for /f "delims=" %%a in ('dir /b CTF-*.*') do ren "%%a" "DM-%%a"
Code: [Select]for /f "tokens=1,* delims=-" %%a in ('dir /b DM-CTF-*.*') do ren "%%a-%%b" "%%b"
Quote from: Squashman on January 04, 2014, 08:04:18 PM
I think you meant /a-d
Yup, it's good to have people willing to look over your work for these kind of mistakes lemonilla and all you gurus here
Thank you very much, you people are truly good.

foxydrive
I tried your code to remove the extension, but it didn't work. Can you please take my example and remove the 2014 from it?

2014 jz - 99 problems.mp3
2014 mj - walk that way.mp3
Code: [Select][quote author=FSamie link=topic=15956.msg898900#msg898900 date=1389110499]
2014 jz - 99 problems.mp3
2014 mj - walk that way.mp3
[/quote]

Because 2014 is ALWAYS in the beginning, and we know that it is 4 characters long, we can set the file name equal to a string, and then remove the first 5 characters (to include the space as well).

setlocal EnableDelayedExpansion
for /f "delims=" %%A in ('dir /b /a:-d') do (
set a=%%A
rename "%%A" "!a:~5!"
)
[/code]

I would recommend testing this on dummy files before you give it a go.
I created a bat file with the following:

for /f "delims=" %%A in ('dir /b /a:-d') do (set a=%%A rename "%%A" "!a:~5!")

but it didn't work.
Give this a run. It will echo the commands to the console so remove echo if you are happy with it.

Code: [Select]@echo off
for /f "tokens-1,* delims= " %%a in (' dir *.mp3 /b /a-d ') do echo ren "%%a %%b" "%%b"
pause
foxi
I tried
for /f "tokens-1,* delims= " %%a in (' dir *.mp3 /b /a-d ') do echo ren "%%a %%b" "%%b"
it didn't work.
Please have in mind I'm an absolute rookie who just created a .bat file with your codes and tried to remove the "2014 " prefix from my files in a specific folder.
Thanks in advanceQuote from: FSamie on January 07, 2014, 03:41:33 PM
I created a bat file with the following:

for /f "delims=" %%A in ('dir /b /a:-d') do (set a=%%A rename "%%A" "!a:~5!")

but it didn't work.

You forgot to enable Delayed Expansion

Code: [Select]setlocal enableDelayedExpansion
for /f "delims=" %%A in ('dir /b /a:-d') do (set a=%%A & rename "%%A" "!a:~5!")

(and your syntax was wrong after the 'do'. You had forgotten to separate 'set' and 'rename' with & or &&)Lemonilla
I tried your codes and they all work fine and summarised here


To add, for example "2014 " to all files:
for /f "delims=" %%A in ('dir /b /a-d') do rename "%%A" "2014 %%A"

To remove "2014 " from all files:

setlocal enableDelayedExpansion
for /f "delims=" %%A in ('dir /b /a:-d') do (set a=%%A & rename "%%A" "!a:~5!")

Again, many thanks
FSamie (beginner)Quote from: FSamie on January 08, 2014, 07:25:56 AM
foxi
I tried
for /f "tokens-1,* delims= " %%a in (' dir *.mp3 /b /a-d ') do echo ren "%%a %%b" "%%b"
it didn't work.

It always helps to mention the error messages on the console - for future posts anyway.

In this case I had hit - instead of = when I typed the code. Oops.

Try this:

Code: [Select]@echo off
for /f "tokens=1,* delims= " %%a in (' dir *.mp3 /b /a-d ') do echo ren "%%a %%b" "%%b"
pause


Discussion

No Comment Found