Solved

Create a Dir Listing as a Text File

Posted on 1997-07-27
4
172 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

747 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

7 Experts available now in Live!

Get 1:1 Help Now