Computing.Net > Forums > Programming > Rename Multiple Files

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.

Rename Multiple Files

Reply to Message Icon

Name: Vin
Date: January 27, 2006 at 09:51:05 Pacific
OS: MS
CPU/Ram: Intel
Comment:

Hi there,

I'm totally new to writing batch files and I hope someone can help me.

I use Windows XP or Windows 2000 depending on work or home but need to apply batch file to both systems.
I have over 1000 files to rename which all exist within sub-directories of a main directory. Only files with specific characters in the name need to be replaced with another set of specific characters.
For the example below, all files within Test folder with b22 in filename need to be replaced with b23:-

C:\Test\Top\v_b22_f.txt -> v_b23_f.txt
C:\Test\Middle\v_b22_m.txt -> v_b23_m.txt
C:\Test\Middle\extra\xb22m.py -> xb23m.py
C:\Test\Bottom\v_b22_r.html -> v_b23_r.html

b22 and b23 could for another example directory, be c44 and d31 respectively so need dos batch files to allow user to input this. Filenames may have various lengths and various subscripts.

Can this be done in a dos batch file?

Any help would be appreciated,

Thanks,

Vin



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: January 27, 2006 at 21:43:30 Pacific
Reply:

Is it always an "increment by 1"?


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

M2


0

Response Number 2
Name: Vin
Date: January 28, 2006 at 02:35:55 Pacific
Reply:

Hi Mechanix2Go,

No, it's not always an increment by one. The letter and number that is consistent throughout all subdirectories can be completely replaced by another letter and number combination.
i.e.
C:\Test\Top\v_b22_f.txt -> v_b23_f.txt
C:\Test\Middle\v_b22_m.txt -> v_b23_m.txt
C:\Test\Middle\extra\xb22m.py -> xb23m.py
C:\Test\Bottom\v_b22_r.html -> v_b23_r.html

C:\New\Top\v_b22_f.txt -> v_c101_f.txt
C:\New\Middle\v_b22_m.txt -> v_c101_m.txt
C:\New\Middle\extra\xb22m.py -> xc101m.py
C:\New\Bottom\v_b22_r.html -> v_c101_r.html

Filenames in subdirectories of TEST top directory with b22 need to be renamed to b23. But in NEW top directory, b22 is to be renamed to c105.

Hope this helps,

Vin


0

Response Number 3
Name: Mechanix2Go
Date: January 28, 2006 at 02:54:43 Pacific
Reply:

Hi Vin,

Yeah that helps. Life gets better.

Will the names be consistent vis-a-vis the directory they are in, or will the batch need to figure it out?

I'm going out in about 90 min; maybe we can solve it before I go.



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

M2


0

Response Number 4
Name: Vin
Date: January 28, 2006 at 03:29:09 Pacific
Reply:

Hi Mechanix2Go,

No it's not always an increment by one. The letter and number that is consistent throughout all subdirectories can be completely replaced by another letter and number combination.
i.e.
C:\Test\Top\v_b22_f.txt -> v_b23_f.txt
C:\Test\Middle\v_b22_m.txt -> v_b23_m.txt
C:\Test\Middle\extra\xb22m.py -> xb23m.py
C:\Test\Bottom\v_b22_r.html -> v_b23_r.html

C:\New\Top\v_b22_f.txt -> v_c101_f.txt
C:\New\Middle\v_b22_m.txt -> v_c101_m.txt
C:\New\Middle\extra\xb22m.py -> xc101m.py
C:\New\Bottom\v_b22_r.html -> v_c101_r.html

Filenames in subdirectories in TEST top directory with b22 need to be renamed to b23. But in NEW top directory, b22 is to be renamed to c105.

Hope this helps,

Vin


0

Response Number 5
Name: Mechanix2Go
Date: January 28, 2006 at 04:00:13 Pacific
Reply:

"But in NEW top directory, b22 is to be renamed to c105"

I hope that 105 is a typo and should be 101.

In this block:

C:\New\Top\v_b22_f.txt -> v_c101_f.txt
C:\New\Middle\v_b22_m.txt -> v_c101_m.txt
C:\New\Middle\extra\xb22m.py -> xc101m.py
C:\New\Bottom\v_b22_r.html -> v_c101_r.html

Notice that the extensions do not change. Is this always the case.

From what you wrote, I think I understand that v_b22_f will always get renamed to v_c101_f and keep it's extension. Is that right?

Will it alwyas be working with filenames like:

v_b22_f.txt

or will it encounter names like:

a-b558z-w.txt



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

M2


0

Related Posts

See More



Response Number 6
Name: Vin
Date: January 28, 2006 at 08:16:40 Pacific
Reply:

Hello again,

Thanks for your prompt response.

Sorry there was a typo on my part, 105 should have been 101 - sorry about that!

File extensions will never change, just the actual name itself. And yes you are correct with v_b22_f on one occasion will get renamed to v_c101_f and keep it's extension.

Unfortunately, I may encounter names like a-b558z-w.txt in the future so a batch that can cover these as well would be good.

Thanks for your efforts so far.

Vin


0

Response Number 7
Name: Mechanix2Go
Date: January 28, 2006 at 12:25:50 Pacific
Reply:

Hi Vin,

Do:

dir /s /b in the relevant directories and ZIP it and send it to me.

BUT

rename the ZIP to extension .ZI

I have bloked all ZIPs because of the spam crap.


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

M2


0

Response Number 8
Name: Mechanix2Go
Date: January 29, 2006 at 23:52:54 Pacific
Reply:

Hi Vin,

Got your ZIP.

I guess I don't 'get it'.

I don't see a pattern to generalize the renaming.

Sorry


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

M2


0

Response Number 9
Name: Vin
Date: January 30, 2006 at 00:30:47 Pacific
Reply:

