Solved

Create a Dir Listing as a Text File

Posted on 1997-07-27
4
174 Views
Last Modified: 2010-04-04
Hi,
      I’m going to put together a program to replace one
at work.  What it does is take about 500 programs with
the names of ####.GL(#is a number) and renames them by removing the Ext part of the name.  Then it moves a set number of files to drive A: and props for another disk
this goes on until all files are copied over.  It also
makes a printout of  the disk directory on drive A: on
each disk. A little history on this program.  I work for
an Aircraft company as a CAD/CAM designer and there are programs for the CNC Mills.  We have to make backups to
keep on the floor so if needed they can be copied into the machines if needed..
   On the existing program I was able to make a text file
to use as a listing of the files to be moved.    
          Comamnd:=Dir C:\Temp\*.* /B >DirList.Doc
Then I user this file to Rename and move the files..
  Is there a way to get this dir listing in Delphi 3 like  the one I have above.  Then I can use it to rename and move the files.  Or if there is a better way to approach this project..

                                       Thanks
                                       Jim Zerbe
0
Comment
Question by:jim zerbe
  • 2
  • 2
4 Comments
 
LVL 4

Accepted Solution

by:
erajoj earned 50 total points
ID: 1339982
There are several ways to do this.
These examples are written on-the-fly ;-)
so there could be minor compilation problems:

1. Do it like you do it today:
   OK := (31 < WinExec(
     'cmd /c Dir C:\Temp\*.* /B >DirList.Doc', SW_HIDE
   ));
 or
   OK := (31 < WinExec(
     'command /c Dir C:\Temp\*.* /B >DirList.Doc', SW_HIDE
   ));

2. Totally programatically:
  procedure SaveDirContentsToFile(
    const Dir, Mask: string; const FileName: string;
    const FilesOnly: Boolean);
  var
    SearcRec  : TSearchRec;
    FileHandle: TextFile;
  begin
    try
      AssignFile(FileHandle, FileName);
      ReWrite(FileHandle);
      if FindFirst(Dir+'\'+Mask, faAnyFile, SearchRec)=0
      then repeat
        with SearchRec do begin
          if (((Attr and faDirectory)=faDirectory) xor FilesOnly)
          then WriteLn(FileHandle, Name);
        end;
      until FindNext(SearchRec)<>0;
      FindClose(SearchRec); // Return allocated memory
      CloseFile(FileHandle);
    except
      MessageBox('Couldn''t save directory to file!');
    end;
  end;

3. Totally programatically, with TStringList:
  procedure SaveDirContentsToFile(
    const Dir, Mask: string; const FileName: string;
    const FilesOnly: Boolean);
  var
    SearcRec  : TSearchRec;
    DirList   : TStringList;
  begin
    try
      DirList := TStringList.Create;
      DirList.Sorted := True;
      if FindFirst(Dir+'\'+Mask, faAnyFile, SearchRec)=0
      then repeat
        with SearchRec do begin
          if (((Attr and faDirectory)=faDirectory) xor FilesOnly)
          then DirList.Add(Name);
        end;
      until FindNext(SearchRec)<>0; // No more dir-entries
      FindClose(SearchRec); // Return allocated memory
      DirList.SaveToFile(FileName); // Use member functionallity
      Dirlist.Free; // Not needed further
    except
      MessageBox('Couldn''t save directory to file!');
    end;
  end;

Hope you can use this.

/// John
0
 

Author Comment

by:jim zerbe
ID: 1339983
Very Good thanks.  I did forget to add one thing will these work in NT work stations..
0
 
LVL 4

Expert Comment

by:erajoj
ID: 1339984
Yes.
In fact only the second example will work on Windows 3.x
since "FindClose" is nonexistant in Delphi 1.x and since
"command" is replaced with "cmd" as the "DOS" command-interpreter
in NT.

Don't hesitate to ask if you run into more problems!

/// John

0
 

Author Comment

by:jim zerbe
ID: 1339985
Thanks for the speedy reply will try it out..
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Is there a maximum column width (in pixels) in a TListView? 22 102
Thread safe  opinion 7 126
Delphi cmd execution 6 55
Controlled Assessment GCSE - desperate help needed 4 75
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…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

929 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

15 Experts available now in Live!

Get 1:1 Help Now