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.
jmhabis2Asked:
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
0
 
gheistCommented:
windows ftp.exe is so clumsy that i feel this is impossible given your limitations.
0
 
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.
0
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:
@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);
?>
0
 
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:

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

0
 
jmhabis2Author 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.
0
 
AlexPaceCommented:
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.
0
 
jmhabis2Author Commented:
Can you please give me a script using the SYNC command that I can work with
0
 
jmhabis2Author 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
0
 
jmhabis2Author Commented:
Not complete
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.