Hello Everyone,
I use the following vbscript code to delete files in a particular directory
on my FTP server. I delete all files in the starting folder and it's
subfolders after 14 days. Today however I had a request to keep files in
one folder and it's subfolders (say folder xyz and it's sub-folders) for 5
months. I thought I could just put an If statement in the selectfiles
Subroutine like so ( I also left it in the code where I thought it should
go)
if folder = "xyz" then
vKillDate = date() - 150
but that did not work. Can anyone help?
Thanks I appreciate it.
Here is the Script I use. It was something I had downloaded several years
ago
'**************************************************************
'VBSCRIPT to delete files
' folder to start search in...
path = "c:\proddev"
' delete files older than 14 days from proddev folder and subfolders...
killdate = date() - 14
arFiles = Array()
set fso = createobject("scripting.filesystemobject")
' Don't do the delete while you still are looping through a
' file collection returned from the File System Object (FSO).
' The collection may get mixed up.
' Create an array of the file objects to avoid this.
'
SelectFiles path, killdate, arFiles, true
nDeleted = 0
for n = 0 to ubound(arFiles)
'=================================================
' Files deleted via FSO methods do *NOT* go to the recycle bin!!!
'=================================================
on error resume next 'in case of 'in use' files...
arFiles(n).delete true
if err.number <> 0 then
wscript.echo "Unable to delete: " & arFiles(n).path
else
nDeleted = nDeleted + 1
end if
on error goto 0
next
'msgbox nDeleted & " of " & ubound(arFiles)+1 _
' & " eligible files were deleted"
sub SelectFiles(sPath,vKillDate,arFilesToKill,bIncludeSubFolders)
on error resume next
'select files to delete and add to array...
'
set folder = fso.getfolder(sPath)
set files = folder.files
'*******************************************
'****** this is where I thought I could put an if condition
if folder = "xyz" then
vKillDate = date() - 150
end if
'********************************************
for each file in files
' uses error trapping around access to the
' Date property just to be safe
'
dtlastmodified = null
on error resume Next
dtlastmodified = file.datelastmodified
on error goto 0
if not isnull(dtlastmodified) Then
if dtlastmodified < vKillDate then
count = ubound(arFilesToKill) + 1
redim preserve arFilesToKill(count)
set arFilesToKill(count) = file
end if
end if
next
if bIncludeSubFolders then
for each fldr in folder.subfolders
SelectFiles fldr.path,vKillDate,arFilesToKill,true
next
end if
end sub