Solved

ordering of files in a FileListBox

Posted on 2011-03-05
7
1,273 Views
Last Modified: 2012-05-11
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.
0
Comment
Question by:ChLa
  • 3
  • 2
  • 2
7 Comments
 
LVL 24

Expert Comment

by:jimyX
ID: 35043778
FileListBox reads the files by sorting them by their name.
0
 

Author Comment

by:ChLa
ID: 35044347
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 ?
0
 
LVL 24

Expert Comment

by:jimyX
ID: 35044380
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.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 32

Expert Comment

by:ewangoya
ID: 35044424

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

If you want a different sort, you have to implement it yourself
0
 

Author Comment

by:ChLa
ID: 35058395
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.
0
 
LVL 32

Expert Comment

by:ewangoya
ID: 35066136

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;
0
 
LVL 24

Accepted Solution

by:
jimyX earned 50 total points
ID: 35068893
> "does the file order stay the same in the listbox, or are they re-ordered ?"

No, you can go through the FileListBox items and rename safely, it does not re-order until you call "FileListBox1.Update;"
While filenumber <= FileListBox1.Items.Count - 1 do
  begin
    // rename the files
  end;
FileListBox1.Update;

Open in new window

0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now