[comp.lang.pascal] Reading a directory from Turbo.

huling@cs.odu.edu (07/20/89)

I was wondering if someone could help me out with something.  I need a 
turbo pascal procedure that will read a disk directory, preferably into an
array of strings.  Any help would be appreciated. Thanks in advance.


				-Wayne(huling@cs.odu.edu)

ts@chyde.uwasa.fi (Timo Salmi LASK) (07/21/89)

In article <9559@xanth.cs.odu.edu> huling@cs.odu.edu () writes:
>
>I was wondering if someone could help me out with something.  I need a 
>turbo pascal procedure that will read a disk directory, preferably into an
>array of strings.  Any help would be appreciated. Thanks in advance.

See PC World, April 1989, page 154, or almost any good guide on
Turbo Pascal.  There are many around starting from O'Brien's
excellent Turbo Pascal, the Complete Reference. 

...................................................................
Prof. Timo Salmi                                (Site 128.214.12.3)
School of Business Studies, University of Vaasa, SF-65101, Finland
Internet: ts@chyde.uwasa.fi Funet: vakk::salmi Bitnet: salmi@finfun
 

mitch@arcturus.UUCP (Mitchell S. Gorman) (07/22/89)

huling@cs.odu.edu writes:


>I was wondering if someone could help me out with something.  I need a 
>turbo pascal procedure that will read a disk directory, preferably into an
>array of strings.  Any help would be appreciated. Thanks in advance.

	I believe there is a program which does this included as an examplu
with versions 4 and 5... I don't recall offhand what the name of this
program is, but if you do a string search on all your .pas files that came
with the compiler for 'directory', you should be able to find it.
	
	Mitch @ Rockwell, Anaheim

	mitch@arcturus.UUCP

Disclaimer:	I said WHAT???

taylorj@yvax.byu.edu (07/22/89)

If you're using version 4.0 or later, look up the FindFirst and FindNext
functions in your manual.  If you're using an older version of Turbo, upgrade!
(Seriously, you can do it with DOS calls.  Check your DOS Tech manual.)

Jim Taylor
Microcomputer Support for Curriculum   |
Brigham Young University               |   Bitnet: taylorj@byuvax.bitnet
101 HRCB, Provo, UT  84602             |   Internet: taylorj@yvax.byu.edu

winfave@dutrun.UUCP (Alexander Verbraeck) (07/24/89)

In article <721taylorj@yvax.byu.edu> taylorj@yvax.byu.edu writes:
>If you're using version 4.0 or later, look up the FindFirst and FindNext
>functions in your manual.  If you're using an older version of Turbo, upgrade!
>(Seriously, you can do it with DOS calls.  Check your DOS Tech manual.)

If anyone wants the TP 3.0 or TP 4.0 sources to read a directory (yes,
using DOS calls), I have them here and it's no problem to e-mail them.
Just drop me an e-mail.

---------------------------------------------------------------------
Alexander Verbraeck                            e-mail:
Delft University of Technology                 winfave@hdetud1.bitnet
Department of Information Systems              winfave@dutrun.uucp
PO Box 356, 2600 AJ  The Netherlands
---------------------------------------------------------------------

raj@pic.ucla.edu (Shiladitya Raj Chaudhury) (07/25/89)

I thought I would just add my two bits worth: Try the Filemanager example
program provided as part of the Turbo Tutor for 4.0. That should do it!

S. Raj Chaudhury
raj@pic.ucla.edu
Dept. of Physics
UCLA, LA CA 90024

natec@infohh.rmi.de (NATEC Institut GmbH) (07/25/89)

Most simple solution is perhaps:

Program Temp;
{$M $4000, 0, 0}
uses Dos;
begin
   Exec('C:\COMMAND.COM','/C DIR >TEMPFILE');
end.


...read that TEMPFILE afterwards.


Dr Gerd C Schmerse  | UUCP: natec@infohh.rmi.de |     ___    
NATEC Institut      | 49 40 88300170  (fax)     |    /o o\   
Hamburg, W.Germany  | 49 40 88300124  (voice)   | -m-"-U-"-m-

winfave@dutrun.UUCP (Alexander Verbraeck) (07/26/89)

