daily pastebin goal
33%
SHARE
TWEET

SHFileOperation.iss

Oxygene Feb 23rd, 2019 95 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;***************************************************************;
  2. ;****************** SHFileOperation.iss ************************;
  3. ;***************************************************************;
  4. ;* Include this file in project. Example:
  5. ;* #include "SHFileOperation.iss"
  6. ;***************************************************************;
  7. ;************************ 1 ************************************;
  8. ;* function CopyDir(const fromDir, toDir: string): Boolean;
  9. ;* Example 1 (without <fromDir> trailing backslash):
  10. ;*     CopyDir('C:\TMP\MyApp', 'C:\TMP\Backup');
  11. ;* Result: C:\TMP\Backup\MyApp\..all <MyApp> subdirs and files
  12. ;* Example 2 (with <fromDir> trailing backslash):
  13. ;*     CopyDir('C:\TMP\MyApp\', 'C:\TMP\Backup');
  14. ;* Result: C:\TMP\Backup\..all <MyApp> subdirs and files
  15. ;***************************************************************;
  16. ;************************ 2 ************************************;
  17. ;* function MoveDir(const fromDir, toDir: string): Boolean;
  18. ;* Example 1 (without <fromDir> trailing backslash):
  19. ;*     MoveDir('C:\TMP\MyApp', 'C:\TMP\Backup');
  20. ;* Result: C:\TMP\Backup\MyApp\..all <MyApp> subdirs and files
  21. ;* Example 2 (with <fromDir> trailing backslash):
  22. ;*     MoveDir('C:\TMP\MyApp\', 'C:\TMP\Backup');
  23. ;* Result: C:\TMP\Backup\..all <MyApp> subdirs and files
  24. ;***************************************************************;
  25. ;************************ 3 ************************************;
  26. ;* function DelDir(dir: string; toRecycle: Boolean): Boolean;
  27. ;*   If <toRecycle> is True, <dir> deleted in Recycle Bin.
  28. ;***************************************************************;
  29. ;************************ 4 ************************************;
  30. ;* function RenameDir(const fromDir, toDir: string): Boolean;
  31. ;***************************************************************;
  32. ;***************************************************************;
  33. ;***************************************************************;
  34.  
  35. [Code]
  36. type
  37.    TSHFileOpStruct =  record
  38.      Wnd: HWND;
  39.      wFunc: UINT;
  40.      pFrom: PChar;
  41.      pTo: PChar;
  42.      fFlags: Word; // FILEOP_FLAGS;
  43.      fAnyOperationsAborted: BOOL;
  44.      hNameMappings: HWND; // Pointer;
  45.      lpszProgressTitle: PChar; { only used if FOF_SIMPLEPROGRESS }
  46.    end;
  47.  
  48. const
  49. // use in wFunc
  50.    { $EXTERNALSYM FO_MOVE }
  51.    FO_MOVE           = $0001;
  52.    { $EXTERNALSYM FO_COPY }
  53.    FO_COPY           = $0002;
  54.    { $EXTERNALSYM FO_DELETE }
  55.    FO_DELETE         = $0003;
  56.    { $EXTERNALSYM FO_RENAME }
  57.    FO_RENAME         = $0004;
  58. // use in fFlags
  59.    { $EXTERNALSYM FOF_MULTIDESTFILES }
  60.    FOF_MULTIDESTFILES         = $0001;
  61.    { $EXTERNALSYM FOF_CONFIRMMOUSE }
  62.    FOF_CONFIRMMOUSE           = $0002;
  63.    { $EXTERNALSYM FOF_SILENT }
  64.    FOF_SILENT                 = $0004;  { don't create progress/report }
  65.   { $EXTERNALSYM FOF_RENAMEONCOLLISION }
  66.   FOF_RENAMEONCOLLISION      = $0008;
  67.   { $EXTERNALSYM FOF_NOCONFIRMATION }
  68.   FOF_NOCONFIRMATION         = $0010;  { Don't prompt the user. }
  69.    { $EXTERNALSYM FOF_WANTMAPPINGHANDLE }
  70.    FOF_WANTMAPPINGHANDLE      = $0020;  { Fill in
  71. SHFILEOPSTRUCT.hNameMappings
  72.                                           Must be freed using
  73. SHFreeNameMappings }
  74.    { $EXTERNALSYM FOF_ALLOWUNDO }
  75.    FOF_ALLOWUNDO              = $0040;
  76.    { $EXTERNALSYM FOF_FILESONLY }
  77.    FOF_FILESONLY              = $0080;  { on *.*, do only files }
  78.    { $EXTERNALSYM FOF_SIMPLEPROGRESS }
  79.    FOF_SIMPLEPROGRESS         = $0100;  { means don't show names of files }
  80.   { $EXTERNALSYM FOF_NOCONFIRMMKDIR }
  81.   FOF_NOCONFIRMMKDIR         = $0200;  { don't confirm making any
  82. needed dirs }
  83.    { $EXTERNALSYM FOF_NOERRORUI }
  84.    FOF_NOERRORUI              = $0400;  { don't put up error UI }
  85.  
  86.  
  87. function SHFileOperation(const lpFileOp: TSHFileOpStruct):Integer;
  88. external 'SHFileOperation@shell32.dll stdcall';
  89.  
  90. {****************************************************************}
  91. {****************************************************************}
  92. {****************************************************************}
  93.  
  94. function BackupDir(const fromDir, toDir: string; IsMove: Boolean): Boolean;
  95. var
  96.  fos: TSHFileOpStruct;
  97.  _fromDir, _toDir: string;
  98.  SR: TFindRec;
  99.  res: Boolean;
  100. begin
  101.    ForceDirectories(toDir);
  102.  if IsMove then
  103.    fos.wFunc  := FO_MOVE else
  104.    fos.wFunc  := FO_COPY;
  105.    fos.fFlags := FOF_FILESONLY or FOF_SILENT or
  106.               FOF_NOCONFIRMATION or FOF_NOCONFIRMMKDIR;
  107.    _fromDir:= AddBackslash(fromDir);
  108.    _toDir  := AddBackslash(toDir);
  109.  if (Length(fromDir) = Length(_fromDir)) then
  110.    begin
  111.        res:= FindFirst(_fromDir + '*', SR);
  112.      try
  113.        while res do
  114.        begin
  115.          if (SR.Name <> '') and (SR.Name <> '.') and (SR.Name <> '..') then
  116.          begin
  117.            if SR.Attributes = FILE_ATTRIBUTE_DIRECTORY then
  118.              begin
  119.                _fromDir:= _fromDir + SR.Name + #0#0;
  120.                _toDir  := _toDir + #0#0;
  121.                fos.pFrom  := PChar(_fromDir);
  122.                fos.pTo    := PChar(_toDir);
  123.              end else
  124.              begin
  125.                _fromDir:= _fromDir + SR.Name + #0#0;
  126.                _toDir  := _toDir   + SR.Name + #0#0;
  127.                fos.pFrom  := PChar(_fromDir);
  128.                fos.pTo    := PChar(_toDir);
  129.              end;
  130.                Result := (0 = ShFileOperation(fos));
  131.                _fromDir:= ExtractFilePath(_fromDir);
  132.                _toDir:= ExtractFilePath(_toDir);
  133.          end;
  134.          res := FindNext(SR);
  135.        end;
  136.      finally
  137.        FindClose(SR);
  138.      end;
  139.    end else
  140.    begin
  141.      _fromDir:= RemoveBackslashUnlessRoot(_fromDir) + #0#0;
  142.      _toDir  := RemoveBackslashUnlessRoot(_toDir)   + #0#0;
  143.      fos.pFrom  := PChar(_fromDir);
  144.      fos.pTo    := PChar(_toDir);
  145.      Result := (0 = ShFileOperation(fos));
  146.    end;
  147. end;
  148.  
  149. {****************************************************************}
  150. function MoveDir(const fromDir, toDir: string): Boolean;
  151. begin
  152.  Result := BackupDir(fromDir, toDir, True);
  153. end;
  154.  
  155. {****************************************************************}
  156. function CopyDir(const fromDir, toDir: string): Boolean;
  157. begin
  158.  Result := BackupDir(fromDir, toDir, False);
  159. end;
  160.  
  161. {****************************************************************}
  162. function DelDir(dir: string; toRecycle: Boolean): Boolean;
  163. var
  164.  fos: TSHFileOpStruct;
  165.  _dir: string;
  166. begin
  167.    _dir:= RemoveBackslashUnlessRoot(dir) + #0#0;
  168.    fos.wFunc  := FO_DELETE;
  169.    fos.fFlags := FOF_SILENT or FOF_NOCONFIRMATION;
  170.  if toRecycle then
  171.    fos.fFlags := fos.fFlags or FOF_ALLOWUNDO;
  172.    fos.pFrom  := PChar(_dir);
  173.  Result := (0 = ShFileOperation(fos));
  174. end;
  175.  
  176. {****************************************************************}
  177. function RenameDir(const fromDir, toDir: string): Boolean;
  178. var
  179.  fos: TSHFileOpStruct;
  180.  _fromDir, _toDir: string;
  181. begin
  182.    _fromDir:= RemoveBackslashUnlessRoot(fromDir) + #0#0;
  183.    _toDir  := RemoveBackslashUnlessRoot(toDir) + #0#0;
  184.    fos.wFunc  := FO_RENAME;
  185.    fos.fFlags := FOF_FILESONLY or FOF_ALLOWUNDO or
  186.              FOF_SILENT or FOF_NOCONFIRMATION;
  187.    fos.pFrom  := PChar(_fromDir);
  188.    fos.pTo    := PChar(_toDir);
  189.  Result := (0 = ShFileOperation(fos));
  190. end;
  191.  
  192. {****************************************************************}
  193. function FilesMaskOperation(const fromDir, toDir, fileMask: string; FileOp: Integer; EmptyDirRemove: Boolean; toRecycle: Boolean): Boolean;
  194. var
  195.  fos: TSHFileOpStruct;
  196.  _fromDir, _toDir: string;
  197.  FSR, DSR: TFindRec;
  198.  FindResult: Boolean;
  199.  APath: string;
  200. begin
  201.  APath := AddBackslash(fromDir);
  202.  FindResult := FindFirst(APath + fileMask, FSR);
  203.  try
  204.    while FindResult do
  205.    begin
  206.      if FSR.Attributes and FILE_ATTRIBUTE_DIRECTORY = 0 then
  207.        begin
  208.          Case FileOp of
  209.            FO_COPY:
  210.              begin
  211.                fos.wFunc  := FO_COPY;
  212.              end;
  213.            FO_MOVE:
  214.              begin
  215.                fos.wFunc  := FO_MOVE;
  216.              end;
  217.            FO_DELETE:
  218.              begin
  219.                fos.wFunc  := FO_DELETE;
  220.                if toRecycle then fos.fFlags := fos.fFlags or FOF_ALLOWUNDO;
  221.              end;
  222.            FO_RENAME:
  223.              begin
  224.                fos.wFunc  := FO_RENAME;
  225.              end;
  226.          else
  227.            ;
  228.          end;
  229.            fos.fFlags := fos.fFlags or FOF_FILESONLY or FOF_SILENT or
  230.                   FOF_NOCONFIRMATION or FOF_NOCONFIRMMKDIR;
  231.            _fromDir:= APath + FSR.Name + #0#0;
  232.            _toDir:= AddBackslash(toDir) + FSR.Name + #0#0;
  233.            ForceDirectories(ExtractFilePath(_toDir));
  234.            fos.pFrom  := PChar(_fromDir);
  235.            fos.pTo    := PChar(_toDir);
  236.            Result := (0 = ShFileOperation(fos));
  237.        end;
  238.      FindResult := FindNext(FSR);
  239.    end;
  240.    FindResult := FindFirst(APath + '*.*', DSR);
  241.    while FindResult do
  242.    begin
  243.      if ((DSR.Attributes and FILE_ATTRIBUTE_DIRECTORY) = FILE_ATTRIBUTE_DIRECTORY) and
  244.        not ((DSR.Name = '.') or (DSR.Name = '..')) then
  245. {Recursion} FilesMaskOperation(APath + DSR.Name, AddBackslash(toDir) + DSR.Name, fileMask, FileOp, EmptyDirRemove, toRecycle);
  246.      FindResult := FindNext(DSR);
  247.    end;
  248.  finally
  249.    FindClose(FSR);
  250.    FindClose(DSR);
  251.    if EmptyDirRemove then RemoveDir(APath);
  252.  end;
  253. end;
  254.  
  255. function CopyFiles(const fromDir, toDir, fileMask: string): Boolean;
  256. begin
  257.  Result := FilesMaskOperation(fromDir, toDir, fileMask,
  258.         FO_COPY, False, False);
  259. end;
  260.  
  261. function MoveFiles(const fromDir, toDir, fileMask: string): Boolean;
  262. begin
  263.  Result := FilesMaskOperation(fromDir, toDir, fileMask,
  264.         FO_MOVE, True, False);
  265. end;
  266.  
  267. function DelFiles(const fromDir, fileMask: string; toRecycle: Boolean ): Boolean;
  268. begin
  269.  Result := FilesMaskOperation(fromDir, '', fileMask,
  270.         FO_DELETE, True, toRecycle);
  271. end;
  272. {****************************************************************}
  273. {****************************************************************}
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top