Solved

ordering of files in a FileListBox

Posted on 2011-03-05
7
1,312 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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

777 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