Link to home
Start Free TrialLog in
Avatar of Luis_Romero
Luis_Romero

asked on

Script to delete files older than X days from FTP folders

I need a script that will look in the FTP folder structure and delete the files that are older than x amount of days. Also the script needs to look in any subfolders that users create on their folders. Then I will run that script once a week.
Avatar of krish5music
krish5music
Flag of United States of America image

Hi,

Please try the following code.

Option Explicit
' Create the FileSystem Object
dim oFileSys
dim dir
'Directory to be scanned
dir = "D:\FTPfiles"

Set oFileSys = CreateObject("Scripting.FileSystemObject" )

'Start up Sub
GetDir dir

sub GetDir(dir)
 dim oFolder,oFolders,oFiles,item,Item2

 set oFolder=oFileSys.GetFolder(dir)
 set oFolders=oFolder.SubFolders
 set oFiles=oFolder.Files

 ' get all sub-folders in this folder
 For each item in oFolders
 GetDir(item)
 Next
 item2=0

 'Scan through the files collection, find files older than 20 days,
and delete.
 For each item2 in oFiles
 Dim accessDate,createdate,fDateInfo,DaysOld
 Set fDateInfo = oFileSys.GetFile(item2)
 createdate = fDateInfo.DateCreated
 accessDate = fDateInfo.DateLastAccessed
 DaysOld = DateDiff("d", accessDate, Now)

 If DaysOld > 20 Then
 item2.delete
 end if

 next

end sub

Thanks!

Krish.
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Luis_Romero
Luis_Romero

ASKER

I tried the script on https://www.experts-exchange.com/questions/27008284/Batch-Script-to-Delete-files-from-Folders-and-Sub-Folders-Older-than-2-months-old.html?anchorAnswerId=35689082#a35689082 and was not able to delete the files. I setup the days to keep files to 7 and on the test folder that I was using, and it did not delete the files. Any reason why is this? I just got the DOS screen with the files been process but not deleted.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That worked wonders. Thanks a lot for all the help
Welcome, glad that was useful.

~bp
Can this script have exceptions for certain folder which files cannot be deleted when running the script. We have a few folders that after looking at their information, the files there cannot be remove.
Yes, that could be added.  Will all the folders be directly below the main folder, or could they be subfolders of subfolders?

And which script were you referring to, the BAT one, or the VBS one?

~bp
The is a combination of both. There are a few folders directly below the main folder, and a few others that are subfolders of subfolders. I am using the BAT script
Okay, so would it be just the name folder that was needed to be specified, or the whole path?

Could you open up a new question for this new feature and I can respond there.  Since this is now a BAT script question I'd target the MS-DOS zone, and the Windows Batch Scripting zone.

~bp