home *** CD-ROM | disk | FTP | other *** search
/ PC-X 1997 October / pcx14_9710.iso / swag / delphi.swg / 0180_Scan disks for files.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-11-29  |  5.2 KB  |  128 lines

  1. {-------------------------------------------------------------------------}
  2. {                                                                         }
  3. {  Delphi Object Pascal Unit.                                             }
  4. {                                                                         }
  5. {  File handling routines for windows, compiled from "WINFILE.TPW" and    }
  6. {  other pascal libraries.                                                }
  7. {                                                                         }
  8. {  Includes the following classes :                                       }
  9. {                                                                         }
  10. {  TDiskFileScaner  :  Used to scan disks for files.                      }
  11. {  TFileDropControl :  Placed on a control or form to make it accept      }
  12. {                      files from "File Manager"                          }
  13. {                                                                         }
  14. {  Most functions moved from "FILE_IO" to add to DLL, some functions are  }
  15. {  still being updated.                                                   }
  16. {                                                                         }
  17. {  Some parts Copyright Boralnd International.                            }
  18. {  Copyright ⌐ UK. Dave Rowlands 1993 - 1995.                             }
  19. {                  All Rights Reserved.                                   }
  20. {                                                                         }
  21. {-------------------------------------------------------------------------}
  22.  
  23. Unit FileFind;
  24.   Interface
  25.     Uses
  26.       WinDOS, Classes, SysUtils;
  27.  
  28. Function GetFileList(Const FileSpec : String; List : TStrings) : LongInt;
  29.  
  30. {--- GetFileList(FileSpec, List) -----------------------------------------}
  31. {                                                                         }
  32. { This funtion fills the "List" with files that match "FileSpec".  If the }
  33. { "FileSpec" contains a directory then that directoy is searched,         }
  34. { otherwise the current directory is searched.  Returns the number of     }
  35. { files found                                                             }
  36. {-------------------------------------------------------------------------}
  37.  
  38. Function HasAttr(Const FileName : String; Attr : Word) : Boolean;  
  39.  
  40. {--- HasAttr(Filename, Attr) ---------------------------------------------}
  41. {                                                                         }
  42. { Returns "True" if "Filename" has the file attribute "Attr".             }
  43. {-------------------------------------------------------------------------}
  44.  
  45. Function ValidDIR(Const DirName : String) : String;  
  46.  
  47. {--- ValidDIR(DIRname) ---------------------------------------------------}
  48. {                                                                         }
  49. { Returns a string representing a valid path, created from "DirName" with }
  50. { the "\" character added if it is not already there.                     }
  51. {-------------------------------------------------------------------------}
  52.  
  53. Implementation
  54.  
  55. Function GetFileList(Const FileSpec : String; List : TStrings) : LongInt;
  56. Var
  57.   sRec  : TSearchRec;  { Required by "FindFirst" and "FindNext" }
  58.   spec  : String;      { For holding search specification       }
  59.   sDir  : String;      { Holds full path                        }
  60.   fName : String;      { For filename                           }
  61. begin
  62.   List.Clear;          { Clear the list, to add to existing list comment out }
  63.   spec := '';
  64.   sDir := '';
  65.   If (FileSpec <> '') then
  66.    begin
  67.      spec := ExtractFilename(FileSpec);
  68.      sdir := ExtractFilePath(FileSpec);
  69.    end
  70.    else spec := '*.*'; { Default to ALL files }
  71.  
  72.   { Check to see if we have a valid directory in the "FileSpec" }
  73.   { If we don't we use the current directory.                   }
  74.  
  75.   If (sDir = '') then GetDir(0, sDir);
  76.  
  77.   { Check and convert }
  78.  
  79.   If (Length(sdir) > 0) then sDir := LowerCase(ValidDIR(sDir));
  80.  
  81.   { Look for the first file matching the file specification, "FindFirst" }
  82.   { returns a non zero value if file not found.                          }
  83.  
  84.   Result := FindFirst(sDir + spec, faAnyFile - faDirectory, sRec);
  85.  
  86.   { While we have a filename, build it up to a fully quallified filename }
  87.  
  88.   While (Result = 0) do
  89.    begin
  90.  
  91.      { First, check to see if it's a directory }
  92.       
  93.      If (sRec.Name[1] <> '.') then { It's not }
  94.       begin
  95.  
  96.         { Create full pathname }
  97.  
  98.         fName := sDir + LowerCase(sRec.Name);
  99.  
  100.         { Add it to the string list }
  101.  
  102.         List.Add(fName);
  103.  
  104.      end;
  105.  
  106.      { Now look for the next match }
  107.  
  108.      Result := FindNext(sRec);                 
  109.   end;
  110.   FindClose(sRec);      { We have finished, so tell system }
  111.   Result := List.Count; { Return the number of items in the string list }
  112. end;
  113.  
  114. Function ValidDIR(Const DirName : String) : String;
  115.  begin
  116.    Result := Dirname;
  117.    If (Result[Length(Result)] = '\') then Exit;
  118.    If FileExists(Dirname) then Result := ExtractFilePath(Dirname);
  119.    If HasAttr(Result, faDirectory) then AppendStr(Result, '\');
  120. end;
  121.  
  122. Function HasAttr(Const FileName : String; Attr : Word) : Boolean;
  123. begin
  124.   Result := (FileGetAttr(FileName) and Attr) = Attr;
  125. end;
  126.  
  127. end.
  128.