#1 2007-12-29 21:37

bobyang
Member
Registered: 2007-05-08
Posts: 33

Find file in the different drive's folder

I know this is very strange but I just wonder if this is doable.

situation:

1. I have 2 HDs with the same file structure because one harddisk is almost full.
2. I have files like "d:\A-file1.dat" "d:\A-file2.dat" "d:\B-file1.dat"..etc
3. after running renamer, they will be saved to  "d:\A\A-file1.dat" "d:\A\A-file2.dat" "d:\B\B-file1.dat".. If the file is there, I will get an warming not able to rename it.

Everything works very well until I start having more than one harddisk, because I found a lot of dupllicate files in the different drive. I didn't know they were there already so I download and do it again. Therefore, I am thinking while it is renaming, is it possible to detect another drive's folder to see if the file is there already?

eg.

d:\A-file1.dat   will detect d:\A\ and e:\A\    if not in both folders, rename it and save to the right folder
d:\A-file2.dat   will detect d:\A\ and e:\A\    if not in both folders, rename it and save to the right folder
d:\B-file1.dat   will detect d:\B\ and e:\B\    if not in both folders, rename it and save to the right folder


thank you!!


currently, I am using this script which only detects files in d:\ but not e:\

var
  Files: TStringsArray;

procedure Add(const S: WideString);
begin
  SetLength(Files, Length(Files)+1);
  Files[Length(Files)-1] := S;
end;

function Exists(const S: WideString): Boolean;
var I: Integer;
begin
  Result := False;
  for I:=0 to Length(Files)-1 do
    if WideSameText(Files[i], S) then
      begin Result := True; Break; end;
end;

var
  NewName, Path, Name, Ext: WideString;
  Counter: Integer;

begin
  Counter := 2;
  Ext := WideExtractFileExt(FileName);
  Name := WideExtractBaseName(FileName);
  Path := WideExtractFilePath(FilePath);  
  NewName := Name;
  //while Exists(Path + NewName) or WideFileExists(Path + NewName + Ext) do
 while Exists(Path + NewName)  do
  //while WideFileExists(Path + NewName + Ext) do
  begin
    NewName := Name +'(' + IntToStr(Counter)+')'; 
    Counter := Counter + 1;
  end;
  Add(Path + NewName);
  FileName := NewName + Ext;
end.

Last edited by bobyang (2008-01-07 22:02)

Offline

#2 2008-01-07 18:00

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

Re: Find file in the different drive's folder

It is not completely clear to me what you want to do with another hard drive. So I'll just answer your question:

Is it possible to detect another drive's folder to see if the file is there already?

Yes, it is possible. Within PascalScript, there is a function called WideFileExists(). You can use the path of the currently processed file, modify it (change the drive letter), and check if the modified path exists. Here is "pseudo" code for this operation (not tested):

const
  Drives = 'CDEFGHI';

var
  I: Integer;
  NewPath: WideString;

begin
  NewPath := WideExtractFilePath(FilePath) + FileName;
  for I:=1 to Length(Drives) do
  begin
    NewPath := WideCopy(Drives, I, 1) +
      WideCopy(NewPath, 2, Length(NewPath));
    if WideFileExists(NewPath) then
    begin
      // do something with new path //
    end;
  end;
end.

Offline

#3 2008-01-07 22:01

bobyang
Member
Registered: 2007-05-08
Posts: 33

Re: Find file in the different drive's folder

thank you. could you help me to merge two scripts because I don't understand pascal script well. or any reference you can refer me to? (I understand VB, C#, java, perl.. but it looks very difference) thank you!

the currect script I have is about rename with the folder name. eg.
d:\auther1-song1.mp3  -> d:\auther1\auther1-song1.mp3 
d:\auther2-song1.mp3  -> d:\auther2\auther2-song1.mp3 

d:\ is full now so I must save all new ones to e:\. However, "auther1-song1.mp3"  is in "d:" NOT in "e:" so if I get the same file again and e:\, the file get renamed and save to e:\auther1\auther1-song1.mp3  without any problems. It will be good if it can detect the file is already in "d:\auther1\" so the files will not be rename and it will come up with and "!" mark if the file cannot be renamed.

thank you!

Offline

#4 2008-01-16 12:58

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

Re: Find file in the different drive's folder

Sorry, but I'm completely confused...

Your current script (slightly altered "serialize duplicates") simply appends increasing numeric value to the end of the filenames which have duplicates in the list. It doesn't actually check for existence of physical files. I don't understand what exactly you need to check on another drive, and what you need to do with the file.