Hi M2,

Thanks for your response.
Not sure why you don't 'get it' but I'll try to explain again if it helps....

The batch file needs to search all files within all the subdirectories of a folder with a specific name i.e. b22 and replace with new name of 3 or 4 characters such as b01 or c124. The batch file would need the user to input the original subset name of filename i.e. b22 and the destined new subset name of filename i.e. c124.
There isn't really a pattern it's a wildcard search for all files with specific subset name i.e. b22 in them to be replaced with something else defined by the user i.e. c124

Sorry if this isn't clear - it's hard to explain it clearly.

Don't worry if this isn't achievable - I'm searching the web for free download software which can do renaming throughout subdirectories.

Thanks for your efforts again.


0

Response Number 10
Name: Mechanix2Go
Date: January 30, 2006 at 01:41:32 Pacific
Reply:

Hi Vin,

The fog is beginning to lift.

In the BAT below look at the line:

echo %old% %new%

I haven't altered that line yet because I wanted to get back to you and see if we're on the same sheet of music.

I'm confident that if that line was changed to:

ren %old% %new%

It would do the trick. For now, try it in the TOPDIR from your ZIP and you'll see that it spits out the old and new names.

AND you will need:

Change


::== Vrename.bat
@echo off

if %2'==' goto :syntax

:main

set OLDstr=%1
set NEWstr=%2
> chg.bat echo change.com name.# "%OLDstr%" "%NEWstr%"
dir/s/b |find /i "%1">workLIST
for /f "tokens=*" %%F in (workLIST) do call :sub1 %%F

goto :eof

:sub1

set old=%1
echo %~nx1>name.#
call chg>nul
set /p new=<name.#
echo %old% %new%
goto :eof

:syntax

echo you need two parameters, string-in-filename AND replacement-string

:eof
:: DONE


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

M2


0

Response Number 11
Name: Vin
Date: February 1, 2006 at 14:21:06 Pacific
Reply:

Hi M2,

Sorry for the late response, been a little busy here to be able to assess your BAT file. But I've just tried it and it works perfectly!!! Thanks so much for this - it's gonna be very very useful for my work.

One query: Could the list of files that are to be changed and their resulting changed filename outputted on the DOS prompt window after the operation either as shown below or as two separate lists of original names and list of changed names?

i.e.

D:\TopDir>vrename b02 c101

Files changed are:-
D:\TopDir\x150_coupe_b02_dampers.txt
D:\TopDir\x150_coupe_c101_dampers.txt

D:\TopDir\Up\x150_b02_dampers.txt
D:\TopDir\Up\x150_c101_dampers.txt

D:\TopDir\Up\x150_coupe_b02_dampers.txt
D:\TopDir\Up\x150_coupe_c101_dampers.txt

D:\TopDir\Up\New\x150dampers_b02.txt
D:\TopDir\Up\New\x150dampers_c101.txt

D:\TopDir\Up\New\Scrap\x150dampers_b02.txt
D:\TopDir\Up\New\Scrap\x150dampers_c101.txt

D:\TopDir\Up\New\Scrap\x150_b02.txt
D:\TopDir\Up\New\Scrap\x150_c101.txt

D:\TopDir\Up\Bottom\x150b02.txt
D:\TopDir\Up\Bottom\x150c101.txt

D:\TopDir\Up\Bottom\x150_fst_b02.txt
D:\TopDir\Up\Bottom\x150_fst_c101.txt

D:\TopDir\Middle\x150_coupe_b02_dampers.txt
D:\TopDir\Middle\x150_coupe_c101_dampers.txt

D:\TopDir\Middle\x150_coupe_b02.txt
D:\TopDir\Middle\x150_coupe_c101.txt

D:\TopDir\Bottom\x150_b02_dampers.txt
D:\TopDir\Bottom\x150_c101_dampers.txt

D:\TopDir\Bottom\x150_coupe_b02_dampers.txt
D:\TopDir\Bottom\x150_coupe_c101_dampers.txt
etc.

D:\TopDir>

This would be a useful check to just to make sure all subdirectories were checked and the required files were changed correctly.

If this is too much hassle then don't worry but would be (once again) much appreciated!!

Thanks again!

Vin



0

Response Number 12
Name: Mechanix2Go
Date: February 1, 2006 at 21:16:50 Pacific
Reply:

Hi Vin,

::== Vrename2.bat
@echo off

if %2'==' goto :syntax

:main

set OLDstr=%1
set NEWstr=%2
> chg.bat echo change.com name.# "%OLDstr%" "%NEWstr%"
dir/s/b |find /i "%1">workLIST
for /f "tokens=*" %%F in (workLIST) do call :sub1 %%F

goto :eof

:sub1

set old=%1
echo %~nx1>name.#
call chg>nul
set /p new=<name.#
ren %old% %new%
echo %old%
echo %new%
echo.
goto :eof

:syntax

echo you need two parameters, string-in-filename AND replacement-string

:eof
:: == DONE


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

M2


0

Response Number 13
Name: Vin
Date: February 2, 2006 at 03:03:14 Pacific
Reply:

Hi M2,

Once again - perfect! Exactly what I wanted! Thanks once again!

Vin


0

Response Number 14
Name: Mechanix2Go
Date: February 2, 2006 at 08:10:59 Pacific
Reply:

Hi Vin,

GTH

I'll send you a decent copy, since all my neat indents get trashed here.


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 Programming Forum Home


Sponsored links

Ads by Google


Results for: Rename Multiple Files

rename multiple file whit batch fil www.computing.net/answers/programming/rename-multiple-file-whit-batch-fil/9028.html

Ren multiple files in DOS www.computing.net/answers/programming/ren-multiple-files-in-dos/19273.html

Renaming Files www.computing.net/answers/programming/renaming-files/15455.html