Computing.Net > Forums > Windows XP > Adding a prefix to/from file names

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Adding a prefix to/from file names

Reply to Message Icon

Name: Elaphe
Date: December 3, 2005 at 09:46:33 Pacific
OS: XP SP2
CPU/Ram: Athlon64 3200 1gb
Comment:

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-*.*



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: December 3, 2005 at 10:56:24 Pacific
Reply:

Try these:

:: noDM.bat
@echo off

for %%D in (DM-*.*) do call :noDM- %%D
goto :eof

:noDM-
set Dname=%1
ren %1 %Dname:~3,8%
goto :eof
:: DONE

###
:: DM.bat
@echo off

for %%D in (*.*) do call :DM- %%D
goto :eof

:DM-
ren %1 DM-%1
goto :eof
:: DONE

You'll want to put the BATs in another directory, so they don't get renamed and confuse the issue.



If at first you don't succeed, you're about average.

M2


0

Response Number 2
Name: Elaphe
Date: December 3, 2005 at 13:58:23 Pacific
Reply:

Thanx very much for your answer. Your skills with batch files is outstanding. I've tried this and the fist bat works fine, except for the VCTF*.* files which are not renamed. The second batch doesn't work as expected. ?¿


0

Response Number 3
Name: Elaphe
Date: December 3, 2005 at 14:04:06 Pacific
Reply:

Sorry I meant the second one works (the one for renaming files). The one which doesn't work at all is the one for restoring them.


0

Response Number 4
Name: Mechanix2Go
Date: December 3, 2005 at 14:42:52 Pacific
Reply:

The noDM.bat only looks for files named DM-*.*

Are ther files of that spec in the directory?

If so, post a few of the exact names.


If at first you don't succeed, you're about average.

M2


0

Response Number 5
Name: Elaphe
Date: December 4, 2005 at 02:58:34 Pacific
Reply:

OK. The bat for renaming:

for %%D in (*.*) do call :DM- %%D
goto :eof

:DM-
ren %1 DM-%1
goto :eof

The bat for restoring:

for %%D in (DM-*.*) do call :noDM- %%D
goto :eof

:noDM-
set Dname=%1
ren %1 %Dname:~3,8%
goto :eof

And the files with which I'm testing:

AS-mapa1.txt
BR-mapaconnombremuylargo.txt
CTF-estoesunmapa.txt
CTF-mapa.txt
CTF-mapaconnombremuylargo.txt
DM-mapa1.txt
DM-mapa1connombremuylargo.txt
DM-nombre-de-un-mapa.txt
ONS-mapaconnombremuylargo.txt
VCTF-unmapa1.txt
TUT-DM.txt
TUT-BR.txt

With the bat for renaming all take the -DM prefix, except for TUT-DM.txt and TUT-BR.txt (it's correct: these files must not be renamed (I placed there just to be sure). However, VCTF-DM.txt keeps the same and no prefix is added, which is not right.

The bat for restoring create a little disaster: some files loose their extensions with this result:

ONS-mapa
DM-nombr
DM-mapa1
DM-DM-mapa1connombremuylargo.txt
DM-CTF-mapaconnombremuylargo.txt
CTF-mapa
CTF-esto etc.

Could you please check it out?

It's important that only the first 3 letters are removed with the second bat (DM-) so as to leave the files with their original names. So, DM-CTF-file.txt should revert to CTF-file.txt, and DM-DM-file should become DM-file.txt, ok?

Thank you very much and excuse my persistance.


0

Related Posts

See More



Response Number 6
Name: Mechanix2Go
Date: December 5, 2005 at 02:33:26 Pacific
Reply:

Persistence is good. It gets things done.

Try these two:

:: addDM2.bat
@echo off

for %%D in (*.*) do call :DM- %%D
goto :eof

:DM-
set workfile=%1
if /i %workfile:~0,3%==DM- goto :skip
ren %1 DM-%1
:skip
goto :eof
:: DONE
==================
:: noDM2.bat
@echo off

for %%D in (DM-*.*) do call :noDM- %%D
goto :eof

:noDM-
set Dname=%1
ren %1 %Dname:~3,80%
goto :eof
:: DONE


::


If at first you don't succeed, you're about average.

M2


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Windows XP Forum Home


Sponsored links

Ads by Google


Results for: Adding a prefix to/from file names

adding a folder to my computer www.computing.net/answers/windows-xp/adding-a-folder-to-my-computer/50931.html

batch file to change file name www.computing.net/answers/windows-xp/batch-file-to-change-file-name/178460.html

remotely adding a comp to a domain www.computing.net/answers/windows-xp/remotely-adding-a-comp-to-a-domain/144861.html