Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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.htmlb22 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

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.htmlC:\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.htmlFilenames 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

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

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.htmlC:\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.htmlFilenames 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

"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.htmlNotice 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

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

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

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

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

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:
::== Vrename.bat
@echo offif %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 %%Fgoto :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

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.txtD:\TopDir\Up\x150_b02_dampers.txt
D:\TopDir\Up\x150_c101_dampers.txtD:\TopDir\Up\x150_coupe_b02_dampers.txt
D:\TopDir\Up\x150_coupe_c101_dampers.txtD:\TopDir\Up\New\x150dampers_b02.txt
D:\TopDir\Up\New\x150dampers_c101.txtD:\TopDir\Up\New\Scrap\x150dampers_b02.txt
D:\TopDir\Up\New\Scrap\x150dampers_c101.txtD:\TopDir\Up\New\Scrap\x150_b02.txt
D:\TopDir\Up\New\Scrap\x150_c101.txtD:\TopDir\Up\Bottom\x150b02.txt
D:\TopDir\Up\Bottom\x150c101.txtD:\TopDir\Up\Bottom\x150_fst_b02.txt
D:\TopDir\Up\Bottom\x150_fst_c101.txtD:\TopDir\Middle\x150_coupe_b02_dampers.txt
D:\TopDir\Middle\x150_coupe_c101_dampers.txtD:\TopDir\Middle\x150_coupe_b02.txt
D:\TopDir\Middle\x150_coupe_c101.txtD:\TopDir\Bottom\x150_b02_dampers.txt
D:\TopDir\Bottom\x150_c101_dampers.txtD:\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

Hi Vin,
::== Vrename2.bat
@echo offif %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 %%Fgoto :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

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

![]() |
![]() |
![]() |

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