Link to home
Start Free TrialLog in
Avatar of ChLa
ChLaFlag for United States of America

asked on

ordering of files in a FileListBox

Hi,
I need to know how the files are listed in a filelistbox. Are they always in alphebetical order, or are they in the same order the files are added to the folder ? Can this order be changed, for example, to the order of the creation dates ?

Right now the folders I am using my two new programs with, have all three things going for them. They were added to their folders in the order of creation, which is also the alphabetical order. I need to know what will happen if I use these programs on a folder whose files are in some other order.

Thank you.
Avatar of jimyX
jimyX

FileListBox reads the files by sorting them by their name.
Avatar of ChLa

ASKER

OK, so a filelistbox is populated in alphabetical order. If the filelistbox remains open and is not repopulated, and some files are renamed, does the file order stay the same in the listbox, or are they re-ordered ?
You can add ShellChangeNotifier and add the code below on files change:
procedure TForm1.ShellChangeNotifier1Change;
begin
  FileListBox1.Update;
end;

Open in new window


Or use ShellListView that has AutoRefresh property.

FileListBox is always sorted alphabetically even if a new item is added

If you want a different sort, you have to implement it yourself
Avatar of ChLa

ASKER

Here's the thing,
I have written a program that renames and renumbers files,.I have used it successfully a few times on folders containing 3500 files each. I use a while-do loop to run through the files until all the files have been renamed. I retrieve the file names like this: name = FileListBox1.Items.Strings[filenumber], and then rename it. The file number increments and I go to the next name until filenumber = FileListBox1.Items.Count - 1. What I understand now is that if I rename a file so it is no longer in the same postion alphabetically, it will move in the FileListBox. For example, If I rename the first file, FileListBox1.Items.Files[0], from A0001.tif, to Z0001.tif, it would move to the end of the list and the third file would become FileListBox.Items.Strings[1], the second file on the list. Now my program would rename the third file, A0003.tif to Z0002.tif, thinking it was the second file, and so on. Now the files would be out of order, incorrectly numbered, and only every other file would be renamed. This would be a disaster.
What should I do to make the program bulletproof ? Is there a way to make the FileListBox freeze the ordering process while my program runs ? Should I copy the filenames from the FilelLstBox to something else, another kind of listbox, or an array for example, so the files will stay in the same order while being renamed ? Or do I need to make sure the new filename will never take the files out of order ? Generally, I want the files in the list box to be listed alphabetically, but to stay in the same order even though they may now be out of order allphabetically, until the renaming is done and I refresh the FileListBox.

If you call BeginUpdate and EndUpdate, the list will not be modified until you are done, this will e even faster

FileListBox1.Item.BeginUpdate;
try
  //do your loop here
finally
  FileListBox1.Item.EndUpdate;
end;

But my recommendation is to save the list to a temporary list then work from that eg
var
  TempList: TStringList;
begin
  TempList := TStringList.Create;
  try
    TempList.Assign(FileListBox.Items);
    FileListBox1.Item.BeginUpdate;
   try
      //here do your loop accessing the TempList
     while filenumber < TempList.Count do
     begin
           FileName := TempList.Items.Strings[filenumber];

           //your rename code
         Inc(filenumber);
      end;
   finally
     //refresh the list box then endupdate
     FileListBox1.Item.EndUpdate;
   end;
  finally
    FreeAndNil(TempList);
  end;
end;
ASKER CERTIFIED SOLUTION
Avatar of jimyX
jimyX

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial