Page 1 of 1

Batch to rename all .avi and .srt files in order.

Posted: 02 May 2013 07:20
by psychoid69
I want to make a script that would work with more .avi and .srt files in the same folder.

This is what I've got so far:

Code: Select all

@echo off
for /f "tokens=*" %%P in ('DIR /B /O:N *.avi') do (
for /f "tokens=*" %%R in ('DIR /B /O:N *.srt') do (
ren %%R %%~nP.srt
)
)


It's obvious to me, why it doesn't work (every .srt file would be renamed to the name of the last .avi file in foler), however I have no more ideas how to solve it... I was thinking, if for could somehow take two parameters, like for /f "tokens=*" %%P in ('DIR /B /O:N *.avi') and %%R in ('DIR /B /O:N *.srt'), but that's not how for works unfortunately.

To give you a better idea about my files, here's what DIR /B /O:N *.avi/srt gives me:

Code: Select all

DIR /B /O:N *.avi
SeriesName.S04E01.avi
SeriesName.S04E02.avi
SeriesName.S04E03.avi
SeriesName.S04E04.avi
SeriesName.S04E05.avi
SeriesName.S04E06.avi
SeriesName.S04E07.avi
SeriesName.S04E08.avi
SeriesName.S04E09.avi
SeriesName.S04E10.avi
SeriesName.S04E11.avi
SeriesName.S04E12.avi
SeriesName.S04E13.avi

DIR /B /O:N *.srt
Series Name - 04x01.srt
Series Name - 04x02.srt
Series Name - 04x03.srt
Series Name - 04x04.srt
Series Name - 04x05.srt
Series Name - 04x06.srt
Series Name - 04x07.srt
Series Name - 04x08.srt
Series Name - 04x09.srt
Series Name - 04x10.srt
Series Name - 04x11.srt
Series Name - 04x12.srt
Series Name - 04x13.srt


Please help. :)

Re: Batch to rename all .avi and .srt files in order.

Posted: 02 May 2013 07:53
by foxidrive
It's fairly simple if you can ensure that the avi name and the appropriate srt name are the same - or if you can rename them both so they start with a number, like this.

A batch file can help with that, if the number of avi files equal the number of srt files in the folder, and they sort properly by name.


01 SeriesName.S04E01.avi
02 SeriesName.S04E02.avi
03 SeriesName.S04E03.avi
04 SeriesName.S04E04.avi
05 SeriesName.S04E05.avi
06 SeriesName.S04E06.avi
07 SeriesName.S04E07.avi


01 Series Name - 04x01.srt
02 Series Name - 04x02.srt
03 Series Name - 04x03.srt
04 Series Name - 04x04.srt
05 Series Name - 04x05.srt
06 Series Name - 04x06.srt
07 Series Name - 04x07.srt

Re: Batch to rename all .avi and .srt files in order.

Posted: 02 May 2013 07:54
by abc0502
Test This, not sure about it, so make sure to check the content of each srt file to see if it match or not.

Test on 3 files first

Code: Select all

@Echo OFF

SETLOCAL EnableDelayedExpansion
For /F "delims=" %%A In (' DIR /B /A:-D "*.avi" ') Do (
   SET "CurrFileName=%%~nA"
   For /F "tokens=1-2* delims=." %%a In ("%%A") Do (
      SET "SecName=%%b"
      SET "Se=!SecName:~1,2!"
      SET "Ep=!SecName:~4,2!"
   )
   For /F "delims=" %%x In (' DIR /B /A:-D "*.srt" ^|Findstr "$*!Se!x!Ep!.srt" ') Do (
      Ren "%%x" "!CurrFileName!.srt"   
   )
)
Pause

Re: Batch to rename all .avi and .srt files in order.

Posted: 02 May 2013 08:08
by psychoid69
abc0502, thankyou, but your script does exactly nothing to any of the files. :)

foxidrive, I want to use the script only in folders which contain the same number of .avi files as .srt files - you say it's easy, could you please direct me towards the solution if you don't want to write the script?

Re: Batch to rename all .avi and .srt files in order.

Posted: 02 May 2013 08:09
by abc0502
Strange ? :?:
did you put it in the same folder with your avi and srt files :?: ?

Re: Batch to rename all .avi and .srt files in order.

Posted: 02 May 2013 08:12
by psychoid69
abc0502, yes I did. I'll try to debug it.

Re: Batch to rename all .avi and .srt files in order.

Posted: 02 May 2013 08:16
by abc0502
are you sure the srt files has this format " 04x01.srt" and your avi "S04E01"?
I tested it on some files with the same format, i used the names you posted and it worked.

Re: Batch to rename all .avi and .srt files in order.

Posted: 02 May 2013 08:24
by psychoid69
My bad... I was testing on my simple-text test files named test1.avi, test2.avi, test3.avi and title1.srt, title2.srt, title3.srt; your script didn't work with those... It works with the files in my first post...

So... this could not be made so that it would work in all scenarios, given that both .avi and .srt files sort in order when listed with dir (that's why I used switch /O:N)?

No matter if we solve it for all scenarios or not, I'm very grateful for what you've given me so far!! Thanks!!! :)

Re: Batch to rename all .avi and .srt files in order.

Posted: 02 May 2013 08:27
by foxidrive
Here you go - run the first batch file in a copy of your folder and it will mate the avi and srt files, by renaming them and adding a number before each filename.
It expects the same number of AVI and SRT files and they must sort in order by name correctly.

Code: Select all

@echo off
set num=00
for /f "delims=" %%a in ('dir *.avi /o:n /b') do call :next "%%a"
set num=00
for /f "delims=" %%a in ('dir *.srt /o:n /b') do call :next "%%a"
goto :EOF
:next
set /a num=num+1
set n=000%num%
set n=%n:~-3%
ren "%~1" "%n% %~1"


In the second batch file the command echo avi "%%a" srt "%%c" can be modified to launch a tool to process the avi and srt.

Code: Select all

@echo off
for /f "delims=" %%a in ('dir *.avi /o:n /b') do (
for /f %%b in ("%%a") do (
for /f "delims=" %%c in ('dir "%%b *.srt" /o:n /b') do (
echo avi "%%a" srt "%%c"
)
)
)
pause

Re: Batch to rename all .avi and .srt files in order.

Posted: 02 May 2013 08:29
by abc0502
it's might be different, but if you can post a sample of each different naming method, we could find something common in all of then and is used to sort and rename.

BTW, if foxidrive code works with you, you can change my code a little and make it then rename srt with the same name.
Only after Foxidrive code.

Re: Batch to rename all .avi and .srt files in order.

Posted: 02 May 2013 08:30
by psychoid69
Thankyou both very much! :)
I've got it all now. :)

Re: Batch to rename all .avi and .srt files in order.

Posted: 02 May 2013 08:48
by foxidrive
I lost track of what we were trying to do! :D

This will rename the SRT files to the same name as the AVI file.
It expects the same number of AVI and SRT files and they must sort in order by name correctly

Filenames with ! and % might cause problems.


Code: Select all

@echo off
setlocal enabledelayedexpansion
dir *.srt /o:n /b>file.tmp
<file.tmp (
for /f "delims=" %%a in ('dir *.avi /o:n /b') do (
set /p srt=
echo ren "!srt!" "%%~na.srt"
ren "!srt!" "%%~na.srt"
)
)
del file.tmp

Re: Batch to rename all .avi and .srt files in order.

Posted: 02 May 2013 09:19
by psychoid69
Yes, foxidrive; this is now exactly the way I wanted it... I was going to try to modify your previous code to achieve this, but thank you very much - you've done it for me (and anyone else who might need this)! :mrgreen: