Rename batch file?

February 2, 2011 at 09:30:55
Specs: WinXP
Hi, I was just wondering if anybody knew of a way that I could search through multiple folders and rename a specific filename (in this case folder.jpg) to have the same name as the first *.mp3 file found in that folder? Thanks

See More: Rename batch file?

Report •

#1
February 2, 2011 at 12:05:49
the first *.mp3 file found in that folder?
As defined by what?

How To Ask Questions The Smart Way


Report •

#2
February 2, 2011 at 12:12:42
So for example the folder.jpg in each of these folders would be renamed to first file.jpg, thanks

Folder
---First file.mp3
---Second file.mp3
---Etc…
---Folder.jpg
Folder
---First file.mp3
---Second file.mp3
---Etc…
---Folder.jpg
Folder
---First file.mp3
---Second file.mp3
---Etc…
---Folder.jpg


Report •

#3
February 2, 2011 at 12:15:32
So search the tree for "Folder.jpg" and rename it to "First File.jpg"?

How To Ask Questions The Smart Way


Report •

Related Solutions

#4
February 2, 2011 at 12:17:11
dir *.pm3 /s

will search all subdirectories.

To rename the first folder I think you can use this...

@echo off
for /f "tokens=*" %%a in ('dir *.mp3 /b /s') do (
ren "%%a\.." "%%a\newname"
)

NOT TESTED

you may want to do...

@echo off
for /f "tokens=*" %%a in ('dir *.mp3 /b /s') do (
echo ren "%%a\.." "%%a\newname"
pause
)

Until you get it the way you want.


Report •

#5
February 2, 2011 at 15:37:29
I guess you want to Rename the folder.jpg to the same name as the first mp3 has in each folder.

Check this out.

@echo off & setlocal enabledelayedexpansion
for /f "tokens=*" %%a in ('dir /b /s folder.jpg') do (
cd %%~pa
dir /b *.mp3 >%temp%\1.txt
set /p newname=<%temp%\1.txt
for /f %%h in ("!newname!") do ren folder.jpg %%~nh.jpg
cd..
pause

)

Subhash Chandra.
www.s-chandra.co.cc


Report •

#6
February 2, 2011 at 15:59:37
Hi Subhash,

Yes that’s exactly it; I want to rename each folder.jpg to the same name as the first mp3 file in their respective folders.

Your code works almost perfectly apart from when renaming it only selects the first word ignoring everything else after any spaces, so for example “this file name” would just become “this”? Thanks


Report •

#7
February 2, 2011 at 16:34:08
The problem I have with that there is no guaranteed order, so someone has to define what "first" means.

How To Ask Questions The Smart Way


Report •

#8
February 2, 2011 at 18:45:40
for /f "tokens=*" %%h in ("!newname!") do ren folder.jpg "%%~nh.jpg"


use this line instead of origianl code if you got spaces between your file names..

It shold work..

Subhash Chandra.
www.s-chandra.co.cc


Report •

#9
February 3, 2011 at 02:43:02
Thanks Subhash,

That worked perfectly, very much appreciated :-)


Report •

#10
February 3, 2011 at 05:03:49
you welcome.

Subhash Chandra.
www.s-chandra.co.cc


Report •

Ask Question