Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

TSearchrec - renaming files with the same extension in a folder

Posted on 2004-11-14
6
Medium Priority
?
1,130 Views
Last Modified: 2012-06-22
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



0
Comment
Question by:MVM_BR
  • 2
3 Comments
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 12580439
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
 

Author Comment

by:MVM_BR
ID: 12585382
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
 
LVL 17

Accepted Solution

by:
Wim ten Brink earned 800 total points
ID: 12589165
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Integration Management Part 2
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question