Mega Code Archive

 
Categories / Delphi / Files
 

How to upload a local directory to an ftp server

Title: How to upload a local directory to an ftp server procedure UploadPerFTP; procedure GetDir(dir: string); var SearchRec: TSearchRec; details, nodetails: TStringList; k: Integer; begin //iterate through directory given if FindFirst(dir + '*.*', faAnyFile, SearchRec) = 0 then begin repeat //get rid of the both "dummy-directories" '.' and '..' //die ordner '.' und '..' brauchen man nicht if (SearchRec.Name '.') and (SearchRec.Name '..') then begin //if we found a folder if (SearchRec.Attr and faDirectory) = faDirectory then begin //get folder contents from ftp. one with details, one without details := TStringList.Create; nodetails := TStringList.Create; FTPClient.List(details, '', True); FTPClient.List(nodetails, '', False); //we only want to have directories in the list (without '.' and '..') for k := details.Count - 1 downto 0 do begin if details.Strings[k] '' then begin if (details.Strings[k][1] 'd') or (nodetails.Strings[k] = '.') or (nodetails.Strings[k] = '..') then begin details.Delete(k); nodetails.Delete(k); end; end; end; //if our directory does not exists on the server, create it if nodetails.IndexOf(SearchRec.Name) = -1 then begin FTPClient.MakeDir(SearchRec.Name); end; //change into next directory on server FTPClient.ChangeDir(SearchRec.Name); details.Free; nodetails.Free; //and also locally go into the next subfolder GetDir(dir + SearchRec.Name + '\'); //we have to go one directory up after leaving the recursion FTPClient.ChangeDirUp; end else begin //if it's only a file, upload it to the current directory FTPClient.Put(dir + SearchRec.Name, SearchRec.Name); end; end; until FindNext(SearchRec) 0; FindClose(SearchRec); end; end; var dir: string; details, nodetails: TStringList; k: Integer; begin //set some basic settings on your ftp client (TIdFTPClient) with FTPClient do begin Host := 'your_server.com'; // Adjust your data here Username := 'your_username'; // Adjust your data here Password := 'your_password'; // Adjust your data here Passive := True; // Adjust your data here end; FTPClient.Connect; //if you want to upload you data to an remote-directory, enter it below (does not matter if 'dir\dir' or 'dir/dir') dir := StringReplace('your/remote_directory', '\', '/', [rfReplaceAll]); // Adjust your data here / Hier gw¨¹nschte Daten eintragen //remove first '/' if there's one //wir l?schen das erste '/', falls eines existiert if dir '' then begin if dir[1] = '/' then Delete(dir, 1, 1); //but add a '/' at the end if dir[Length(dir)] '/' then dir := dir + '/'; //loop through our remote-directories while Pos('/', dir) 0 do begin //get folder contents from ftp. one with details, one without details := TStringList.Create; nodetails := TStringList.Create; FTPClient.List(details, '', True); FTPClient.List(nodetails, '', False); //we only want to have directories in the list (without '.' and '..') for k := details.Count - 1 downto 0 do begin if details.Strings[k] '' then begin if (details.Strings[k][1] 'd') or (nodetails.Strings[k] = '.') or (nodetails.Strings[k] = '..') then begin details.Delete(k); nodetails.Delete(k); end; end; end; //if our directory does not exists on the server, create it if nodetails.IndexOf(Copy(dir, 1, Pos('/', dir) - 1)) = -1 then begin FTPClient.MakeDir(Copy(dir, 1, Pos('/', dir) - 1)); end; //change to our directory on server FTPClient.ChangeDir(Copy(dir, 1, Pos('/', dir) - 1)); //remove first directory from path ('your/directory/subdir/' -- 'directory/subdir/') Delete(dir, 1, Pos('/', dir)); details.Free; nodetails.Free; end; end; //ftp client is ready in your remote-directory //begin to upload our local directory dir := 'C:\your\local\directory\'; if dir[Length(dir)] '\' then dir := dir + '\'; GetDir(dir); FTPClient.Disconnect; end;