#1 2013-05-09 02:49

eR@SeR
Senior Member
From: Земун, Србија
Registered: 2008-01-23
Posts: 353

Rename files with specific extension same as provided one

Well, situation is next. Have main folder called "Movies". In it folders with Movie names.
Structure is like this:

D:\Movies\Moviename\Moviename.avi
D:\Movies\Moviename\Moviename.srt
D:\Movies\Moviename\Moviename.jpg
D:\Movies\Moviename\CouldBeMoreFiles...

I want to rename all subtitle files (extension are .ass .srt .ssa .sub .txt) same as video file (extensions are .asf .avi .flv .mp4 .mkv .mp(e)g .wmv) in that folder so I can play subtitles on any device. Most of them/all requires same video/subtitle filename in order to be displayed.

Example:

D:\Movies\Green Zone [2010]\Green.Zone[2010]DvDrip-aXXo.avi
D:\Movies\Green Zone [2010]\Green Zone.srt
D:\Movies\Green Zone [2010]\green.zone-aXXo.nfo

to be...

D:\Movies\Green Zone [2010]\Green.Zone[2010]DvDrip-aXXo.avi
D:\Movies\Green Zone [2010]\Green.Zone[2010]DvDrip-aXXo.srt
D:\Movies\Green Zone [2010]\green.zone-aXXo.nfo

I would like it if script could be used for other extensions, just rename/replace original and resulting ones. If I missed something to mention, ask.

Thanks! smile


TRUTH, FREEDOM, JUSTICE and FATHERLAND are the highest morale values which human is born, lives and dies for!

Offline

#2 2013-05-09 16:53

Stefan
Moderator
From: Germany, EU
Registered: 2007-10-23
Posts: 1,161

Re: Rename files with specific extension same as provided one

You mean there could be only one "video file (extensions are .asf .avi .flv .mp4 .mkv .mp(e)g .wmv)" in a folder
but any amount of "subtitle files (extension are .ass .srt .ssa .sub .txt)" ?

So the rule is:
Step 1
- enumerate all files from a folder
- find the "video file"
- take the name part of that "video file"
Step 2
- enumerate all files from a folder
- work on the "subtitle files"
- rename them with that video file base name, leaving original extension

?yes?


