Board index » delphi » File Listing Within a Directory

File Listing Within a Directory

Can someone please post the command or list of commands to allow a
program to write all the file names of files within a certain
directory.  Like the DIR command works under DOS.

Thanks A lot

Gavin Carter

 

Re:File Listing Within a Directory


Quote
Gavin Carter wrote:
> Can someone please post the command or list of commands to allow a
> program to write all the file names of files within a certain
> directory.  Like the DIR command works under DOS.

Well, for starters, there's no such thing as a "command" per se in TP,
it's all statements and expressions; but that's probably nitpicking.
:-)

Here's something to start off with. You can modify this procedure as
needed:

uses
  dos; {requires this unit}

procedure listfiles (homedir : pathstr);

var
  s : searchrec;

begin
  findfirst (homedir,AnyFile,s);
  while DosError=0 do
    begin
      writeln (s.name);
      findnext (s);
    end;
end;

For more details, look up findfirst(), findnext, and searchrec in the
manuals.

Quote
> Thanks A lot

> Gavin Carter

--
Scott Earnest                      | _,-""-_,-""-_,-""-_,-""-_,-""-_,-"
|
set...@ix.netcom.com (primary)     | We now return you to our regularly
|
siny...@{*word*104}space.org (alternate) | scheduled chaos and mayhem. . . .
|

Re:File Listing Within a Directory


Quote
> Can someone please post the command or list of commands to allow a
> program to write all the file names of files within a certain
> directory.  Like the DIR command works under DOS.

   Sure: FindFirst and FindNext, along with SearchRec.

Re:File Listing Within a Directory


In article <3294A373.4...@dial.pipex.com> of Thu, 21 Nov 1996 18:46:11
in comp.lang.pascal.borland, Gavin Carter <gavin_car...@dial.pipex.com>
wrote:

Quote
>Can someone please post the command or list of commands to allow a
>program to write all the file names of files within a certain
>directory.  Like the DIR command works under DOS.

See the online help for FindFirst, and modify as required.

--
John Stockton, Surrey, UK.  J...@merlyn.demon.co.uk  Turnpike v1.12  MIME
    http://www.merlyn.demon.co.uk/programs/listdirs.pas

Re:File Listing Within a Directory


Use the FindFirst to find the first file and/or directory and the FindNext
procedure to find all the rest of the files present.
Something like this :

Uses
  Dos ;

Var
  S : SearchRec ;

Begin
  FindFirst ('C:\*.*', AnyFile, S) ;
  While DosError = 0 Do
    Begin
      If S.Attr = Directory
        Then WriteLn ('Directory : ', S.Name)
        Else If S.Attr <> VolumeID
               Then WriteLn ('File      : ', S.Name) ;
      FindNext (S)
    End
End .

--
           ,,,
          (o o)
 -----oOO--(_)--OOo-----
|       ASS-Ware        |
|    is watching you    |
 -----------------------

Gavin Carter <gavin_car...@dial.pipex.com> wrote in article
<3294A373.4...@dial.pipex.com>...

Quote
> Can someone please post the command or list of commands to allow a
> program to write all the file names of files within a certain
> directory.  Like the DIR command works under DOS.

> Thanks A lot

> Gavin Carter

Re:File Listing Within a Directory


Quote
Gavin Carter <gavin_car...@dial.pipex.com> wrote:
>Can someone please post the command or list of commands to allow a
>program to write all the file names of files within a certain
>directory.  Like the DIR command works under DOS.
>Thanks A lot
>Gavin Carter

This program does it EXACTLY like the DOS DIR command
Clif

Program ShowDirectoryDemo;
{ Written in Turbo v.6.0     <clifp...@airmail.net>  Nov 24, 1996
When you wish a directory displayed or redirected EXACTLY the way it
is shown in DOS, you do not need the added complications related to
DOS TPU FindFirst and FindNext. However, in a real program, the
HeapMax size may become too small for the rest of the program.  }

Uses CRT,  (* For ClrScr, TextColor *)
     DOS;  (* Used only for EXEC. All other disk commands do not
              require DOS *)
CONST
filename = 'DIRCOPY.TXT';
bel = Chr(7);   ent = Chr(13);    esc = Chr(27);

VAR
f:Text;
ch:Char;
DefaultDir, DirName, DirLine:String[80];
ok:Boolean;

Begin
         (* $M stack, heapmin, heapmax required for DOS EXEC *)
     {$M 16384, 0, 100000}  (* If heapmax too large, no directory *)
     GetDir(0, DefaultDir); (* Gets current directory path\name *)
     ClrScr;

Repeat
     ok := true;
     Writeln('Enter Q to quit Program');
     Write('Just <Enter> for ', DefaultDir, '.  Path\name for new: ');
     Readln(DirName);
     If (Length(DirName) = 1) AND (DirName[1] in ['Q', 'q']) then
EXIT;

     If DirName <> '' then
     Begin
          {$I-}
          ChDir(DirName);    (* attempt change to desired dir *)
          {$I+}
          ok := IOResult = 0;
          If not ok then Writeln('No such DIR', bel);
     End;

     If ok then
     Begin
          (* following shows dir a page at a time *)
          EXEC('\COMMAND.COM', '/C DIR/P') ;   (* DOS TPU *)
          Writeln;

          TextColor(14);  (* yellow *)
          Writeln('The above DIR will now be stored on disk.');
          Write('Press Enter ');  Readln;

          (* following redirects dir to file *)
          EXEC('\COMMAND.COM', '/C DIR > ' + filename);
          Writeln;  Writeln;

          Writeln('Verifying DIRCOPY.TXT');
          Writeln('Press Enter');  Readln;
          Assign(f, filename);
          Reset(f);
          While not eof(f) Do
          Begin
              Readln(f, DirLine);
              Writeln(DirLine);
          End;
          Close(f);
          Erase(f);

          TextColor(LightGray);
          Writeln; Writeln('Finished showing DIRCOPY.TXT');
          Writeln('DIRCOPY.TXT deleted. Press Enter');
          Readln;    Writeln;
     End;
     Writeln; Writeln;
     ChDir(DefaultDir);  (* changes dir back to original default *)

Until UpCase(DirName[1]) = 'Q';
End.

Other Threads