TSearchrec - renaming files with the same extension in a folder

Hi,

I'm trying to use TSearchrec so I can develop a small program for renaming files with the same extension (ex.: .jpg) in a folder. I got to rename the files using the function RenameFile but I need to rename it according to some file properties like LAST MODIFIED, FILE SIZE as the following example:

if i have:

photo_1.jpg       last mod.  11/07/2004
photo_2.jpg       last mod.  11/11/2004
photo_3.jpg       last mod.  11/02/2004

I need to order these files by the field last modified so I would have:

photo_3.jpg       last mod.  11/02/2004
photo_1.jpg       last mod.  11/07/2004
photo_2.jpg       last mod.  11/11/2004

And then i would rename those files like the following, using FindFirst(path,faAnyFile,searchrec) and FindNext(searchrec) and RenameFile:

photo_1.jpg       last mod.  11/02/2004
photo_2.jpg       last mod.  11/07/2004
photo_3.jpg       last mod.  11/11/2004

I hope my explanation was clear. I have the program working right now but I don't know how it is renaming the files, if by name or size or last modified date, what I need is to give the user this choice.

Thanks in advance for the help.

MVM_BR



MVM_BRAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Wim ten BrinkSelf-employed developerCommented:
Let me see... First you build a list of records containing filename, filesize and date. Then you sort this list (List1) by size or by date. You create a second list (List2) with the filenames only and you sort this by name. Now, the first item in List1 should be renamed to the first name in List2. The second file in List1 should be renamed to the second name in List2. Etc. However, this will create a minor problem since you would be trying to rename a file to an existing filename. So you need to rename the files twice!
Now, to do this, rename the files from List1[I].Filename to List2[I].+Filename+'.tmp'. In other words, when you rename them the first time, add some more characters to the name in List2. Once you're done renaming all files, you would rename them to their original names by walking through List2 again.

With your example, ordered by date:
photo_1.jpg       last mod.  11/07/2004
photo_2.jpg       last mod.  11/11/2004
photo_3.jpg       last mod.  11/02/2004

List1 would be:
photo_3.jpg       last mod.  11/02/2004
photo_1.jpg       last mod.  11/07/2004
photo_2.jpg       last mod.  11/11/2004

List2 would be:
photo_1.jpg
photo_2.jpg
photo_3.jpg

List1 is assumed to be sorted correctly already.

Now, the files are renamed for the first time, resulting in:
photo_1.jpg.tmp       last mod.  11/02/2004
photo_2.jpg.tmp       last mod.  11/07/2004
photo_3.jpg.tmp       last mod.  11/11/2004

And then renamed for a second time:
photo_1.jpg       last mod.  11/02/2004
photo_2.jpg       last mod.  11/07/2004
photo_3.jpg       last mod.  11/11/2004

That should be just it...
Now, for sorting List1 I would advise to use a TList and fill it with records of this structure:
type
  PFileRecord = ^TFileRecord;
  TFileRecord = record
    Name: string;
    Size:Int64;
    Date:TDateTime;
  end;
Make sure you also write an appopiate compare routine that the sort routine of the TList will need.
For List2 you could use a simple TStringList since all you need to sort are string names...

I hope this is clear...
0
MVM_BRAuthor Commented:
Thanks for the answer. It is very clear but I still have problems understanding how to sort the files from the selected folder. I was doing the following:

var
   searchrec:TSearchrec;
   path: string;
   found,s: integer;

begin
 path:='c:\teste\*.jpg';
 found:=FindFirst(path,faAnyFile,searchrec);
 s:=1;
 while found=0 do begin
    if not RenameFile(dir+searchrec.name,dir+pref+inttostr(s)+ext2) then
     raise Exception.Create('Error: not possible to rename file);
    s:=s+1;
    found:=FindNext(searchrec);
   end
 else
  begin
   Application.messagebox(no files to rename','Atenção',
                          MB_IconExclamation + MB_OK);
  end;
  FindClose(searchrec);
end;


And you said i should use:

type
  PFileRecord = ^TFileRecord;
  TFileRecord = record
    Name: string;
    Size:Int64;
    Date:TDateTime;
  end;

But I don't know how. Is there a way to do it using my routine above? or how to sort using you sugestion?

0
Wim ten BrinkSelf-employed developerCommented:
Actually, I suggest to use a TList object. Something like:

type
  PFileRecord = ^TFileRecord;
  TFileRecord = record
    Name: string;
    Time: Integer;
    Size: Integer;
  end;

function CompareFileRecord(Item1, Item2: Pointer): Integer;
begin
  // Used to sort the list on date/time first, name second. Don't worry about converting the date. No need for this.
  if (PFileRecord(Item1).Time < PFileRecord(Item2).Time) then begin
    Result := -1;
  end
  else if (PFileRecord(Item1).Time > PFileRecord(Item2).Time) then begin
    Result := 1;
  end
  else if (PFileRecord(Item1).Name < PFileRecord(Item2).Name) then begin
    Result := -1;
  end
  else if (PFileRecord(Item1).Name > PFileRecord(Item2).Name) then begin
    Result := 1;
  end
  else begin
    Result := 0;
  end;
end;

procedure ListFiles;
var
  SearchRec: TSearchRec;
  List: TList;
  Item: PFileRecord;
  I: Integer;
  Filenames: TStringList;
begin
  // Create the lists.
  List := TList.Create;
  Filenames := TStringList.Create;
  // Fill the lists.
  if (FindFirst('D:\Images\Sandra\*.jpg', faAnyfile - faDirectory, SearchRec) = 0) then begin
    repeat
      New(Item);
      Item.Name := SearchRec.Name;
      Item.Time := SearchRec.Time;
      Item.Size := SearchRec.Size;
      List.Add(Item);
      Filenames.Add(SearchRec.Name);
    until (FindNext(SearchRec) <> 0);
    FindClose(SearchRec);
  end;
  // Sort the lists.
  List.Sort(CompareFileRecord);
  Filenames.Sort;
  // Process the lists. (Renaming, for example. But here I just write them.
  for I := 0 to Pred(List.Count) do begin
    Item := List.Items[I];
    WriteLn(Filenames[I], '': 20 - Length(Filenames[I]), ' ', Item.Name, '': 20 - Length(Item.Name), Item.Time: 8, ' ', Item.Size);
  end;
  // Clean up the lists.
  for I := 0 to Pred(List.Count) do begin
    Item := List.Items[I];
    Dispose(Item);
  end;
  // Free the lists.
  Filenames.Free;
  List.Free;
end;

In above code, I'm just writing the sorted files to the output console. (Because I tested it in a console application.) You should rename the files in this phase, though. Remember that the filenames already exist.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.