#1 2013-05-21 02:41

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

Synchronize filenames in different file paths

I need a script to synchronize filenames in different file paths i.e. drives (D:\, F:\, G:\...). To recognize which ones to rename, conditions are:

- same file size in bytes
- same extension
- or same CRC (if possible)
- skip renaming of same filenames

Those files would be added in files table and compared. If in one drive are two or more identical files give an error and/or ignore renaming. To determine direction of synchronizing filenames, create and point the line in code so user can choose direction.
Upper line in script renames/gives lower one new filenames. Could be G:\ gives D:\ new filenames...

Examples:

D:\Movies\Salt 2010\Noir.nfo    3,986
D:\Movies\Salt 2010\Salt.2010.R5.LiNE.Xvid {1337x}-Noir.avi    735,872,924
D:\Movies\Salt 2010\Salt.2010.R5.LiNE.Xvid {1337x}-Noir.srt    58,703

becomes

G:\Movies\Salt 2010\1337x.Org.txt    26
G:\Movies\Salt 2010\Blazinseedboxes.com.txt    83
G:\Movies\Salt 2010\Noir.nfo    3,986    --->   (ignored)
G:\Movies\Salt 2010\Salt [2010].srt    58,703    --->   Salt.2010.R5.LiNE.Xvid {1337x}-Noir.srt    58,703
G:\Movies\Salt 2010\Salt.2010.R5.LiNE.Xvid {1337x}-Noir.avi    735,872,924    --->   (ignored)

Note: files don't have to be in same path as here!

I hope that I described everything... smile


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

Offline

#2 2013-05-21 21:27

den4b
Administrator
From: den4b.com
Registered: 2006-04-06
Posts: 3,479

Re: Synchronize filenames in different file paths

This is one tricky task... Anyhow, below is a script to get you going.

The script will check for each file, extract the path without the ORIGIN to construct the target path in TARGET. Then, it will enumerate all files in the target path with the same extension as the original file and detect a file that has a different filename but the same size. A dialog is displayed to confirm the paths and when confirmed by user the matching file in TARGET is renamed to match the filename in ORIGIN.

const
  ORIGIN = 'D:\Temp\CopyA\';  // can be 'C:\'
  TARGET = 'D:\Temp\CopyB\';  // can be 'D:\'

var
  VariablePath: WideString;
  TargetPath: WideString;
  TargetMask: WideString;
  TargetRenamePath: WideString;
  Files: TStringsArray;
  I: Integer;
  Prompt: WideString;

begin
  // check that filename matches our origin
  if WideSameText(ORIGIN, WideCopy(FilePath, 1, WideLength(ORIGIN))) then
  begin
    
    // prepare target path, target mask, and target rename path
    VariablePath := WideCopy(WideExtractFilePath(FilePath),
      WideLength(ORIGIN) + 1, WideLength(FilePath) - WideLength(ORIGIN));
    TargetPath := TARGET + VariablePath;
    TargetMask := '*' + WideExtractFileExt(FilePath);
    TargetRenamePath := TargetPath + WideExtractFileName(FilePath);   

    // find all files in target path matching target mask
    SetLength(Files, 0);
    WideScanDirForFiles(TargetPath, Files, False, False, False, TargetMask);
    for I := 0 to Length(Files) - 1 do
    begin
    
      // skip files with identical filename
      if not WideSameText(WideExtractFileName(Files[i]), WideExtractFileName(FilePath)) then
      begin
        // skip files with different sizes
        if WideFileSize(Files[i]) = WideFileSize(FilePath) then
        begin

          // found matching file, confirm and rename
          Prompt := 'Found two matching files:' + #13#13 + FilePath + #13 + Files[i] +
            #13#13 + '...rename matching file to?' + #13#13 + TargetRenamePath;
          if WideDialogYesNo(Prompt) then
          begin
            
            // rename target file
            WideRenameFile(Files[i], TargetRenamePath);
          end;
          
          // stop processing other matching files, just in case
          Break;
        end;
      end;
      
    end;
  end;
end.

Offline

#3 2013-05-22 03:59

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

Re: Synchronize filenames in different file paths

A dialog is displayed to confirm the paths and when confirmed by user the matching file in TARGET is renamed to match the filename in ORIGIN.

That was/could be the issue in case we have a lots of files. Dialog has Yes and No buttons. There is no way to cancel dialog, even cannot close ReNamer. Better solution is to see regular preview in files table, then hit Rename roll

Nevertheless I successfully synchronized filenames. Thank you smile

P.S. Cannot sort list by path and filename so I can see changes more clearly. Is it possible to do that somehow?

D:\Movies\Ali [2001]\Ali[2001]DvDrip[Eng]-prithwi.avi
D:\Movies\Ali [2001]\Ali[2001]DvDrip[Eng]-prithwi.nfo
D:\Movies\Ali [2001]\Ali[2001]DvDrip[Eng]-prithwi.srt
D:\Movies\Ali [2001]\Ali[2001]DvDrip[Eng]-prithwi.sub
D:\Movies\Alice In Wonderland\Alice.in.Wonderland.DVDRip.XviD-DiAMOND.avi
D:\Movies\Alice In Wonderland\Alice.in.Wonderland.DVDRip.XviD-DiAMOND.srt
G:\Movies\Ali [2001]\Ali[2001]DvDrip[Eng]-prithwi.avi
G:\Movies\Ali [2001]\Ali[2001]DvDrip[Eng]-prithwi.nfo
G:\Movies\Ali [2001]\Ali[2001]DvDrip[Eng]-prithwi.srt
G:\Movies\Ali [2001]\Ali[2001]DvDrip[Eng]-prithwi.sub
G:\Movies\Alice In Wonderland\Alice.in.Wonderland.DVDRip.XviD-DiAMOND.avi
G:\Movies\Alice In Wonderland\Alice.in.Wonderland.DVDRip.XviD-DiAMOND.srt

to be

D:\Movies\Ali [2001]\Ali[2001]DvDrip[Eng]-prithwi.avi
G:\Movies\Ali [2001]\Ali[2001]DvDrip[Eng]-prithwi.avi
D:\Movies\Ali [2001]\Ali[2001]DvDrip[Eng]-prithwi.nfo
G:\Movies\Ali [2001]\Ali[2001]DvDrip[Eng]-prithwi.nfo
D:\Movies\Ali [2001]\Ali[2001]DvDrip[Eng]-prithwi.srt
G:\Movies\Ali [2001]\Ali[2001]DvDrip[Eng]-prithwi.srt
D:\Movies\Ali [2001]\Ali[2001]DvDrip[Eng]-prithwi.sub
G:\Movies\Ali [2001]\Ali[2001]DvDrip[Eng]-prithwi.sub
D:\Movies\Alice In Wonderland\Alice.in.Wonderland.DVDRip.XviD-DiAMOND.avi
G:\Movies\Alice In Wonderland\Alice.in.Wonderland.DVDRip.XviD-DiAMOND.avi
D:\Movies\Alice In Wonderland\Alice.in.Wonderland.DVDRip.XviD-DiAMOND.srt
G:\Movies\Alice In Wonderland\Alice.in.Wonderland.DVDRip.XviD-DiAMOND.srt


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

Offline

Board footer

Powered by FluxBB