一段copy文件代码
写if FindFileData.dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY
找到目录也不认
写成if FindFileData.dwFileAttributes =2064就能判断是目录文件
还有就是加FindClose(hFindfile); 这句后编译不过以上两个问题为什么请帮我看看。
function TMainForm.DoCopyDir(sDirName, sToDirName: String): Boolean;
var
  hFindfile : Cardinal;
  t, tfile:String;
  sCurDir:String[255];
  FindFileData:WIN32_FIND_DATA;
begin
    sCurDir:=GetCurrentDir;
    ChDir(sDirName);
    hFindfile:=FindFirstFile('*.*',FindFileData);
    if hFindFile <> INVALID_HANDLE_VALUE then
  begin
    if not DirectoryExists(sToDirName) then
    begin
      ForceDirectories(sToDirName);
    end;
    Memo1.Lines.Add('hFindFile <> INVALID_HANDLE_VALUE');
    repeat
      tfile:=FindFileData.cFileName;
      if (tfile='.') or (tfile='..') then
      begin
        Continue;
      end;
      //是目录文件的建立 
      if FindFileData.dwFileAttributes = 2064 //FILE_ATTRIBUTE_DIRECTORY
      then
      begin
        t:=sToDirName+'\'+tfile;
        if not DirectoryExists(t) then
        begin
          ForceDirectories(t);
        end;
        if sDirName[Length(sDirName)] <> '\' then
        begin
          DoCopyDir(sDirName+'\'+tfile,t);
        end else begin
          DoCopyDir(sDirName+tfile,t);
        end;      end else begin
        t:=sToDirName+'\'+tFile;
        CopyFile(PChar(tfile),PChar(t),True);
      end;
    until
      FindNextFile(hFindFile,FindFileData) = False;
    FindClose(hFindfile);
  end else begin
    ChDir(sCurDir);
    Result:= True;
  end;
  
  ChDir(sCurDir);
  Result:=true;
end;

解决方案 »

  1.   

    {--- 查找当前目录及此目录下的子目录中的所有的文件 ---}
    procedure FindFile(Path: String);
    var
      hData: TWin32FindData;
      hFile: THandle;
    begin
      hFile := FindFirstFile(PAnsiChar(Path + '\*.*'), hData);
      if hFile <> INVALID_HANDLE_VALUE then
      repeat
        begin
    {--- 屏蔽掉'.'和'..'目录 ---}
          if (hData.cFileName[0] = '.') then
            Continue;
    {--- 若找到的是二级文件目录则在此目录下递归查找 ---}
          if hData.dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY then
            FindFile(Path + '\' + hData.cFileName)
          else
    {--- 若找到的是文件 ---}
          begin
            //进行你的操作,Copy也好,Path + '\' + hData.cFileName这是完整的文件名。
          end;
        end;
      until not FindNextFile(hFile, hData)
      else
      begin
        ShowMessage('This Path is Error!');
        Exit;
      end;
    end;
      

  2.   

    文件拷贝的函数,只要输入源文件和目标文件名即可。
    // 设置文件拷备函数
    function WinCOPY(SourceFileName: String; TargetFileName: String): String;
    var
      OpStruc: TSHFileOpStruct;
      FromBuf,ToBuf: Array[0..128] of Char;
    begin
      FillChar(FromBuf,Sizeof(FromBuf),0);
      FillChar(ToBuf,Sizeof(ToBuf),0);
      //用0初始化FromBuf和ToBuf数组
      StrPCopy(FromBuf,Pchar(SourceFileName));
      StrPCopy(ToBuf,Pchar(TargetFileName));
      //分别在 FromBuf和ToBuf数组中填入操作的源目录及目标目录
      //开始填充OpStruc记录
      with OpStruc do
      begin
        Wnd:=Handle;
        wFunc:=FO_COPY;
        //复制操作
        pFrom:=@FromBuf;
        pTo:=@ToBuf;
        fFlags:=FOF_NOCONFIRMATION or FOF_RENAMEONCOLLISION;
        fAnyOperationsAborted:=False;
        hNameMappings:=nil;
        lpszProgressTitle:=nil;
        OpStR:= SHFileOperation(OpStruc);
      end;
    end;
      

  3.   

    使用 FindFirst,FindNext,FindClose来代替API。