Backup folder on FTP Server

Hi all,

How can I connect to a specified path on the FTP and make a backup of all data into another folder on the ftp please?

thx

st3vo
ST3VOAsked:
Who is Participating?
 
Eddie ShipmanAll-around developerCommented:
Try this code, it may have to be modified slightly but gives you the basics of how to do it both ways.


procedure TForm1.TransferDirectory(ADir: String; ASubDirs: Boolean = True);
var
  Dir: String;
  iIndex : Integer;
  SearchRec : TSearchRec;
begin
  Dir := IncludeTrailingPathDelimiter(ADir);
  FillChar(SearchRec,SizeOf(SearchRec),0);
  if FindFirst(Dir + '*.*', faAnyFile, SearchRec) = 0 then
  begin
    try
      repeat
        if (SearchRec.Name <> '.') and (SearchRec.Name <> '..') then
        begin
          if ((SearchRec.Attr and faAnyFile > 0) or (SearchRec.Attr = 0)) then
          begin
            if ASubDirs then
            begin
              if SearchRec.Attr and faDirectory <> 0 then
              begin
                // This is a Directory, create it on the FTP Server
                // and recurse to transfer all files.
                IdFTP1.MakeDir(SearchRec.Name);
                IdFTP1.ChangeDir(SearchRec.Name);
                TransferDirectory(Dir + SearchRec.Name);
              end
              else
                IdFTP1.Put(Dir + SearchRec.Name, SearchRec.Name);
            end
            else
              IdFTP1.Put(Dir + SearchRec.Name, SearchRec.Name);
          end;
        end;
      until FindNext(SearchRec) <> 0;
    finally
      FindClose(SearchRec);
    end;
  end;
end;

procedure TForm1.DownloadFolder(AFTP: TIdFTP; const Source: String; 
                                Dest: String; RootFlag: Boolean);
var
  RootDir: String;
  TempItem: TTreeNode;
  I: Integer;
  DirList: TIdFTPListItems;
  DirItem: TIdFTPListItem;
  LS: TStringList;

    function InvertSlash(sSource: String): String;
    begin
      Result := StringReplace(sSource, '/', '\', [rfReplaceAll])
    end;
begin
  if RootFlag = True then
  begin
    RootDir := StringReplace(Source, '/', '\', [rfReplaceAll]);
    CreateDir(Dest + '\' + RootDir);
    Dest := Dest + '\' + RootDir;
    RootFlag := False;
  end
  else
  begin
    CreateDir(Dest);
  end;


  LS := TStringList.Create;

  try
    LS.Sorted := True;

    try
      if (Source <> '') then
        AFTP.ChangeDir(Source);

      AFTP.TransferType := ftASCII;
      AFTP.List(nil);

      DirList := AFTP.DirectoryListing;
      for i := 0 to DirList.Count - 1 do
      begin
        try
          DirItem := DirList.Items[i];
          if (DirItem.ItemType = ditDirectory) then
          begin
            LS.Add(Trim(DirItem.FileName) + '/');
          end
          else
          begin
            AFTP.TransferType := ftBinary;
            // Need to write the DownloadFile function to do the get...
            DownloadFile(DirItem.FileName, Dest);
          end;
        except
        end;//try
      end;//for

      for i := 0 to LS.Count - 1 do
      begin
        DownloadFolder(AFTP, Source + '/' + LS.Strings[i], Dest + '\' + InvertSlash(LS.Strings[i]), False);
      end;
    finally
    end;
  finally
    LS.Free;
  end;
end;

Open in new window

0
 
Eddie ShipmanAll-around developerCommented:
What you would do is download all files from the FTP folder locally and then transfer them back to the new directory. There is no way to do it just on the FTP side unless you want to just rename the directory but that would not be making a backup copy, now would it?
0
 
ST3VOAuthor Commented:
Thx... I will try it out after work :-)
0
 
ST3VOAuthor Commented:
Thanks very much!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.