We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Script to delete a folder on FTP

jmhabis2
jmhabis2 asked
on
Medium Priority
547 Views
Last Modified: 2012-05-11
Dear Experts,

I need a batch script that would delete a folder with its subfolders from an FTP account.
Comment
Watch Question

Top Expert 2015

Commented:
windows ftp.exe is so clumsy that i feel this is impossible given your limitations.
CERTIFIED EXPERT
Top Expert 2004

Commented:
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.

Author

Commented:
@routinet:

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

<?php
$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
ftp_close($conn_id);
?>
CERTIFIED EXPERT
Top Expert 2004

Commented:
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:

http://www.weberdev.com/get_example-4797.html

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.
CERTIFIED EXPERT

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
http://www.columbia.edu/kermit/ftpscripts.html
http://winscp.net/eng/docs/scripting

Commented:
This type of FTP automation is super-easy with Robo-FTP.
FTPLOGON "ftp.mydomain.com" /user="UserId" /pw="secret" 
FTPDELDIR "FolderToDelete" /delfiles /incldirs
FTPLOGOFF
EXIT

Open in new window

Author

Commented:
@AlexPace

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
:login
FTPLOGON "ftp.mycompany.com" /user="UserID" /pw="password"
;FTPCD "/top/remote/folder"  ; uncomment this line if not sending to root of remote site
SENDFILE "*" /ifnewer /localdate /subdirs /emptydirs  
FTPLOGOFF
PAUSE /for=30  ; wait 30 seconds before next upload
GOTO login
:done
EXIT

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.

Commented:
It Perhaps the SYNC command is better for your requirements.  Here is the page describing how to use it:  http://www.robo-ftp.com/help/3.7/sync.htm

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.

Author

Commented:
Can you please give me a script using the SYNC command that I can work with
CERTIFIED EXPERT
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

Commented:
Hi,

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

Author

Commented:
Not complete
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.