Solved

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

Posted on 2002-07-02
5
243 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
[X]
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
5 Comments
 
LVL 7

Accepted Solution

by:
Cynna earned 50 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

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

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

728 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