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

Re: Help with VBscript to delete files by Pegasus

Pegasus
Wed Dec 05 15:49:16 PST 2007


"Jeff Belorit" <jbelorit@mbi-inc.com> wrote in message
news:7B82FABF-307D-40BE-8623-8AB774B9E683@microsoft.com...
> 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
>

Some days I feel lazy and disinclined to re-invent the wheel. Instead
of writing and debugging a 60-line VB Script program, I knock
together a 6-line batch program, relying on the work that others
have done before me. Below is the result.

$@echo off
$Set Target=c:\proddev
$set Skip=c:\proddev\xyz
$
$dir /s /b /ad "%Target%" | find /i "%Skip%" > "%temp%\150days.txt"
$dir /s /b /ad "%Target%" | find /i /v "%Skip%" > "%temp%\14days.txt"
$echo %target% >> "%temp%\14days.txt"
$
$for /F "delims=" %%a in ('type "%temp%\14days.txt"') do xxcopy /L /rs
/db#14 /ed /yy "%%a\*.*"
$for /F "delims=" %%a in ('type "%temp%\150days.txt"') do xxcopy /L /rs
/db#150 /ed /yy "%%a\*.*"

I suggest you do this:
1. Copy and paste the code into your own batch file "DelOld.bat".
2. Remove the $ characters. They only serve to mark the beginning of
each line.
3. Set the Target and the Skip variables.
4. Download xxcopy.exe from any of a number of sites.
5. Execute your batch file like so:
DelOld.bat > c:\Test.txt
6. Examine c:\Test.txt carefully to ensure that the right files would be
deleted.
7. Remove the /L switch in the two xcopy commands to activate the command.