delphi - 如何通过递归复制文件夹而不在目标文件夹中包含源文件夹标签?

标签 delphi delphi-10-seattle

我使用下面的代码通过递归复制文件夹。工作正常,但对我来说至少存在一个问题,因为复制后源文件夹标签也包含在 dest 文件夹中,我不想要它。在这一刻发生的事情是这样的:

SRC 文件夹:

C:\MyTest
  -firstfolder
  -secondfolder
  -- secondfolderFile

DEST 文件夹(复制后):
C:\NewTest
  -MyTest
  -firstfolder
  -secondfolder
  -- secondfolderFile

然后我需要在 dest 文件夹中只保留:
C:\NewTest
  -firstfolder
  -secondfolder
  -- secondfolderFile

如何使用以下代码进行此操作?
program testCopyRecursion;

{$APPTYPE CONSOLE}
{$R *.res}

uses
  Windows,
  ShellAPI,
  SysUtils;

function CopyFolder(const SrcFolder, DestFolder: String; iFileOp: Integer;
  OverWrite: Boolean; ShowDialog: Boolean): Boolean;
var
  MyFOStruct: TSHFileOpStruct;
  Src, Dest: String;
  ResultVal: Integer;
begin
  Result := False;

  Src := SrcFolder;
  Dest := DestFolder;

  if not DirectoryExists(Dest) then
    ForceDirectories(Dest);

  if (Src = '') or ((iFileOp <> FO_DELETE) and (Dest = '')) or
    (CompareText(Src, Dest) = 0) then
    Exit;

  if Src[Length(Src)] = '\' then
    SetLength(Src, Length(Src) - 1);
  Src := Src + #0#0;

  if (Dest <> '') and (Dest[Length(Dest)] = '\') then
    SetLength(Dest, Length(Dest) - 1);
  Dest := Dest + #0#0;

  FillChar(MyFOStruct, SizeOf(MyFOStruct), 0);

  with MyFOStruct do
  begin
    Wnd := 0;

    wFunc := iFileOp;
    pFrom := @Src[1];
    pTo := @Dest[1];

    fFlags := FOF_ALLOWUNDO or FOF_NOCONFIRMMKDIR;

    if not OverWrite then
      fFlags := fFlags or FOF_RENAMEONCOLLISION;
    if not ShowDialog then
      fFlags := fFlags or FOF_SILENT;
  end;

  try
    MyFOStruct.fAnyOperationsAborted := False;
    MyFOStruct.hNameMappings := nil;
    ResultVal := ShFileOperation(MyFOStruct);
    Result := (ResultVal = 0);
  finally
  end;
end;

begin
  try
    CopyFolder('C:\MyTest', 'C:\NewTest', FO_COPY, True, False);
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
  Readln;

end.

最佳答案

您正在复制 C:\MyTest文件夹本身进入 C:\NewTest文件夹,而不仅仅是复制 C:\MyTest 中的内容.尝试将源路径设置为 'C:\MyTest\*'而是只复制 C:\MyTest 中的内容.

仅供引用,您不需要调用 ForceDirectories() ,如 SHFileOperation()如果目标文件夹不存在,则创建它。 documentation甚至这样说:

Copy and Move operations can specify destination directories that do not exist. In those cases, the system attempts to create them and normally displays a dialog box to ask the user if they want to create the new directory. To suppress this dialog box and have the directories created silently, set the FOF_NOCONFIRMMKDIR flag in fFlags.



尝试更多类似的东西:
program testCopyRecursion;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  Windows, ShellAPI, SysUtils;

function CopyFolder(const SrcFolder, DestFolder: String;
  OverWrite: Boolean; ShowDialog: Boolean): Boolean;
var
  MyFOStruct: TSHFileOpStruct;
  Src, Dest: String;
begin
  Result := False;

  if (SrcFolder = '') or (DestFolder = '') or
     (CompareText(SrcFolder, DestFolder) = 0) then
    Exit;

  Src := IncludeTrailingPathDelimiter(SrcFolder) + '*'#0;
  Dest := ExcludeTrailingPathDelimiter(DestFolder) + #0;

  FillChar(MyFOStruct, SizeOf(MyFOStruct), 0);

  with MyFOStruct do
  begin
    wFunc := FO_COPY;
    pFrom := PChar(Src);
    pTo := PChar(Dest);
    fFlags := FOF_ALLOWUNDO or FOF_NOCONFIRMMKDIR;
    if not OverWrite then
      fFlags := fFlags or FOF_RENAMEONCOLLISION;
    if not ShowDialog then
      fFlags := fFlags or FOF_SILENT;
  end;

  Result := (SHFileOperation(MyFOStruct) = 0);
end;

begin
  try
    CopyFolder('C:\MyTest', 'C:\NewTest', True, False);
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
  Readln;
end.

关于delphi - 如何通过递归复制文件夹而不在目标文件夹中包含源文件夹标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55962831/

相关文章:

delphi - Delphi7中如何从流中剪切一部分

delphi - 创建一个包含外部 dll 函数的 Delphi 类

delphi - 用 PChar 操作替换字符串赋值

delphi - 如何避免函数结果被函数内部的Free破坏?

sockets - 如何使用 Winsock api 检查互联网连接?

delphi - 为什么MagSetImageScalingCallback函数在新桌面执行时失败?

delphi - Ini 文件 - 读取多行?

delphi - 具有恢复支持的 Indy Http Server

delphi - "Stay on top"Delphi XE中的主窗体和模式对话框

XML 数据绑定(bind)向导 float 和定点数