Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

ordering of files in a FileListBox

Posted on 2011-03-05
7
1,328 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

837 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