rename srt files in sub folder to video file

August 23, 2010 at 10:06:48
Specs: Windows 7
Hello all,

Scripting in dos is completely new for me and I will really appreciate some help! I've looked at other postings in the forum and still haven't made much progress.

I need a script that will enter all sub folders one level down, check if the folder contains a .srt file, and if found rename it to the same file name (without extention) as the video file in that folder.

Each sub folder contains only contains one video file, it can be either an .avi , .mkv or .iso

e.g
ROOT
Folder 1: abc.srt ; moviefile.avi
Folder 2: def.srt ; moviefile2.avi

With the script executed in ROOT folder, abc.srt is renamed to moviefile.srt
def.srt is renamed to moviefile2.srt (files remain in their original folders)

If someone can help, i'd really appreciate it.


See More: rename srt files in sub folder to video file

Report •

#1
August 23, 2010 at 18:08:05
@echo off
setLocal EnableDelayedExpansion
for /f "tokens=*" %%a in ('dir /b /ad') do (
set /a n+=1
set dir!n!=%%a
)
set c=
:loop
set /a c+=1
cd !dir%c%!
set name=
for /f %%b in ('dir /b *.avi *.mkv *.iso') do (
set name=%%b
set name=!name:.mkv=!
set name=!name:.avi=!
set name=!name:.iso=!
rename *.srt !name!.srt
)
cd..
if %c%==%n% goto out
goto loop
:out
cls
echo Rename completed..
pause

Report •

#2
August 24, 2010 at 01:06:21
thank you very much! (i don't think I could have written this myself)

I made a minor adjustment to deal with spaces in movie file names by changing the loop to:

for /f "delims=" %%b in ('dir /b *.avi *.mkv *.iso') do (
set name=%%b
set name=!name:.mkv=!
set name=!name:.avi=!
set name=!name:.iso=!
rename *.srt "!name!.srt"
)


I hope others can find this useful also.


Report •

#3
August 24, 2010 at 13:42:00
You're welcome and thanks for reporting back the outcome.

Report •
Related Solutions


Ask Question