[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Script to delete a folder on FTP

Posted on 2011-04-23
12
Medium Priority
?
533 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.
0
Comment
Question by:jmhabis2
  • 5
  • 2
  • 2
  • +2
12 Comments
 
LVL 62

Expert Comment

by:gheist
ID: 35455929
windows ftp.exe is so clumsy that i feel this is impossible given your limitations.
0
 
LVL 51

Expert Comment

by:Steve Bink
ID: 35456388
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
 

Author Comment

by:jmhabis2
ID: 35456697
@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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 51

Expert Comment

by:Steve Bink
ID: 35461160
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
 
LVL 32

Expert Comment

by:Robberbaron (robr)
ID: 35464611
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
 
LVL 16

Expert Comment

by:AlexPace
ID: 35477406
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
 

Author Comment

by:jmhabis2
ID: 35481232
@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
 
LVL 16

Expert Comment

by:AlexPace
ID: 35484939
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
 

Author Comment

by:jmhabis2
ID: 35485264
Can you please give me a script using the SYNC command that I can work with
0
 
LVL 32

Accepted Solution

by:
Robberbaron (robr) earned 1000 total points
ID: 35490112
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
 

Author Comment

by:jmhabis2
ID: 35496304
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
 

Author Closing Comment

by:jmhabis2
ID: 35981126
Not complete
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've always wanted to allow a user to have a printer no matter where they login. The steps below will show you how to achieve just that. In this Article I'll show how to deploy printers automatically with group policy and then using security fil…
On July 14th 2015, Windows Server 2003 will become End of Support, leaving hundreds of thousands of servers around the world that still run this 12 year old operating system vulnerable and potentially out of compliance in many organisations around t…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question