[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Read the directory for file names ( preview, rename, delete).

Posted on 2002-07-02
5
Medium Priority
?
246 Views
Last Modified: 2010-04-04
I am looking for a piece of code that might help me to start with following issues.
The programme must be able
1)To read a directory and list all files in the directory (with particular extension for example only html files).
2)allow the user to select a file for
  Preview, rename and deletion

Thank you in advance

0
Comment
Question by:Cyrus2k
5 Comments
 
LVL 7

Accepted Solution

by:
Cynna earned 150 total points
ID: 7124295
Cyrus2k,

This is one simple way to do it:

1. Place DriveComboBox1, DirectoryListBox1 and FileListBox1 on the form.

2. Place 3 buttons and name them Preview, Rename and Delete

3. Set following properties (from object inspector):
- DriveComboBox1.DirList:=DirectoryListBox1;
- DirectoryListBox1.FileList:=FileListBox1;

4. Set Mask property of FileListBox1; to whatever you like. For your example:
FileListBox1.Mask:='*.htm;*.html;*.mht';

5. Set DirectoryListBox1.Directory (from code) to whatever initial dir you wish

6. When you click on any of the buttons you can access currently selected file by:
   FileListBox1.FileName
.. and do any operation you like with it.



0
 
LVL 17

Expert Comment

by:geobul
ID: 7124326
Hi,
Another way: drop a TListBox and a button on a form. Then:

procedure GetFiles(DirStr, Mask: String; List: TStrings);
var
  DirInfo: TSearchRec;
  R: Integer;
begin
  R := FindFirst(DirStr + Mask, faAnyfile, DirInfo);
  if R = 0 then
  begin
    repeat
      if not (DirInfo.Attr and faDirectory) <> 0 then List.Add(DirStr + DirInfo.Name);
      R := FindNext(DirInfo);
    until R <> 0;
    FindClose(DirInfo);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  GetFiles('C:\MyDir\', '*.htm?', ListBox1.Items);
end;

// write a TListBox.OnClick event handler like:

procedure TForm1.ListBox1Click(Sender: TObject);
begin
  with ListBox1 do begin
    if ItemIndex >= 0 then begin
      // do something with the selected file
      ShowMessage(Items[ItemIndex]);
    end;
  end;
end;

Regards, Geo
0
 
LVL 17

Expert Comment

by:geobul
ID: 7124341
Instead of using ListBox1.OnClick you may add a TPopupMenu with Items [Preview, Rename, Delete]. Set ListBox1.PopupMenu to PopupMenu1 and write PopupMenu events. For example Preview may look like:

procedure TForm1.Preview1Click(Sender: TObject);
begin
  ShellExecute(0, 'open', PChar(ListBox1.Items[ListBox1.ItemIndex]), nil, nil, SW_SHOWNORMAL);
end;

Regards, Geo
0
 
LVL 3

Expert Comment

by:SteveWaite
ID: 7124482
you could use an opendialog, the user selects a file to view or simply renames or deletes fom within the dialog.
place an OpenDialog on the form and something like this to call it

  with OpenDialog1 do
  begin
    InitialDir := 'c:\'; // start dir
    FileName := ''; // file to start with
    DefaultExt := ''; // ext to start with
    Title := 'Select a file to view, rename or delete';
    Filter := 'All files (*.*)|*.*'; // filter for all
    Options := [ofEnableSizing,ofHideReadOnly]; // etc
    if Execute then
    begin
      DisplayMyFileOrWhatever(FileName);
      // etc.
    end;
  end;
0
 
LVL 1

Author Comment

by:Cyrus2k
ID: 7129087
It is a good way of doing this problem.
Thank you
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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…
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…
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.…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

868 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