If you can write a "pseudo" code (complete sequence of actions), I will be able to convert it into pascal script.

Offline

#5 2008-01-16 23:38

bobyang
Member
Registered: 2007-05-08
Posts: 33

Re: Find file in the different drive's folder

I am really sorry. I think I may paste a wrong code.
here is what I try to do.

var current_file_fullPath         //eg,  d:\mp3\auther1-song1.mp3; d:\mp3\auther2-song1.mp3  --- this is the file listed in the renamer
var folder_to_check_fullPath[]        // eg, d:\mp3\; e:\mp3

string method_get_FileName(string current_file_fullPath2) {
         var fileNameOnly=split(current_file_fullPath2,"\")   // so fileNameOnly[last] is filename, eg "auther1-song1.mp3"
         return fileNameOnly[last]   //"auther1-song1.mp3"
     }

string method_get_Folder_Name_from_FileName(string current_file_fullPath3) {
         var autherName = split(method_get_FileName(current_file_fullPath3) ,"-")    // so authername[0] is "auther1"
         return autherName[0]   // "auther1"
     }



boolean method_check_File_Exist {
      foreach current_file_fullPath   //eg, d:\mp3\auther1-song1.mp3
        {
       for (folder_to_check_fullPath[0] to folder_to_check_fullPath[last])   // check file d:\mp3\ and e:\mp3\
             {
               if ( Exsit (folder_to_check_fullPath[i] + method_get_Folder_Name_from_FileName(current_file_fullPath) + "\" +  method_get_FileName(current_file_fullPath) ) )
                 {
                   return ture   //auther1-song1.mp3 is found in d:\mp3\auther1\auther1-song1.mp3 or e:\mp3\auther1\auther1-song1.mp3
                  }
               else
                  {
                     return false  //file are not found in d:\auther1\; e:\auther1\  -- OK to rename
                  }
              }
         }
     }

Last edited by bobyang (2008-01-16 23:41)

Offline

#6 2008-01-19 13:53

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

Re: Find file in the different drive's folder

After looking through your pseudo code, I came up with this:

For each file in the table (e.g. c:\folder\auther1-song1.mp3), it will create an array of folders for all drives (e.g. c:\folder, d:\folder, e:\folder, etc), then for each one of these folders it will create new path (e.g. c:\folder\auther1\auther1-song1.mp3, d:\folder\auther1\auther1-song1.mp3, etc), then it will check the existence of every new path. Those files which will have at least 1 new generated path existing, will be prefixed with the "Prefix" text, i.e. put some forbidden characters into it to make files fail to rename.

NOTE: Test it before using on the real files!

NOTE: You'll need to get latest dev version: ReNamerBeta.zip

const
  Drives = 'CDEFGHIKLMNOPQRSTVXYZ';
  Prefix = '<!> ';

function PathInAllDrives(const Path: WideString): TStringsArray;
var I: Integer;
begin
  SetLength(Result, Length(Drives));
  for I:=1 to Length(Drives) do
    Result[I-1] := Drives[i] + WideCopy(Path, 2, Length(Path));
end;

function ExtractMyAuthor(const Path: WideString): WideString;
var Position: Integer;
begin
  Result := WideExtractFileName(Path);
  Position := WidePos('-', Result);
  if Position > 0 then
    Result := WideCopy(Result, 1, Position-1)
  else
    Result := '';
end;

function CheckNewFileExists(const Path: WideString): Boolean;
var
  I: Integer;
  Dir, NewPath: WideString;
  AllDirs: TStringsArray;
begin
  Result := False;
  Dir := WideExtractFileDir(Path);
  AllDirs := PathInAllDrives(Dir);
  for I:=0 to Length(AllDirs)-1 do
  begin
    NewPath := AllDirs[i] +'\'+ ExtractMyAuthor(Path) +'\'+ WideExtractFileName(Path);
    if WideFileExists(NewPath) then
    begin
      Result := True;
      Exit;
    end;
  end;
end;

var
  OldPathNewName: WideString;

begin
  OldPathNewName := WideExtractFilePath(FilePath) + FileName;
  if CheckNewFileExists(OldPathNewName) then
    FileName := Prefix + FileName;
end.

Offline

#7 2008-01-19 21:11

bobyang
Member
Registered: 2007-05-08
Posts: 33

Re: Find file in the different drive's folder

thank you very much! you are the best!
I am going to download the new version and try this out. thank you again!!

Offline

Board footer

Powered by FluxBB