Script to delete a folder on FTP

Dear Experts,

I need a batch script that would delete a folder with its subfolders from an FTP account.
Who is Participating?
Robberbaron (robr)Connect With a Mentor Commented:
in which direction do you want the sync to occur.  it is important when you start using the /Delete or /Skip options.

have a good read of the help page, about halfway down where it gives examples.

once you can define what you want to happen to orphan files, we can be of assistance.

The following example synchronizes files from a specific local folder with the specific directory on the FTP site:
SYNC "c:\My Data" "server_dir"

The following example descends into local subfolders (and corresponding server subdirectories) as well as deleting orphans on both sides:
SYNC "c:\My Data" /subdirs /delete=both
windows ftp.exe is so clumsy that i feel this is impossible given your limitations.
Steve BinkCommented:
Agreed.  Windows FTP has no scripting capabilities built in.  You would need to use another platform to connect to the FTP.  This might be possible with Windows Scripting Host, but my first inclination is to turn  to something like PHP, which offers socket communication.
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

jmhabis2Author Commented:

Would a script like the one below do the job, and can I run such a script as a schedule task.

For example I want to delete these files every day in the morning

$file = 'public_html/old.txt';

// set up basic connection
$conn_id = ftp_connect($ftp_server);

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

// try to delete $file
if (ftp_delete($conn_id, $file)) {
 echo "$file deleted successful\n";
} else {
 echo "could not delete $file\n";

// close the connection
Steve BinkCommented:
That is the basic idea for how to delete a file.  I believe that is the example from the ftp_delete() documentation.  But before you remove a directory, you must delete any files and subdirectories it contains.  Here is a bit of code someone else wrote for this task:

I have not tested it, but it looks on target.   I did see a user comment under ftp_size() stating Windows returns "0" for a directory size, not "-1" as indicated by the code.  You will want to do some testing before putting this into production.
Robberbaron (robr)Commented:
psftp , part of the PuTTY suite allows a batch of commands to be sent. No error checking though you can bypass error stop.

but there are other opensource/freeware options
This type of FTP automation is super-easy with Robo-FTP.
FTPLOGON "" /user="UserId" /pw="secret" 
FTPDELDIR "FolderToDelete" /delfiles /incldirs

Open in new window

jmhabis2Author Commented:

Mentioning Robo-FTP I was using of the scripts related to it to push data from a local folder to an FTP:

WORKINGDIR "c:\source\folder\path"
IFERROR!= $ERROR_SUCCESS GOTO done  ; bail out if source path unreachable
FTPLOGON "" /user="UserID" /pw="password"
;FTPCD "/top/remote/folder"  ; uncomment this line if not sending to root of remote site
SENDFILE "*" /ifnewer /localdate /subdirs /emptydirs  
PAUSE /for=30  ; wait 30 seconds before next upload
GOTO login

After exactly one week the script stopped working as it was supposed to work, it started throwing the data I have on my local folder to different directories inside the ftp. I don't know if there is something wrong with the script above, but what I want is as follows; I want the folder structure to be exactly the same on both. Whenever information, or a subfolder is added to my local folder to be pushed under the same structure. Maybe we can add the delete function to this script, so we won't have to run two different scripts.
It Perhaps the SYNC command is better for your requirements.  Here is the page describing how to use it:

The SYNC command has a /delete option so you might be able to use the one command to do all the work instead of a combination of SENDFILE and FTPDELDIR.
jmhabis2Author Commented:
Can you please give me a script using the SYNC command that I can work with
jmhabis2Author Commented:

I want to push the data from my local folder with its subfolders to the FTP, so The sync should occur from my local machine to the FTP.

I will try the example you give at the end
jmhabis2Author Commented:
Not complete
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.