I got a number of e-mails asking for the sources for the directory
procedure. So: here it is!
Here are the Turbo Pascal version 3 and Turbo Pascal version 4 or 5 
sources for printing a directory.


TURBO 4 / 5 SOURCE
----------------------------------------------------------------------

procedure ViewDir(MatchPtrn : string[64]; FromLine : integer);

var
  DirInfo  : SearchRec;
  Line     ,
  Position : integer;

begin
  LowVideo;
  GotoXY(1,FromLine); ClrEos;
  Line:=FromLine; Position:=1;
  FindFirst(MatchPtrn,$37,DirInfo);
  if DosError<>0 then
    writeln('*** NO FILES FOUND ***')
  else
  while (DosError=0) and (Line<21) do
  begin
    GotoXY(Position,Line);
    if DirInfo.Attr=$10 then HighVideo;
    write(DirInfo.Name);
    LowVideo;
    Position:=Position+16;
    if Position>65 then
    begin
      Line:=Line+1;
      Position:=1;
    end;
    FindNext(DirInfo);
  end;
  NormVideo;
end;

----------------------------------------------------------------------


TURBO 3 SOURCE
----------------------------------------------------------------------

{ Source: TURBO Pascal Program Library
          Tom Rugg, Phil Feldman
          Que Corporation, 1986
          Indianapolis
          ISBN 0-88022-244-1
  Pages:  135 - 145
}

procedure ViewDir(MatchPtrn : string[64]; FromLine : integer);

type
  UserSpec   = string[64];
  Registers  = record
                 AX,BX,CX,DX,BP,SI,DI,DS,ES,Flags : integer;
               end;
  FileName   = string[13];
  DTAPointer = ^DTARecord;
  DTARecord  = record
                 DOSReserved : array[1..21] of byte;
                 Attribute   : byte;
                 FileTime    ,
                 FileDate    ,
                 SizeLow     ,
                 SizeHigh    : integer;
                 FoundName   : array[1..13] of char;
               end;

const
  NUL        = ^@;
  SeekAttrib = $16;

var
  TransferRec : DTAPointer;
  RetName     : FileName;
  FilSize     : Real;
  Count       : integer;
  NoFind      ,
  LastFile    ,
  SubDirec    : boolean;



procedure PointDTA(var DTARec : DTAPointer);

const
  GetDTA = $2F00;

var
  Regs : Registers;

begin
  Regs.AX := GetDTA;
  MsDos(Regs);
  DTARec := Ptr(Regs.ES,Regs.BX);
end;


function SizeOfFile(HiWord, LoWord : integer) : real;

var
  BigNo, Size : real;

begin
  BigNo := (MaxInt*2.0) + 2;
  if HiWord < 0 then
    Size := (BigNo+HiWord)*BigNo
  else
    Size := HiWord*BigNo;
  if LoWord >= 0 then
    Size := Size+LoWord
  else
    Size := Size+(BigNo+LoWord);
  SizeOfFile := Size;
end;



procedure FindFirst(Pattern:UserSpec; var Found:FileName; var Size:real;
                    var NoMatch:boolean; var LastOne:boolean;
                    var SubDir:boolean);

const
  FindFirst   = $4E00;

type
  ASCIIZ      = array[1..64] of char;

var
  FileSpec : ASCIIZ;
  Regs     : Registers;
  PosInStr ,
  Count    : integer;
  FoundLen : byte absolute Found;

begin
  for PosInStr:=1 to length(Pattern) do
    FileSpec[PosInStr] := Pattern[PosInStr];
  FileSpec[length(Pattern)+1] := NUL;
  with Regs do
  begin
    DS := Seg(FileSpec);
    DX := Ofs(FileSpec);
    CX := SeekAttrib;
    AX := FindFirst;
    MsDos(Regs);
    if (Flags and 1) > 0 then
    begin
      case AX of
        2 : begin  { No match }
              NoMatch := true;
              LastOne := true;
            end;
       18 : begin  { No more files }
              NoMatch := false;
              LastOne := true;
            end;
      else
        Fout('Can''t interpret error return code');
        Exit
      end;  { case }
    end
    else
    begin  { No error return code }
      NoMatch := false;
      LastOne := false;
    end;
  end;  { with Regs }

  if (not NoMatch) then
  with TransferRec^ do
  begin
    Found := FoundName;
    Count := 0;
    while Found[Count] <> NUL do Count := Count+1;
    FoundLen := Count;
    for Count := Length(Found)+1 to 13 do Found := Found+' ';
    if (Attribute and SeekAttrib) <> 0 then
      SubDir := true
    else
      SubDir := false;
    if not SubDir then
      Size := SizeOfFile(SizeHigh,SizeLow)
    else
      Size := 0.0;
  end;  { with TransferRec }