At the moment I can think of such workflow:
Step 1 - PS-Rule1
- enumerate all files from a folder
- find the "video file"
- take the name part of that "video file"
- store it somehow to a var or file or clipboard (http://www.den4b.com/forum/viewtopic.php?id=1754)
Step 2 - PS-Rule2
- get the content of the var or file or clipboard
- enumerate all files from a folder
- work on the "subtitle files"
- rename them with that video file base name, leaving original extension


(Or merged to single rule which you have to execute twice,.... I don' know at the moment if we could use a kind of "Initial procedure" to do this work for us?)




666 ^^


Read the  *WIKI* for HELP + MANUAL + Tips&Tricks.
If ReNamer had helped you, please *DONATE* to Denis or buy a PRO license. (Read *Lite vs Pro*)

Offline

#3 2013-05-13 02:23

eR@SeR
Senior Member
From: Земун, Србија
Registered: 2008-01-23
Posts: 353

Re: Rename files with specific extension same as provided one

You mean there could be only one "video file (extensions are .asf .avi .flv .mp4 .mkv .mp(e)g .wmv)" in a folder
but any amount of "subtitle files (extension are .ass .srt .ssa .sub .txt)" ?

So the rule is:
Step 1
- enumerate all files from a folder
- find the "video file"
- take the name part of that "video file"
Step 2
- enumerate all files from a folder
- work on the "subtitle files"
- rename them with that video file base name, leaving original extension

?yes?

Actually there could be 2 video files (for example movie have cd1 or cd2) so there are 2 subtitle files for both videos. In other cases 1 video file is with 1 subtitle file,...BUT it could be 2 or rarely 3 subtitle files as alternative in folder. And yes, that kind of procedure (step 1/step 2) just with this:

- if there are 2 or more same extensions fix conflicting new names

(Or merged to single rule which you have to execute twice,.... I don' know at the moment if we could use a kind of "Initial procedure" to do this work for us?)

So are you telling me that we cannot do anything about this? roll

Thanx for effort smile


TRUTH, FREEDOM, JUSTICE and FATHERLAND are the highest morale values which human is born, lives and dies for!

Offline

#4 2013-05-14 07:40

SafetyCar
Senior Member
Registered: 2008-04-28
Posts: 446
Website

Re: Rename files with specific extension same as provided one

Does this do more or less what you wanted?

It takes the name of the first video it finds, probably that should be enough. But if not, we can make it give a warning or something.

var
  BrotherFiles: TStringsArray;
  BasePath: WideString;
  I: Integer;

begin
  if (Length(SubMatchesRegEx(FileName, '\.(ASS|SRT|SSA|SUB|TXT)$', False)) > 0) then
  begin

    BasePath := WideExtractFilePath(FilePath);
    
    SetLength(BrotherFiles, 0);
    WideScanDirForFiles(BasePath, BrotherFiles, False, False, False, '*');
    For I:=0 to Length(BrotherFiles)-1 do
    begin
      if (Length(SubMatchesRegEx(BrotherFiles[i], '\.(ASF|AVI|FLV|MP4|MKV|MPE?G|WMV)$', False)) > 0) then
      begin
        FileName := WideExtractBaseName(BrotherFiles[i]) + WideExtractFileExt(FileName);
        Break;
      end;
    end;

  end;
end.

Last edited by SafetyCar (2013-05-14 07:45)


If this software has helped you, consider getting your pro version. :)

Offline

#5 2013-05-16 04:29

eR@SeR
Senior Member
From: Земун, Србија
Registered: 2008-01-23
Posts: 353

Re: Rename files with specific extension same as provided one

Does this do more or less what you wanted?

Just as you said more or less it's doing. There are titles that have more info than video files so I've switched extensions in PS and works smile

It takes the name of the first video it finds, probably that should be enough. But if not, we can make it give a warning or something.

Enough for many examples smile But... Try to handle these situations:

D:\Movies\King Kong [2005]\King Kong CD1 (2005)(1).srt
D:\Movies\King Kong [2005]\King Kong CD1 (2005).srt
D:\Movies\King Kong [2005]\King Kong CD2 (2005)(1).srt
D:\Movies\King Kong [2005]\King Kong CD2 (2005).srt
D:\Movies\King Kong [2005]\king-kong-2005-aXXo.nfo
D:\Movies\King Kong [2005]\King.Kong[2005]DvDrip.AC3[Eng]CD1-aXXo.avi
D:\Movies\King Kong [2005]\King.Kong[2005]DvDrip.AC3[Eng]CD2-aXXo.avi
D:\Movies\One Day 2011\1-3-3-8.com_l-oneday.xvid.cd2.avi
D:\Movies\One Day 2011\l-oneday.xvid.cd1.srt
D:\Movies\One Day 2011\l-oneday.xvid.cd2.srt
D:\Movies\One Day 2011\One Day 2011 BRRip XVID AbSurdiTy.avi
D:\Movies\The Manchurian Candidate\dmt-mancan-cd1.avi
D:\Movies\The Manchurian Candidate\dmt-mancan-cd1_SRPSKI_Duke2.srt
D:\Movies\The Manchurian Candidate\dmt-mancan-cd2.avi
D:\Movies\The Manchurian Candidate\dmt-mancan-cd2_SRPSKI_Duke2.srt

becomes

D:\Movies\King Kong [2005]\King.Kong[2005]DvDrip.AC3[Eng]CD1-aXXo.srt
D:\Movies\King Kong [2005]\King.Kong[2005]DvDrip.AC3[Eng]CD1-aXXo.srt
D:\Movies\King Kong [2005]\King.Kong[2005]DvDrip.AC3[Eng]CD1-aXXo.srt
D:\Movies\King Kong [2005]\King.Kong[2005]DvDrip.AC3[Eng]CD1-aXXo.srt

D:\Movies\King Kong [2005]\king-kong-2005-aXXo.nfo
D:\Movies\King Kong [2005]\King.Kong[2005]DvDrip.AC3[Eng]CD1-aXXo.avi
D:\Movies\King Kong [2005]\King.Kong[2005]DvDrip.AC3[Eng]CD2-aXXo.avi
D:\Movies\One Day 2011\1-3-3-8.com_l-oneday.xvid.cd2.avi
D:\Movies\One Day 2011\1-3-3-8.com_l-oneday.xvid.cd2.srt
D:\Movies\One Day 2011\1-3-3-8.com_l-oneday.xvid.cd2.srt
(duplicate)
D:\Movies\One Day 2011\One Day 2011 BRRip XVID AbSurdiTy.avi
D:\Movies\The Manchurian Candidate\dmt-mancan-cd1.avi
D:\Movies\The Manchurian Candidate\dmt-mancan-cd1.srt
D:\Movies\The Manchurian Candidate\dmt-mancan-cd2.avi
D:\Movies\The Manchurian Candidate\dmt-mancan-cd1.srt (should be cd2!)

So if you can, try to modify script to handle occurrences with cd1 or cd2 to replace them properly.

Note: In 2nd folder 2nd video file don't have cd1 as it should so if you cannot made to correct itself never mind, it's only one occurrence.
Note2: If there are duplicates as in 1st folder "Fix conflicting new names" is used.


D:\Movies\Friends 5\Friends_S5_01.avi
D:\Movies\Friends 5\Friends_S5_01.srt
D:\Movies\Friends 5\Friends_S5_02.avi
D:\Movies\Friends 5\Friends_S5_02.srt
D:\Movies\Friends 5\Friends_S5_03.avi
D:\Movies\Friends 5\Friends_S5_03.srt
D:\Movies\Friends 5\Friends_S5_04.avi
D:\Movies\Friends 5\Friends_S5_04.srt
.
.
.

becomes

D:\Movies\Friends 5\Friends_S5_01.avi
D:\Movies\Friends 5\Friends_S5_01.srt
D:\Movies\Friends 5\Friends_S5_02.avi
D:\Movies\Friends 5\Friends_S5_01.srt
D:\Movies\Friends 5\Friends_S5_03.avi
D:\Movies\Friends 5\Friends_S5_01.srt
D:\Movies\Friends 5\Friends_S5_04.avi
D:\Movies\Friends 5\Friends_S5_01.srt
.
.
.

If video and title are same ignore replacing.

I think that's it. Thanx again, you saved me a lots of renaming wink


TRUTH, FREEDOM, JUSTICE and FATHERLAND are the highest morale values which human is born, lives and dies for!

Offline

#6 2013-05-16 09:08

SafetyCar
Senior Member
Registered: 2008-04-28
Posts: 446
Website

Re: Rename files with specific extension same as provided one

eR@SeR wrote:

Enough for many examples smile But... Try to handle these situations:

That is getting complex, I think is better to keep it simple...

Why not this?...

You add every file to renamer, and you sort the files first one video then it's subtitle, and so on.
And then with this script you put the name of the last video on the next file.

var
  BasePath, LastPath, LastName: WideString;

begin
  BasePath := WideExtractFilePath(FileName);
  
  if (Length(SubMatchesRegEx(FileName, '\.(ASF|AVI|FLV|MP4|MKV|MPE?G|WMV)$', False)) > 0) then
  begin
    LastName := FileName;
    LastPath := BasePath;
  end
  else
  begin
    if (BasePath <> LastPath) then
    begin
      LastPath := BasePath;
      LastName := '';
    end;
    
    if (LastName <> '') then
    begin
      FileName := WideStripExtension(LastName) + WideExtractFileExt(FileName);
      LastName := '';
    end
    else
    begin
      FileName := '';
    end;  
  end;
end.

If you don't like the order, reorder it (Ctrl+Arrow) and preview again.
It's a little bit more work but it gives more awareness of what is changing, and I don't think that can be bad...


If this software has helped you, consider getting your pro version. :)

Offline

#7 2013-05-16 15:11

eR@SeR
Senior Member
From: Земун, Србија
Registered: 2008-01-23
Posts: 353

Re: Rename files with specific extension same as provided one

If you don't like the order, reorder it (Ctrl+Arrow) and preview again.
It's a little bit more work but it gives more awareness of what is changing, and I don't think that can be bad...

In my case it's lots of work sad It renames every file below video file no matter of extension (except video ext) and that is what we want to evade.

D:\Movies\King Kong [2005]\King.Kong[2005]DvDrip.AC3[Eng]CD1-aXXo.avi
D:\Movies\King Kong [2005]\King Kong CD1 (2005)(1).srt
D:\Movies\King Kong [2005]\King Kong CD1 (2005).srt
D:\Movies\King Kong [2005]\King.Kong[2005]DvDrip.AC3[Eng]CD2-aXXo.avi
D:\Movies\King Kong [2005]\King Kong CD2 (2005)(1).srt
D:\Movies\King Kong [2005]\King Kong CD2 (2005).srt
D:\Movies\King Kong [2005]\king-kong-2005-aXXo.nfo

becomes

D:\Movies\King Kong [2005]\King.Kong[2005]DvDrip.AC3[Eng]CD1-aXXo.avi
D:\Movies\King Kong [2005]\King.Kong[2005]DvDrip.AC3[Eng]CD1-aXXo.srt
D:\Movies\King Kong [2005]\King Kong CD1 (2005).srt (ignored - should be as upper result)
D:\Movies\King Kong [2005]\King.Kong[2005]DvDrip.AC3[Eng]CD2-aXXo.avi
D:\Movies\King Kong [2005]\King.Kong[2005]DvDrip.AC3[Eng]CD2-aXXo.srt
D:\Movies\King Kong [2005]\King Kong CD2 (2005).srt (ignored - should be as upper result)
D:\Movies\King Kong [2005]\king-kong-2005-aXXo.nfo

Have to repeat/add files again sad

With modified:

D:\Movies\King Kong [2005]\King Kong CD1 (2005)(1).srt
D:\Movies\King Kong [2005]\King Kong CD1 (2005).srt
D:\Movies\King Kong [2005]\King Kong CD2 (2005)(1).srt
D:\Movies\King Kong [2005]\King Kong CD2 (2005).srt
D:\Movies\King Kong [2005]\king-kong-2005-aXXo.nfo
D:\Movies\King Kong [2005]\King.Kong[2005]DvDrip.AC3[Eng]CD1-aXXo.avi
D:\Movies\King Kong [2005]\King.Kong[2005]DvDrip.AC3[Eng]CD2-aXXo.avi

becomes

D:\Movies\King Kong [2005]\King.Kong[2005]DvDrip.AC3[Eng]CD1-aXXo.srt
D:\Movies\King Kong [2005]\King.Kong[2005]DvDrip.AC3[Eng]CD1-aXXo.srt
D:\Movies\King Kong [2005]\King.Kong[2005]DvDrip.AC3[Eng]CD2-aXXo.srt
D:\Movies\King Kong [2005]\King.Kong[2005]DvDrip.AC3[Eng]CD2-aXXo.srt

D:\Movies\King Kong [2005]\king-kong-2005-aXXo.nfo
D:\Movies\King Kong [2005]\King.Kong[2005]DvDrip.AC3[Eng]CD1-aXXo.avi
D:\Movies\King Kong [2005]\King.Kong[2005]DvDrip.AC3[Eng]CD2-aXXo.avi

No matter of the order and thinking what and where to put. The simplest solution, don't you think?

First script is very good, I just suggested what criteria to add to make it even better to handle more things without changing the intended result, but if that cannot be modified than I'll use last PS.


TRUTH, FREEDOM, JUSTICE and FATHERLAND are the highest morale values which human is born, lives and dies for!

Offline

#8 2013-05-16 16:46

SafetyCar
Senior Member
Registered: 2008-04-28
Posts: 446
Website

Re: Rename files with specific extension same as provided one

eR@SeR wrote:

In my case it's lots of work sad It renames every file below video file no matter of extension (except video ext) and that is what we want to evade.

What problem do you see in the last script? not avoiding non subtitle extensions? It can be added.

eR@SeR wrote:

Have to repeat/add files again sad

I made the script clear the last name if it was already used, do you want to keep using it even if it creates duplicates?

eR@SeR wrote:

No matter of the order and thinking what and where to put. The simplest solution, don't you think?

Not so simple to code...

eR@SeR wrote:

First script is very good, I just suggested what criteria to add to make it even better to handle more things without changing the intended result, but if that cannot be modified than I'll use last PS.

Now, I tried to fix the previous one, and this is what came up: (I don't think I can get much better than this...)

var
  BrotherFiles, VidFiles: TStringsArray;
  BasePath, RExVid, RExSub: WideString;
  I: Integer;
  Found: Boolean;

begin
  if (Length(SubMatchesRegEx(FileName, '\.(ASS|SRT|SSA|SUB|TXT)$', False)) > 0) then
  begin
    
    BasePath := WideExtractFilePath(FilePath);
    
    SetLength(VidFiles, 0);
    SetLength(BrotherFiles, 0);
    
    WideScanDirForFiles(BasePath, BrotherFiles, False, False, False, '*');
    For I:=0 to Length(BrotherFiles)-1 do
    begin
      if (Length(SubMatchesRegEx(BrotherFiles[i], '\.(ASF|AVI|FLV|MP4|MKV|MPE?G|WMV)$', False)) > 0) then
      begin
        SetLength(VidFiles, Length(VidFiles)+1);
        VidFiles[Length(VidFiles)-1] := WideExtractBaseName(BrotherFiles[i]);
      end;
    end;
    
    if (Length(VidFiles) > 0) then
    begin
      if (Length(VidFiles) = 1) then
      begin
        FileName := VidFiles[0] + WideExtractFileExt(FileName);
      end
      else
      begin
        RExSub := ReplaceRegEx(FileName, '^.*?((\b|_)(CD\d+)(\b|_)).*?$', '$3', False, True);
        if (RExSub <> '') then
        begin
          Found := False;
          
          For I:=0 to Length(VidFiles)-1 do begin
            RExVid := ReplaceRegEx(VidFiles[i], '^.*?((\b|_)(CD\d+)(\b|_)).*?$', '$3', False, True);
            if WideSameText(RExVid, RExSub) then
            begin
              Found := True;
              FileName := VidFiles[i] + WideExtractFileExt(FileName);
              Break
            end;
          end;
          
          if (not Found) then
            FileName := '';
        end;
      end;
    end
    else
    begin
      FileName := '';
    end;
    
  end;
end.

If this software has helped you, consider getting your pro version. :)

Offline

#9 2013-05-17 02:03

eR@SeR
Senior Member
From: Земун, Србија
Registered: 2008-01-23
Posts: 353

Re: Rename files with specific extension same as provided one

I made the script clear the last name if it was already used, do you want to keep using it even if it creates duplicates?
Not so simple to code...

You already done it big_smile

Now, I tried to fix the previous one, and this is what came up: (I don't think I can get much better than this...)

What I've asked, you made it. Works almost perfect for every occurred scenario! Thanx! smile

This was ignored only:

D:\Movies\Casshern\Casshern.jpg
D:\Movies\Casshern\Cassherncd1 Srp.srt
D:\Movies\Casshern\Cassherncd1.avi
D:\Movies\Casshern\Cassherncd2 Srp.srt
D:\Movies\Casshern\Cassherncd2.avi

...but never mind.

So, script finds video files (.asf .avi .flv .mp4 .mkv .mp(e)g .wmv) and renames corresponding subtitle files (.ass .srt .ssa .sub .txt) as video basenames. Also do:

- skips renaming of same basenames
- finds cd(digit) in filename and renames corresponding files containing cd(digit) [case insensitive]
- if there is surplus of subtitle file(s) in same folder it will be renamed as 1st one (must use "Fix conflicting new names")

Note: *.txt don't have to be subtitle file so be careful!

var
  BrotherFiles, VidFiles: TStringsArray;
  BasePath, RExVid, RExSub: WideString;
  I: Integer;
  Found: Boolean;

begin
  if (Length(SubMatchesRegEx(FileName, '\.(ASS|SRT|SSA|SUB|TXT)$', False)) > 0) then
  begin
    
    BasePath := WideExtractFilePath(FilePath);
    
    SetLength(VidFiles, 0);
    SetLength(BrotherFiles, 0);
    
    WideScanDirForFiles(BasePath, BrotherFiles, False, False, False, '*');
    For I:=0 to Length(BrotherFiles)-1 do
    begin
      if (Length(SubMatchesRegEx(BrotherFiles[i], '\.(ASF|AVI|FLV|MP4|MKV|MPE?G|WMV)$', False)) > 0) then
      begin
        SetLength(VidFiles, Length(VidFiles)+1);
        VidFiles[Length(VidFiles)-1] := WideExtractBaseName(BrotherFiles[i]);
      end;
    end;
    
    if (Length(VidFiles) > 0) then
    begin
      if (Length(VidFiles) = 1) then
      begin
        FileName := VidFiles[0] + WideExtractFileExt(FileName);
      end
      else
      begin
        RExSub := ReplaceRegEx(FileName, '^.*?((\b|_)(CD\d+)(\b|_)).*?$', '$3', False, True);
        if (RExSub <> '') then
        begin
          Found := False;
          
          For I:=0 to Length(VidFiles)-1 do begin
            RExVid := ReplaceRegEx(VidFiles[i], '^.*?((\b|_)(CD\d+)(\b|_)).*?$', '$3', False, True);
            if WideSameText(RExVid, RExSub) then
            begin
              Found := True;
              FileName := VidFiles[i] + WideExtractFileExt(FileName);
              Break
            end;
          end;
          
          if (not Found) then
            FileName := '';
        end;
      end;
    end
    else
    begin
      FileName := '';
    end;
    
  end;
end.

TRUTH, FREEDOM, JUSTICE and FATHERLAND are the highest morale values which human is born, lives and dies for!

Offline

Board footer

Powered by FluxBB