Re: want to create script to auto delete folders by Jonathan
Jonathan
Thu Jan 25 00:38:14 CST 2007
You could use something like this vbs, it will allow you to alter the
amount of days to look back where necessary and you can use have the
script look in multiple directories if necessary just by duplicating
the appropriate line below:
Set up a Windows Scheduled task to run C:\WINDOWS\System32\cscript.exe
"path of vbscript" to automate the process.
set Shell = CreateObject( "WScript.Shell")
set fso = CreateObject( "Scripting.FileSystemObject")
set SystemEnv = Shell.Environment( "SYSTEM")
set ProcessEnv = Shell.Environment( "PROCESS")
'Anything older than todays date minus (number below) will be deleted
dtCutoff = DateAdd( "d", -20, Now())
'Files will be deleted from this folder and all sub folders (no
physical folders will be deleted)
DeleteOldFiles fso.GetFolder( "D:\Temp"), dtCutoff
DeleteOldFiles fso.GetFolder( "D:\Export"), dtCutoff
DeleteOldFiles fso.GetFolder( "D:\Import"), dtCutoff
function DeleteOldFiles( Folder, dtCutoff)
'Determine location and name of log file (log file appends itself)
set logf = fso.OpenTextFile( "E:\Deletelog.txt", 8, true)
logf.WriteLine( Now & " " & Folder.Path)
logf.Close
on error resume next
' Delete old files from this directory
for each File in Folder.Files
if File.DateCreated < dtCutoff then
WScript.Echo File.Path
File.Delete
end if
next
' Delete old files from all sub directories
for each SubFolder in Folder.SubFolders
DeleteOldFiles SubFolder, dtCutoff
next
end function