我引用了别人写的一段递归上传到服务器端的例子。有时能传。当在服务器端删掉文件。再传好像就传不上了。另外代码也常出错。请高手指点。如何写一个上传整个目录的函数?谢谢了。

解决方案 »

  1.   

    代码如下:
    function TForm1.DoUploadDir(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
              if(sToDirName<>'') then
                IdFTP1.MakeDir(sToDirName);
             end;
             }
            repeat
                  tfile:=FindFileData.cFileName;
                  if (tfile='.') or (tfile='..') then
                     Continue;
                  if (FindFileData.dwFileAttributes =FILE_ATTRIBUTE_DIRECTORY) then
                  begin
                       t:=sToDirName+'\'+tfile;
                       if sDirName[Length(sDirName)]<>'\' then
                          DoUploadDir(sDirName+'\'+tfile,t)
                       else
                          DoUploadDir(sDirName+tfile,sToDirName+tfile);
                  end
                  else
                  begin
                       t:=sDirName+'\'+tFile;
                       try
                        IdFTP1.Put( t,tfile,True);
                       except end;
                  end
            until FindNextFile(hFindFile,FindFileData)=false;
           // FindClose(hFindFile);
       end
       else
       begin
            ChDir(sCurDir);
            result:=false;
            exit;
       end;
       //回到原来的目录下
       ChDir(sCurDir);
       result:=true;
    end;
      

  2.   

    function Tform1.DoUploadDir(sDirName, sToDirName: String): Boolean;
    var
       hFindFile:Cardinal;
       t,tfile:String;
       sCurDir:String[255];
       FindFileData:WIN32_FIND_DATA;
    begin
        ;
        ChDir(sDirName);
        hFindFile:=FindFirstFile('*.*',FindFileData);
       if hFindFile<>INVALID_HANDLE_VALUE then
         begin
               if not EnterDirectories('\'+sToDirName) then
               begin
               if  sToDirName<>'' then
                  CreateDirectories('\'+sToDirName);
               end;
            repeat
                  tfile:=FindFileData.cFileName;
                  if (tfile='.') or (tfile='..') then //假如是顶级目录中断找下一个。
                     Continue;
                  if ((FindFileData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) =FILE_ATTRIBUTE_DIRECTORY) then //是否是文件目录
                  begin
                       t:=sToDirName+'\'+tfile; //给出其路径进入文件遍历的递归。
                       if sDirName[Length(sDirName)]<>'\' then
                          DoUploadDir(sDirName+'\'+tfile,t)
                       else
                          DoUploadDir(sDirName+tfile,sToDirName+tfile);
                  end
                  else
                  begin
                       if sDirName[length(sDirName)]<>'\' then
                         t:=sDirName+'\'+tFile   //取文件的路径 开始出栈。
                       else
                         t:=sDirName+tFile;
                       try
                        idftp1.Put( t,'\'+sToDirName+'\'+tFile);
                       except end;
                  end
            until FindNextFile(hFindFile,FindFileData)=false;
       end
       else
       begin
            ChDir(sCurDir);
            result:=false;
            exit;
       end;
       result:=true;
    end;