end;



procedure FindNext(var Found:FileName; var Size:real;
                   var LastOne:boolean; var SubDir:boolean);

const
  FindNext   = $4F00;

var
  Regs     : Registers;
  Count    : integer;
  FoundLen : byte absolute Found;

begin
  with Regs do
  begin
    AX := FindNext;
    MsDos(Regs);
    if (Flags and 1) > 0 then
    begin
      if AX=18 then LastOne := true
      else
      begin
        writeln(^G'Can''t interpret error return code');
        Halt;
      end;
    end
    else LastOne:=false;
  end;  { with Regs }

  with TransferRec^ do
  begin
    Found := FoundName;
    Count := 0;
    while Found[Count] <> NUL do Count := Count+1;
    FoundLen := Count;
    for Count := Length(Found)+1 to 13 do Found := Found+' ';
    if (Attribute and SeekAttrib) <> 0 then
      SubDir := true
    else
      SubDir := false;
    if not SubDir then
      Size := SizeOfFile(SizeHigh,SizeLow)
    else
      Size := 0.0;
  end;  { with TransferRec }
end;


{ Start of ViewDir }

begin
  GotoXY(1,FromLine); ClrEos;
  NormVideo;
  Count:=0;
  PointDTA(TransferRec);
  FindFirst(MatchPtrn,RetName,FilSize,NoFind,LastFile,SubDirec);
  if NoFind or LastFile then
    writeln('*** NO FILES FOUND ***')
  else
  begin
    while not LastFile do
    begin
      if SubDirec then LowVideo;
      if Pos('.',RetName)=0 then write(RetName,'  ') else
        write(copy(copy(RetName,1,Pos('.',RetName)-1)+'        ',1,8),
              copy(RetName,Pos('.',RetName),4),'  ');
      Count:=Count+1;
      NormVideo;
      if (Count mod 5) = 0 then writeln;
      FindNext(RetName,FilSize,LastFile,SubDirec);
    end;
  end;
  LowVideo;
end;

----------------------------------------------------------------------


Call of procedure ViewDir :

----------------------------------------------------------------------

begin
  { ... }
  GotoXY(1,7);
  writeln('PRN FILES IN DIRECTORY \TMP: : ');
  ViewDir('\TMP\*.PRN',9);
  { ... }
end;

----------------------------------------------------------------------


If you have any questions, please ask.
Sincerely,

---------------------------------------------------------------------
Alexander Verbraeck                            e-mail:
Delft University of Technology                 winfave@hdetud1.bitnet
Department of Information Systems              winfave@dutrun.uucp
PO Box 356, 2600 AJ  The Netherlands
---------------------------------------------------------------------

andyross@ddsw1.MCS.COM (Andrew Rossmann) (07/29/89)

In article <563@infohh.rmi.de> natec@infohh.rmi.de (NATEC Institut GmbH) writes:
>Most simple solution is perhaps:
>
>Program Temp;
>{$M $4000, 0, 0}
>uses Dos;
>begin
>   Exec('C:\COMMAND.COM','/C DIR >TEMPFILE');
>end.
>
>...read that TEMPFILE afterwards.
>
>Dr Gerd C Schmerse  | UUCP: natec@infohh.rmi.de |     ___    

  A more correct version would be:

Program test;
{$M 1024,0,0}
Uses Dos;
begin
  SwapVectors;
  ExecGetEnv('COMSPEC'), '/C DIR > TEMPFILE');
  SwapVectors
end.

  Not EVERYONE puts COMMAND.COM in the root directory. Many people prefer
to have it elsewhere. The above will only work under TP 5 and 5.5. The
GetEnv function was not in TP3 or  4.

  andyross@ddsw1.MCS.COM