home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 November / Chip_2002-11_cd1.bin / zkuste / delphi / unity / d56 / DW / DW10242.ZIP / FileWorks.pas < prev    next >
Pascal/Delphi Source File  |  2002-08-08  |  25KB  |  746 lines

  1. (*-------------------------------FileWorks.pas--------------------------
  2.  V1.0.242 - 01.07.2002 current release
  3. ------------------------------------------------------------------------
  4. *)
  5.  
  6. unit FileWorks;
  7.  
  8. interface
  9.  
  10. uses Windows, Classes, SysUtils, ShellAPI, StringWorks, FileCtrl;
  11.  
  12. type
  13.    TFileDateTimeStamp = packed record
  14.       Creation,
  15.       LastAccess,
  16.       LastWrite: TDateTime;
  17.    end;
  18.  
  19. //V1.0.242
  20. function GetFileCRC32Int(const Filename: String): DWord;
  21. function GetFileCRC32Hex(const Filename: String): String;
  22.  
  23. //V1.0.241
  24. function FileIsOpen(const Filename: String): Boolean;
  25. function CopyDirectory(const Directory, DestinationFolder: String; const GUI, SimpleGUI, CopyConfirmation, MkDirConfirmation, ErrorGUI: Boolean; var UserHasCancelled: Boolean): Boolean;
  26.  
  27. //V1.0.240
  28. function NTFSCompressFile(const FileName: String; const ForceCompress: Boolean): Boolean;
  29. function NTFSUncompressFile(const FileName: String): Boolean;
  30.  
  31. //V1.0.239
  32. function GetFileDateTimeStamp(const Filename: String; var FileDateTimeStamp: TFileDateTimeStamp): Boolean;
  33. function SetFileDateTimeStamp(const Filename: String; var FileDateTimeStamp: TFileDateTimeStamp): Boolean;
  34.  
  35. function CreateDummyFile(Filename: String; FileSize: Int64): Int64;
  36. function DeleteDirectory(Directory: String; CanUndo, DeleteNotEmpty, GUI: Boolean): Boolean;
  37. function DeleteFiles(Path, Mask: string): Boolean;
  38. function DeleteFilesRecursive(Path, Mask: string): Boolean;
  39. function DirIsEmpty(Path: String): Boolean;
  40. function ExtractDriveFromPath(Path: String): String;
  41. function ExtractPureFilename(Name: String): String;
  42. function ExtractFileExtension(Name: String): String;
  43. function FileAttrib(Filename: String; A,H,R,S: Boolean): Boolean;
  44. function FileCopy(Source, Destination: String): Integer;
  45. function GetFileSize(Datei: String): Int64;
  46. function ITB(Directory: String): String;
  47. procedure ListFiles(const Path, Mask: string; var List: TStringList; const ShowPath: Boolean);
  48. function ListFilesRecursive(Path, Mask: string; ShowPath: Boolean): TStringList;
  49. //function ListFilesRecursive2(Path, Mask: String; ShowPath: Boolean): TStringList;
  50. function ListFolders(Path: string; ShowPath: boolean): TStringlist;
  51. procedure PatchFile(filename:string;data:array of byte;offset,count:longint);
  52. function RenameDirectory(OldPath, NewPath: String): Boolean;
  53. function MoveDirectory(const Directory, DestinationFolder: String; const GUI, SimpleGUI, MoveConfirmation, MkDirConfirmation, ErrorGUI: Boolean; var UserHasCancelled: Boolean): Boolean;
  54. function ReadStringFromFile(const Filename: String): String;
  55. procedure SaveStringToFile(const Str, Filename: String);
  56.  
  57. implementation
  58.  
  59. function GetFileCRC32Int(const Filename: String): DWord;
  60. const 
  61.   dwCRC32Table: array[0..255] of DWORD =
  62.    ($00000000, $77073096, $EE0E612C, $990951BA,
  63.     $076DC419, $706AF48F, $E963A535, $9E6495A3,
  64.     $0EDB8832, $79DCB8A4, $E0D5E91E, $97D2D988,
  65.     $09B64C2B, $7EB17CBD, $E7B82D07, $90BF1D91,
  66.     $1DB71064, $6AB020F2, $F3B97148, $84BE41DE,
  67.     $1ADAD47D, $6DDDE4EB, $F4D4B551, $83D385C7,
  68.     $136C9856, $646BA8C0, $FD62F97A, $8A65C9EC,
  69.     $14015C4F, $63066CD9, $FA0F3D63, $8D080DF5,
  70.     $3B6E20C8, $4C69105E, $D56041E4, $A2677172,
  71.     $3C03E4D1, $4B04D447, $D20D85FD, $A50AB56B,
  72.     $35B5A8FA, $42B2986C, $DBBBC9D6, $ACBCF940,
  73.     $32D86CE3, $45DF5C75, $DCD60DCF, $ABD13D59,
  74.     $26D930AC, $51DE003A, $C8D75180, $BFD06116,
  75.     $21B4F4B5, $56B3C423, $CFBA9599, $B8BDA50F,
  76.     $2802B89E, $5F058808, $C60CD9B2, $B10BE924,
  77.     $2F6F7C87, $58684C11, $C1611DAB, $B6662D3D,
  78.     $76DC4190, $01DB7106, $98D220BC, $EFD5102A,
  79.     $71B18589, $06B6B51F, $9FBFE4A5, $E8B8D433,
  80.     $7807C9A2, $0F00F934, $9609A88E, $E10E9818,
  81.     $7F6A0DBB, $086D3D2D, $91646C97, $E6635C01,
  82.     $6B6B51F4, $1C6C6162, $856530D8, $F262004E,
  83.     $6C0695ED, $1B01A57B, $8208F4C1, $F50FC457,
  84.     $65B0D9C6, $12B7E950, $8BBEB8EA, $FCB9887C,
  85.     $62DD1DDF, $15DA2D49, $8CD37CF3, $FBD44C65,
  86.     $4DB26158, $3AB551CE, $A3BC0074, $D4BB30E2,
  87.     $4ADFA541, $3DD895D7, $A4D1C46D, $D3D6F4FB,
  88.     $4369E96A, $346ED9FC, $AD678846, $DA60B8D0,
  89.     $44042D73, $33031DE5, $AA0A4C5F, $DD0D7CC9,
  90.     $5005713C, $270241AA, $BE0B1010, $C90C2086,
  91.     $5768B525, $206F85B3, $B966D409, $CE61E49F,
  92.     $5EDEF90E, $29D9C998, $B0D09822, $C7D7A8B4,
  93.     $59B33D17, $2EB40D81, $B7BD5C3B, $C0BA6CAD,
  94.     $EDB88320, $9ABFB3B6, $03B6E20C, $74B1D29A,
  95.     $EAD54739, $9DD277AF, $04DB2615, $73DC1683,
  96.     $E3630B12, $94643B84, $0D6D6A3E, $7A6A5AA8,
  97.     $E40ECF0B, $9309FF9D, $0A00AE27, $7D079EB1,
  98.     $F00F9344, $8708A3D2, $1E01F268, $6906C2FE,
  99.     $F762575D, $806567CB, $196C3671, $6E6B06E7,
  100.     $FED41B76, $89D32BE0, $10DA7A5A, $67DD4ACC,
  101.     $F9B9DF6F, $8EBEEFF9, $17B7BE43, $60B08ED5,
  102.     $D6D6A3E8, $A1D1937E, $38D8C2C4, $4FDFF252,
  103.     $D1BB67F1, $A6BC5767, $3FB506DD, $48B2364B,
  104.     $D80D2BDA, $AF0A1B4C, $36034AF6, $41047A60,
  105.     $DF60EFC3, $A867DF55, $316E8EEF, $4669BE79,
  106.     $CB61B38C, $BC66831A, $256FD2A0, $5268E236,
  107.     $CC0C7795, $BB0B4703, $220216B9, $5505262F,
  108.     $C5BA3BBE, $B2BD0B28, $2BB45A92, $5CB36A04,
  109.     $C2D7FFA7, $B5D0CF31, $2CD99E8B, $5BDEAE1D,
  110.     $9B64C2B0, $EC63F226, $756AA39C, $026D930A,
  111.     $9C0906A9, $EB0E363F, $72076785, $05005713,
  112.     $95BF4A82, $E2B87A14, $7BB12BAE, $0CB61B38,
  113.     $92D28E9B, $E5D5BE0D, $7CDCEFB7, $0BDBDF21,
  114.     $86D3D2D4, $F1D4E242, $68DDB3F8, $1FDA836E,
  115.     $81BE16CD, $F6B9265B, $6FB077E1, $18B74777,
  116.     $88085AE6, $FF0F6A70, $66063BCA, $11010B5C,
  117.     $8F659EFF, $F862AE69, $616BFFD3, $166CCF45,
  118.     $A00AE278, $D70DD2EE, $4E048354, $3903B3C2,
  119.     $A7672661, $D06016F7, $4969474D, $3E6E77DB,
  120.     $AED16A4A, $D9D65ADC, $40DF0B66, $37D83BF0,
  121.     $A9BCAE53, $DEBB9EC5, $47B2CF7F, $30B5FFE9,
  122.     $BDBDF21C, $CABAC28A, $53B39330, $24B4A3A6,
  123.     $BAD03605, $CDD70693, $54DE5729, $23D967BF,
  124.     $B3667A2E, $C4614AB8, $5D681B02, $2A6F2B94,
  125.     $B40BBE37, $C30C8EA1, $5A05DF1B, $2D02EF8D);
  126. var
  127.   F: file;
  128.   BytesRead: DWORD;
  129.   Buffer: array[1..65521] of Byte;
  130.   i: Word;
  131. begin
  132.   FileMode := 0;
  133.   result    := $ffffffff;
  134.   {$I-}
  135.   AssignFile(F, FileName); 
  136.   Reset(F, 1);
  137.   if IOResult = 0 then 
  138.   begin
  139.     repeat
  140.       BlockRead(F, Buffer, SizeOf(Buffer), BytesRead);
  141.       for i := 1 to BytesRead do
  142.         result := (result shr 8) xor dwCRC32Table[Buffer[i] xor (result and $000000FF)];
  143.     until BytesRead = 0;
  144.   end;
  145.   CloseFile(F);
  146.   {$I+}
  147.   result := not result;
  148. end;
  149.  
  150. function GetFileCRC32Hex(const Filename: String): String;
  151. begin
  152.    result:= IntToHex(GetFileCRC32Int(Filename), 6);
  153. end;
  154.  
  155. function FileIsOpen(const Filename: String): Boolean;
  156. var
  157.    Datei: TFileStream;
  158. begin
  159.   result:= FALSE; 
  160.   if FileExists(Filename) then begin
  161.     try
  162.       Datei:= TFileStream.Create(Filename, fmOpenRead);
  163.       Datei.Free;
  164.     except
  165.       on EFOpenError do result:= true;
  166.     end;
  167.   end;
  168. end;
  169.  
  170.  
  171. function CopyDirectory(const Directory, DestinationFolder: String; const GUI, SimpleGUI, CopyConfirmation, MkDirConfirmation, ErrorGUI: Boolean; var UserHasCancelled: Boolean): Boolean;
  172. var
  173.   FOS : TSHFileOpStruct;
  174.   Flags: Word;
  175. begin
  176.    Flags:= 0;
  177.    if GUI then if SimpleGUI then Flags:= Flags or FOF_SIMPLEPROGRESS
  178.           else                   Flags:= Flags or FOF_SILENT;
  179.    if not CopyConfirmation  then Flags:= Flags or FOF_NOCONFIRMATION;
  180.    if not MkDirConfirmation then Flags:= Flags or FOF_NOCONFIRMMKDIR;
  181.    if not ErrorGUI          then Flags:= Flags or FOF_NOERRORUI;
  182.    ZeroMemory(@FOS,SizeOf(FOS));
  183.    with FOS do begin
  184.       wFunc := FO_COPY;
  185.       fFlags := Flags;
  186.       pFrom := PChar(Directory + #0);
  187.       pTo := PChar(DestinationFolder)
  188.    end;
  189.    RESULT := (0 = ShFileOperation(FOS));
  190.    UserHasCancelled:= FOS.fAnyOperationsAborted;
  191. end;
  192.  
  193. function NTFSCompressFile(const FileName: String; const ForceCompress: Boolean): Boolean;
  194. const
  195.   COMPRESSION_FORMAT_NONE = 0;
  196.   COMPRESSION_FORMAT_DEFAULT = 1;
  197.   FILE_DEVICE_FILE_SYSTEM = 9;
  198.   METHOD_BUFFERED       = 0;
  199.   FILE_READ_DATA        = 1;
  200.   FILE_WRITE_DATA       = 2;
  201.   FSCTL_SET_COMPRESSION = (FILE_DEVICE_FILE_SYSTEM shl 16) or
  202.     ((FILE_READ_DATA or FILE_WRITE_DATA) shl 14) or (16 shl 2) or METHOD_BUFFERED;
  203. var
  204.   hnd:  Integer;
  205.   Comp: SHORT;
  206.   res:  DWORD;
  207.   pcFilename: PChar;
  208. begin
  209.   pcFilename:= PChar(Filename);
  210.   if forceCompress or ((GetFileAttributes(PChar(ExtractFilePath(FileName))) and
  211.     FILE_ATTRIBUTE_COMPRESSED) <> 0) then
  212.   begin
  213.     Result := False;
  214.     if (GetFileAttributes(pcFileName) and FILE_ATTRIBUTE_COMPRESSED) = 0 then
  215.     begin
  216.       hnd := CreateFile(pcFileName, GENERIC_READ or GENERIC_WRITE, 0,nil, OPEN_EXISTING, 0,0);
  217.       try
  218.         Comp := COMPRESSION_FORMAT_DEFAULT;
  219.         if not DeviceIoControl(hnd, FSCTL_SET_COMPRESSION, @Comp,
  220.           SizeOf(SHORT), nil, 0, res, nil) then Exit;
  221.       finally
  222.         CloseHandle(hnd);
  223.       end;
  224.     end;
  225.     Result := True;
  226.   end
  227.   else
  228.     Result := True;
  229. end;
  230.  
  231. function NTFSUncompressFile(const FileName: String): Boolean;
  232. const
  233.   COMPRESSION_FORMAT_NONE = 0;
  234.   COMPRESSION_FORMAT_DEFAULT = 1;
  235.   FILE_DEVICE_FILE_SYSTEM = 9;
  236.   METHOD_BUFFERED       = 0;
  237.   FILE_READ_DATA        = 1;
  238.   FILE_WRITE_DATA       = 2;
  239.   FSCTL_SET_COMPRESSION = (FILE_DEVICE_FILE_SYSTEM shl 16) or
  240.     ((FILE_READ_DATA or FILE_WRITE_DATA) shl 14) or (16 shl 2) or METHOD_BUFFERED;
  241. var
  242.   hnd:  Integer;
  243.   Comp: SHORT;
  244.   res:  DWORD;
  245.   pcFilename: PChar;
  246. begin
  247.   pcFilename:= PChar(Filename);
  248.   Result := False;
  249.   if (GetFileAttributes(pcFileName) and FILE_ATTRIBUTE_COMPRESSED) <> 0 then
  250.   begin
  251.     hnd := CreateFile(pcFileName, GENERIC_READ or GENERIC_WRITE, 0,nil, OPEN_EXISTING, 0,0);
  252.     try
  253.       Comp := COMPRESSION_FORMAT_NONE;
  254.       if not DeviceIoControl(hnd, FSCTL_SET_COMPRESSION, @Comp,
  255.         SizeOf(SHORT), nil, 0, res, nil) then Exit;
  256.     finally
  257.       CloseHandle(hnd);
  258.     end;
  259.     Result := True;
  260.   end
  261.   else
  262.     Result := True;
  263. end;
  264.  
  265. function GetFileDateTimeStamp(const Filename: String; var FileDateTimeStamp: TFileDateTimeStamp): Boolean;
  266. var
  267.    hFile: THandle;
  268.    ftCreation,
  269.    ftLastAccess,
  270.    ftLastWrite,
  271.    ftlCreation,
  272.    ftlLastAccess,
  273.    ftlLastWrite: TFileTime;
  274.    stCreation,
  275.    stLastAccess,
  276.    stLastWrite: TSystemTime;
  277. begin
  278.    result:= FALSE;
  279.    with FileDateTimeStamp do begin
  280.       Creation:= 0;
  281.       LastAccess:= 0;
  282.       LastWrite:= 0;
  283.    end;
  284.    hFile:= CreateFile(PChar(Filename),
  285.                       GENERIC_READ or GENERIC_WRITE,
  286.                       FILE_SHARE_READ,
  287.                       nil,
  288.                       OPEN_EXISTING,
  289.                       FILE_FLAG_BACKUP_SEMANTICS
  290.                       or FILE_ATTRIBUTE_DIRECTORY, 0);
  291.    if (hFile = INVALID_HANDLE_VALUE) then begin
  292.       CloseHandle(hFile);
  293.       exit;
  294.    end else
  295.    try
  296.       if GetFileTime(hFile, @ftCreation,
  297.                             @ftLastAccess,
  298.                             @ftLastWrite) then begin
  299.          if  FileTimeToLocalFileTime(ftCreation, ftlCreation)
  300.          and FileTimeToLocalFileTime(ftLastAccess, ftlLastAccess)
  301.          and FileTimeToLocalFileTime(ftLastWrite, ftlLastWrite) then begin
  302.             if  FileTimeToSystemTime(ftlCreation, stCreation)
  303.             and FileTimeToSystemTime(ftlLastAccess, stLastAccess)
  304.             and FileTimeToSystemTime(ftlLastWrite, stLastWrite) then begin
  305.                FileDateTimeStamp.Creation:= SystemTimeToDateTime(stCreation);
  306.                FileDateTimeStamp.LastAccess:= SystemTimeToDateTime(stLastAccess);
  307.                FileDateTimeStamp.LastWrite:= SystemTimeToDateTime(stLastWrite);
  308.                result:= TRUE;
  309.             end;
  310.          end;
  311.       end;
  312.    finally
  313.       CloseHandle(hFile);
  314.    end;
  315. end;
  316.  
  317. function SetFileDateTimeStamp(const Filename: String; var FileDateTimeStamp: TFileDateTimeStamp): Boolean;
  318. var
  319.    hFile: THandle;
  320.    ftCreation,
  321.    ftLastAccess,
  322.    ftLastWrite,
  323.    ftlCreation,
  324.    ftlLastAccess,
  325.    ftlLastWrite: TFileTime;
  326.    stCreation,
  327.    stLastAccess,
  328.    stLastWrite: TSystemTime;
  329. begin
  330.    result:= FALSE;
  331.    with FileDateTimeStamp do begin
  332.       Creation:= 0;
  333.       LastAccess:= 0;
  334.       LastWrite:= 0;
  335.    end;
  336.    hFile:= CreateFile(PChar(Filename),
  337.                       GENERIC_READ or GENERIC_WRITE,
  338.                       FILE_SHARE_READ,
  339.                       nil,
  340.                       OPEN_EXISTING,
  341.                       FILE_FLAG_BACKUP_SEMANTICS
  342.                       or FILE_ATTRIBUTE_DIRECTORY, 0);
  343.    if (hFile = INVALID_HANDLE_VALUE) then begin
  344.       CloseHandle(hFile);
  345.       exit;
  346.    end else
  347.    try
  348.       with FileDateTimeStamp do begin
  349.          DateTimeToSystemTime(Creation, stCreation);
  350.          DateTimeToSystemTime(LastAccess, stLastAccess);
  351.          DateTimeToSystemTime(LastWrite, stLastWrite);
  352.          if SystemTimeToFileTime(stCreation, ftlCreation) and
  353.             SystemTimeToFileTime(stLastAccess, ftlLastAccess) and
  354.             SystemTimeToFileTime(stLastWrite, ftlLastWrite) then begin
  355.             if LocalFileTimeToFileTime(ftlCreation, ftCreation) and
  356.                LocalFileTimeToFileTime(ftlLastAccess, ftLastAccess) and
  357.                LocalFileTimeToFileTime(ftlLastWrite, ftLastWrite) then begin
  358.                result:= SetFileTime(hFile, @ftCreation, @ftLastAccess, @ftLastWrite);
  359.             end;
  360.          end;
  361.       end;
  362.    finally
  363.       CloseHandle(hFile);
  364.    end;
  365. end;
  366.  
  367. function CreateDummyFile(Filename: String; FileSize: Int64): Int64;
  368. var
  369.    FS: TFileStream;
  370.    I: Integer;
  371.    FillChr: Char;
  372.    SizeF: Integer;
  373. begin
  374.    result:= -1;
  375.    SizeF:= 1;
  376.    randomize;
  377.    if FileExists(Filename) then exit;
  378.    FS:= TFileStream.Create(Filename, fmCreate);
  379.    for I:=0 to FileSize - 1 do begin
  380.       FillChr:= Chr(I-Random(255)+1);
  381.       FS.Write(FillChr ,SizeF);
  382.    end;
  383.    FS.Free;
  384. end;
  385.  
  386. function ExtractPureFilename(Name: String): String;
  387. var
  388.    Dots, DotPos: Integer;
  389. begin
  390.    Dots:= StringCountInStr('.', Name);
  391.    DotPos:= SubPositionByIndex(Name, '.', Dots);
  392.    result:= StrLeft(Name, DotPos - 1);
  393. end;
  394.  
  395. function ExtractFileExtension(Name: String): String;
  396. var
  397.    Dots, DotPos, StrLn: Integer;
  398. begin
  399.    Dots:= StringCountInStr('.', Name);
  400.    StrLn:= StrLen(PChar(Name));
  401.    DotPos:= SubPositionByIndex(Name, '.', Dots);
  402.    result:= Copy(Name, DotPos + 1, StrLn);
  403. end;
  404.  
  405. function FileCopy(Source, Destination: String): Integer;
  406. var
  407.   Q, Z: TFileStream;
  408. begin
  409.    if not FileExists(Source) then begin
  410.       result:= -2; (* Fehlercode fⁿr "Quelle nicht gefunden" *)
  411.       exit;
  412.    end;
  413.    if FileExists(Destination) then begin
  414.       result:= -3; (* Fehlercode fⁿr "Zieldatei vorhanden" *)
  415.       exit;
  416.    end;
  417.    try
  418.       Q := TFileStream.Create(Source, fmOpenRead);
  419.       Z := TFileStream.Create(Destination, fmCreate);
  420.       Z.CopyFrom(Q, Q.Size);
  421.       Q.Free;
  422.       Z.Free;
  423.       result:= 0; (* Fehlercode 0 fⁿr erfolgreiche Ausfⁿhrung *)
  424.    except
  425.       result:= -4; (* Fehlercode fⁿr "Datei konnte nicht kopiert werden" *)
  426.    end;
  427. end;
  428.  
  429. function FileMove(Source, Destination: String): Integer;
  430. var
  431.   Q, Z: TFileStream;
  432. begin
  433.    if not FileExists(Source) then begin
  434.       result:= -2; (* Fehlercode fⁿr "Quelle nicht gefunden" *)
  435.       exit;
  436.    end;
  437.    if FileExists(Destination) then begin
  438.       result:= -3; (* Fehlercode fⁿr "Zieldatei vorhanden" *)
  439.       exit;
  440.    end;
  441.    try
  442.       Q := TFileStream.Create(Source, fmOpenRead);
  443.       Z := TFileStream.Create(Destination, fmCreate);
  444.       Z.CopyFrom(Q, Q.Size);
  445.       Q.Free;
  446.       Z.Free;
  447.       if not DeleteFile(PChar(Source)) then begin
  448.          result:= -5; (* Fehlercode fⁿr "Quelldatei kann nicht entfernt werden" *)
  449.          exit;
  450.       end;
  451.    result:= 0; (* Fehlercode 0 fⁿr erfolgreiche Ausfⁿhrung *)
  452.    except
  453.       result:= -4; (* Fehlercode fⁿr "Datei konnte nicht kopiert werden" *)
  454.    end;
  455. end;
  456.  
  457. procedure ListFiles(const Path, Mask: string; var List: TStringList; const ShowPath: Boolean);
  458. var
  459.   SRec: TSearchRec;
  460.   sPath, sSearch: String;
  461. begin
  462.    sPath:=IncludeTrailingBackslash(Path);
  463.    sSearch:=sPath + Mask;
  464.    if not ShowPath then sPath:= '';
  465.    if FindFirst(sSearch, faAnyFile-faDirectory, SRec)=0 then
  466.    repeat
  467.       List.Add(sPath+sRec.Name);
  468.    until FindNext(SRec)<>0;
  469.    FindClose(SRec);
  470. end;
  471.  
  472. function ListFolders(Path: string; ShowPath: boolean): TStringlist;
  473. var
  474.   SRec: TSearchRec;
  475.   SL: TStringList;
  476. begin
  477.    (* ++++++++++++++++ Code sample by Pumi ++++++++++++++++++ *)
  478.    SL := TStringList.Create;
  479.    result:= TStringList.Create;   
  480.    FindFirst(Path + '*.*', {not faAnyFile +
  481.                            not faSysFile +
  482.                            not faHidden +
  483.                            not faReadOnly}
  484.                            faDirectory,
  485.                            SRec);
  486.    if ShowPath then
  487.       SL.Add(Path + SRec.Name)
  488.    else
  489.       SL.Add(SRec.Name);
  490.    while FindNext(SRec) = 0 do
  491.       if ShowPath then
  492.          SL.Add(Path + SRec.Name)
  493.       else
  494.          SL.Add(SRec.Name);
  495.    FindClose(SRec);
  496.    Result.Assign(SL);
  497. end;
  498.  
  499. function DeleteFiles(Path, Mask: string): Boolean;
  500. var
  501.   SRec: TSearchRec;
  502.   SL: TStringList;
  503.   I: Integer;
  504. begin
  505.    (* ++++++++++++++++ Code based on Assarbad ++++++++++++++++++ *)
  506.    result:= FALSE;
  507.    SL := TStringList.Create;
  508.    FindFirst(Path + Mask, not faDirectory, SRec);
  509.    SL.Add(Path + SRec.Name);
  510.    while FindNext(SRec) = 0 do SL.Add(Path + SRec.Name);
  511.    FindClose(SRec);
  512.    if SL.Count < 1 then exit;
  513.    for I:= 0 to SL.Count - 1 do begin
  514.       DeleteFile(PChar(SL[I]));
  515.    end;
  516.    result:= TRUE;
  517. end;
  518.  
  519. { -------- This version has a bug in recursive search---------
  520. function ListFilesRecursive(Path, Mask: string; ShowPath: Boolean): TStringList;
  521. var SR: TSearchRec;
  522.     err, I: Integer;
  523.     dummy: String;
  524.     TempList: TStringList;
  525. begin
  526.    (* ++++++++++++++++ Code sample by Assarbad ++++++++++++++++++ *)
  527.      err:=FindFirst(Path+Mask, faAnyFile, SR);
  528.      Path:= IncludeTrailingBackslash(Path);
  529.      result:= TStringList.Create;
  530.      TempList:= TStringList.Create;
  531.      while err=0 do begin
  532.            if (sr.name<>'.') and (sr.name<>'..') then
  533.               if ((sr.Attr and fadirectory)<>0) then begin
  534.                  dummy:=path+sr.name+'\';
  535.                  TempList.Assign(ListFilesRecursive(dummy, mask, ShowPath));
  536.                  for I:= 0 to TempList.Count - 1 do begin
  537. //                    if ShowPath then result.Add(Dummy + TempList[I]) else
  538.                        result.Add(TempList[I]);
  539.                  end;
  540.               end else if ShowPath then result.Append(Path + sr.name) else
  541.                  result.Append(sr.name);
  542.            err:=findnext(sr);
  543.      end;
  544.      FindClose(SR);
  545.      TempList.Free;
  546. end;}
  547.  
  548. function ListFilesRecursive(Path, Mask: String; ShowPath: Boolean): TStringList;
  549. var SR: TSearchRec;
  550.     Erg, I: Integer;
  551.     RelDir, TempStr : String;
  552.     TempList: TStringList;
  553. begin
  554.    IncludeTrailingBackslash(Path);
  555.    RelDir := ExtractFilePath(Mask);
  556.    Mask := ExtractFileName(Mask);
  557.    result:= TStringList.Create;
  558.    TempList:= TStringList.Create;
  559.    Erg := FindFirst(Path+RelDir+'*.*', faDirectory, SR);
  560.    while Erg = 0 do
  561.    begin
  562.       if (SR.attr and faDirectory) <> 0 then
  563.          if SR.Name[1] <> '.' then begin
  564.             TempList.Assign(ListFilesRecursive(Path, RelDir+SR.Name+'\'+Mask, ShowPath));
  565.             for I:= 0 to TempList.Count - 1 do begin
  566.                TempStr:= TempList[I];
  567.                if ShowPath and not (TempStr[2] = ':') then TempStr:= Path + TempStr;
  568.                result.Add(TempList[I]);
  569.             end;
  570.          end;
  571.       Erg := FindNext(SR);
  572.    end;
  573.    Erg := FindFirst(Path+RelDir+Mask, $27, SR);
  574.    {Das Attribut $27 bedeutet
  575.    alle echten Dateien}
  576.    while Erg = 0 do
  577.    begin
  578.       if not ShowPath then
  579.          result.Add(RelDir+SR.Name)
  580.       else
  581.       result.Add(Path+RelDir+SR.Name);
  582.       Erg := FindNext(SR);
  583.    end;
  584.    FindClose(SR);
  585.    TempList.Free;
  586. end;
  587.  
  588. function DeleteFilesRecursive(Path, Mask: string): Boolean;
  589. var SR: TSearchRec;
  590.     err, I:integer;
  591.     dummy:string;
  592.     SL: TStringList;
  593. begin
  594.    (* ++++++++++++++++ Code based on Assarbad ++++++++++++++++++ *)
  595.      result:= FALSE;
  596.      err:=FindFirst(Path+Mask, faAnyFile, SR);
  597.      SL:= TStringList.Create;
  598.      while err=0 do begin
  599.            if (sr.name<>'.') and (sr.name<>'..') then
  600.               if ((sr.Attr and fadirectory)<>0) then begin
  601.                  dummy:=path+sr.name+'\';
  602.                  ListFilesRecursive(dummy, Mask, TRUE);
  603.               end else SL.Append(sr.name);
  604.            err:=findnext(sr);
  605.      end;
  606.      FindClose(SR);
  607.      if SL.Count < 1 then exit;
  608.      for I:= 0 to SL.Count - 1 do begin
  609.         DeleteFile(PChar(SL[I]));
  610.      end;
  611.      result:= TRUE;
  612. end;
  613.  
  614. function DirIsEmpty(Path: String): Boolean;
  615. var
  616.    SL: TStringList;
  617. begin
  618.    SL:= ListFilesRecursive(ITB(Path), '*.*', TRUE);
  619.    result:= (SL.Count < 1);
  620. end;
  621.  
  622. function ExtractDriveFromPath(Path: String): String;
  623. begin
  624.    if (StrLen(PChar(Path)) > 0) then result:= Copy(Path, 1, 1) else result:= '';
  625. end;
  626.  
  627. function ITB(Directory: String): String;
  628. begin
  629.    result:= UpperCase(IncludeTrailingBackslash(Directory));
  630. end;
  631.  
  632. function RenameDirectory(OldPath, NewPath: String): Boolean;
  633. begin
  634.    result:= FALSE;
  635.    if not (ExtractDriveFromPath(OldPath) = ExtractDriveFromPath(NewPath)) then
  636.       exit;
  637.    result:= MoveFile(PChar(OldPath), PChar(NewPath));
  638. end;
  639.  
  640. function MoveDirectory(const Directory, DestinationFolder: String; const GUI, SimpleGUI, MoveConfirmation, MkDirConfirmation, ErrorGUI: Boolean; var UserHasCancelled: Boolean): Boolean;
  641. var
  642.   FOS : TSHFileOpStruct;
  643.   Flags: Word;
  644. begin
  645.    Flags:= 0;
  646.    if GUI then if SimpleGUI then Flags:= Flags or FOF_SIMPLEPROGRESS
  647.           else                   Flags:= Flags or FOF_SILENT;
  648.    if not MoveConfirmation  then Flags:= Flags or FOF_NOCONFIRMATION;
  649.    if not MkDirConfirmation then Flags:= Flags or FOF_NOCONFIRMMKDIR;
  650.    if not ErrorGUI          then Flags:= Flags or FOF_NOERRORUI;
  651.    ZeroMemory(@FOS,SizeOf(FOS));
  652.    with FOS do begin
  653.       wFunc := FO_MOVE;
  654.       fFlags := Flags;
  655.       pFrom := PChar(Directory + #0);
  656.       pTo := PChar(DestinationFolder)
  657.    end;
  658.    RESULT := (0 = ShFileOperation(FOS));
  659.    UserHasCancelled:= FOS.fAnyOperationsAborted;
  660. end;
  661.  
  662. function DeleteDirectory(Directory: String; CanUndo, DeleteNotEmpty, GUI: Boolean): Boolean;
  663. var
  664.   FOS : TSHFileOpStruct;
  665.   FOS_FLAG: DWORD;
  666. begin
  667.    result:= FALSE;
  668.    Directory:= ITB(Directory);
  669.    if not DirectoryExists(Directory) then exit;
  670.    if not DeleteNotEmpty then begin
  671.       if not DirIsEmpty(Directory) then exit;
  672.    end;
  673.    ZeroMemory(@FOS,SizeOf(FOS));
  674.    if CanUndo then FOS_FLAG:= FOF_ALLOWUNDO else FOS_FLAG:= 0;
  675.    if not GUI then FOS_FLAG:= FOS_FLAG + FOF_NOCONFIRMATION;
  676.    with FOS do begin
  677.       wFunc := FO_DELETE;
  678.       fFlags := FOS_FLAG;
  679.       pFrom := PChar(Directory + #0);
  680.    end;
  681.    RESULT := (0 = ShFileOperation(FOS));
  682. end;
  683.  
  684. function  GetFileSize(Datei: String): Int64;
  685. var
  686.    TempFileSize: Int64;
  687.    SR: TSearchRec;
  688. begin
  689.      TempFileSize:= 0;
  690.      if FindFirst(Datei,faAnyFile,SR) = 0 then
  691.      TempFileSize:= SR.Size;
  692.      FindClose(SR);
  693.      result:= TempFileSize;
  694. end;
  695.  
  696. procedure SaveStringToFile(const Str, Filename: String);
  697. var
  698.    TF: TextFile;
  699. begin
  700.    AssignFile(TF, Filename);
  701.    Rewrite(TF);
  702.    Write(TF, Str);
  703.    Close(TF);
  704. end;
  705.  
  706. function ReadStringFromFile(const Filename: String): String;
  707. var
  708.    TF: TextFile;
  709. begin
  710.    if not FileExists(Filename) then begin
  711.       result:= '';
  712.       exit;
  713.    end;
  714.    AssignFile(TF, Filename);
  715.    Reset(TF);
  716.    Read(TF, result);
  717.    Close(TF);
  718. end;
  719.  
  720. function FileAttrib(Filename: String; A,H,R,S: Boolean): Boolean;
  721. var
  722.    Attrbs: DWord;
  723. begin
  724.    Attrbs:= 0;
  725.    if A then Attrbs:= Attrbs and FILE_ATTRIBUTE_ARCHIVE;
  726.    if H then Attrbs:= Attrbs and FILE_ATTRIBUTE_HIDDEN;
  727.    if R then Attrbs:= Attrbs and FILE_ATTRIBUTE_READONLY;
  728.    if S then Attrbs:= Attrbs and FILE_ATTRIBUTE_SYSTEM;
  729.    result:= SetFileAttributes(PChar(Filename), Attrbs);
  730. end;
  731. procedure PatchFile(filename:string;data:array of byte;offset,count:longint);
  732. var
  733.   f:file;
  734. begin
  735.   assignfile(f,filename);
  736.   {$i-}reset(f,1);{$i+}if ioresult<>0 then exit;
  737.   seek(f,offset);
  738.   blockwrite(f,data,count);
  739.   closefile(f);
  740. end;
  741.  
  742.  
  743.  
  744.  
  745. end.
  746.