WinBatch® Technical Support Forum

All Things WinBatch => WinBatch => Topic started by: jpaton on January 13, 2016, 10:03:49 AM

Title: Delete files on ftp server older than x days
Post by: jpaton on January 13, 2016, 10:03:49 AM
Does anyone know of a way to login to an ftp server and delete files older than x amount of days?  I know how to do it with regular folders (c:\temp, etc) by using a function but I'm not sure how it would work on an ftp server where I can't specify a drive letter and folder.  Any ideas are greatly appreciated.
Title: Re: Delete files on ftp server older than x days
Post by: td on January 13, 2016, 11:00:34 AM
Check out the WinInet extender's iFtpCmd (MDTM command) and iFtpDelete functions.  Not aware of any working examples but will check around and post if I find something.
Title: Re: Delete files on ftp server older than x days
Post by: td on January 13, 2016, 01:08:13 PM
Here is quick and dirty example that does some of what you require.  It gets the modify time of a file and deletes it, if it wasn't modified today:

Code (winbatch) Select
AddExtender("WWINT44I.DLL")

UserId="me"
Pswd="*topsecret*"
SERVER="my.server.com"

tophandle=iBegin(0,"","")

conhandle=iHostConnect(tophandle,SERVER,@FTP, userid,pswd)
If conhandle==0
      Message("ERROR","Unable to connect to host")
      Exit
EndIf

; Get the modify date.
strTime = iFtpCmd(conhandle, 'MDTM', 'test.txt', @ASCII)

; Cleanup result - exactly how depends on server's return info.
strTime = ItemExtract( 1, ItemExtract(2, strTime, ' '), @CR)

nResult = 0
if  strTime < '20160113000000'  ; If it wasn't modified today.
   nResult = iFtpDelete(conhandle, 'test.txt')
endif

Message("File Deleted?", nResult)
iClose(conhandle)
iClose(tophandle)
Title: Re: Delete files on ftp server older than x days
Post by: jpaton on January 14, 2016, 06:38:46 AM
Thanks, that is helpful.  I was hoping to find a way to check all files and delete only those that were x amount of days old. 
Title: Re: Delete files on ftp server older than x days
Post by: td on January 14, 2016, 06:50:23 AM
Check out the iFtpFindInit and iFtpFindNext functions in the Consolidated WIL Help file.  You use them to create a list of files that meet your date criteria because  iFtpFindNext returns create and modification date information. 

I completely forgot about iFtpFindNext's ability to retrieve create date information...