home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / pascal / turbo55 / tp55 / dirdemo.pas < prev    next >
Pascal/Delphi Source File  |  1989-05-02  |  5KB  |  240 lines

  1. { Turbo Directory }
  2. { Copyright (c) 1985, 1989 by Borland International, Inc. }
  3.  
  4. program DirDemo;
  5. { Demonstration program that shows how to use:
  6.  
  7.     o Directory routines from DOS unit
  8.     o Procedural types (used by QuickSort)
  9.  
  10.   Usage:
  11.  
  12.     dirdemo [options] [directory mask]
  13.  
  14.   Options:
  15.  
  16.     -W      Wide display
  17.     -N      Sort by file name
  18.     -S      Sort by file size
  19.     -T      Sort by file date and time
  20.  
  21.   Directory mask:
  22.  
  23.     Path, Filename, wildcards, etc.
  24.  
  25. }
  26.  
  27. {$I-,S-}
  28. {$M 8192,8192,655360}
  29.  
  30. uses Dos;
  31.  
  32. const
  33.   MaxDirSize = 512;
  34.   MonthStr: array[1..12] of string[3] = (
  35.     'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
  36.     'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
  37.  
  38. type
  39.   DirPtr   = ^DirRec;
  40.   DirRec   = record
  41.                Attr: Byte;
  42.                Time: Longint;
  43.                Size: Longint;
  44.                Name: string[12];
  45.              end;
  46.   DirList  = array[0..MaxDirSize - 1] of DirPtr;
  47.   LessFunc = function(X, Y: DirPtr): Boolean;
  48.  
  49. var
  50.   WideDir: Boolean;
  51.   Count: Integer;
  52.   Less: LessFunc;
  53.   Path: PathStr;
  54.   Dir: DirList;
  55.  
  56. function NumStr(N, D: Integer): String;
  57. begin
  58.   NumStr[0] := Chr(D);
  59.   while D > 0 do
  60.   begin
  61.     NumStr[D] := Chr(N mod 10 + Ord('0'));
  62.     N := N div 10;
  63.     Dec(D);
  64.   end;
  65. end;
  66.  
  67. {$F+}
  68.  
  69. function LessName(X, Y: DirPtr): Boolean;
  70. begin
  71.   LessName := X^.Name < Y^.Name;
  72. end;
  73.  
  74. function LessSize(X, Y: DirPtr): Boolean;
  75. begin
  76.   LessSize := X^.Size < Y^.Size;
  77. end;
  78.  
  79. function LessTime(X, Y: DirPtr): Boolean;
  80. begin
  81.   LessTime := X^.Time > Y^.Time;
  82. end;
  83.  
  84. {$F-}
  85.  
  86. procedure QuickSort(L, R: Integer);
  87. var
  88.   I, J: Integer;
  89.   X, Y: DirPtr;
  90. begin
  91.   I := L;
  92.   J := R;
  93.   X := Dir[(L + R) div 2];
  94.   repeat
  95.     while Less(Dir[I], X) do Inc(I);
  96.     while Less(X, Dir[J]) do Dec(J);
  97.     if I <= J then
  98.     begin
  99.       Y := Dir[I];
  100.       Dir[I] := Dir[J];
  101.       Dir[J] := Y;
  102.       Inc(I);
  103.       Dec(J);
  104.     end;
  105.   until I > J;
  106.   if L < J then QuickSort(L, J);
  107.   if I < R then QuickSort(I, R);
  108. end;
  109.  
  110. procedure GetCommand;
  111. var
  112.   I,J: Integer;
  113.   Attr: Word;
  114.   S: PathStr;
  115.   D: DirStr;
  116.   N: NameStr;
  117.   E: ExtStr;
  118.   F: File;
  119. begin
  120.   WideDir := False;
  121.   @Less := nil;
  122.   Path := '';
  123.   for I := 1 to ParamCount do
  124.   begin
  125.     S := ParamStr(I);
  126.     if S[1] = '-' then
  127.       for J := 2 to Length(S) do
  128.         case UpCase(S[J]) of
  129.           'N': Less := LessName;
  130.           'S': Less := LessSize;
  131.           'T': Less := LessTime;
  132.           'W': WideDir := True;
  133.         else
  134.           WriteLn('Invalid option: ', S[J]);
  135.           Halt(1);
  136.         end
  137.     else
  138.       Path := S;
  139.   end;
  140.   Path := FExpand(Path);
  141.   if Path[Length(Path)] <> '\' then
  142.   begin
  143.     Assign(F, Path);
  144.     GetFAttr(F, Attr);
  145.     if (DosError = 0) and (Attr and Directory <> 0) then
  146.       Path := Path + '\';
  147.   end;
  148.   FSplit(Path, D, N, E);
  149.   if N = '' then N := '*';
  150.   if E = '' then E := '.*';
  151.   Path := D + N + E;
  152. end;
  153.  
  154. procedure FindFiles;
  155. var
  156.   F: SearchRec;
  157. begin
  158.   Count := 0;
  159.   FindFirst(Path, ReadOnly + Directory + Archive, F);
  160.   while (DosError = 0) and (Count < MaxDirSize) do
  161.   begin
  162.     GetMem(Dir[Count], Length(F.Name) + 10);
  163.     Move(F.Attr, Dir[Count]^, Length(F.Name) + 10);
  164.     Inc(Count);
  165.     FindNext(F);
  166.   end;
  167. end;
  168.  
  169. procedure SortFiles;
  170. begin
  171.   if (Count <> 0) and (@Less <> nil) then
  172.     QuickSort(0, Count - 1);
  173. end;
  174.  
  175. procedure PrintFiles;
  176. var
  177.   I, P: Integer;
  178.   Total: Longint;
  179.   T: DateTime;
  180.   N: NameStr;
  181.   E: ExtStr;
  182. begin
  183.   WriteLn('Directory of ', Path);
  184.   if Count = 0 then
  185.   begin
  186.     WriteLn('No matching files');
  187.     Exit;
  188.   end;
  189.   Total := 0;
  190.   for I := 0 to Count-1 do
  191.   with Dir[I]^ do
  192.   begin
  193.     P := Pos('.', Name);
  194.     if P > 1 then
  195.     begin
  196.       N := Copy(Name, 1, P - 1);
  197.       E := Copy(Name, P + 1, 3);
  198.     end else
  199.     begin
  200.       N := Name;
  201.       E := '';
  202.     end;
  203.     Write(N, ' ': 9 - Length(N), E, ' ': 4 - Length(E));
  204.     if WideDir then
  205.     begin
  206.       if Attr and Directory <> 0 then
  207.         Write(' DIR')
  208.       else
  209.         Write((Size + 1023) shr 10: 3, 'k');
  210.       if I and 3 <> 3 then
  211.         Write(' ': 3)
  212.       else
  213.         WriteLn;
  214.     end else
  215.     begin
  216.       if Attr and Directory <> 0 then
  217.         Write('<DIR>   ')
  218.       else
  219.         Write(Size: 8);
  220.       UnpackTime(Time, T);
  221.       WriteLn(T.Day: 4, '-',
  222.         MonthStr[T.Month], '-',
  223.         NumStr(T.Year mod 100, 2),
  224.         T.Hour: 4, ':',
  225.         NumStr(T.Min, 2));
  226.     end;
  227.     Inc(Total, Size);
  228.   end;
  229.   if WideDir and (Count and 3 <> 0) then WriteLn;
  230.   WriteLn(Count, ' files, ', Total, ' bytes, ',
  231.     DiskFree(Ord(Path[1])-64), ' bytes free');
  232. end;
  233.  
  234. begin
  235.   GetCommand;
  236.   FindFiles;
  237.   SortFiles;
  238.   PrintFiles;
  239. end.
  240.