bmlguy
asked on
VB script, delete files older than 7 days, but skip specific files
Hi I want to use this code that deletes files older than 7 days but add logic that doesn't delete any *-Init.log files. I know this has to be pretty simple, but I'm rusty with vb script.
Thanks
Thanks
Dim strPath = "C:\Temp\"
Dim iDays = 7
Set objFolder = objFSO.GetFolder(strPath)
For Each objItem In objFolder.Files
If objItem.DateLastModified < date() - iDays
if objIte then
objFSO.DeleteFile(strPath & objitem.Name)
End If
Next
woops, hang on, reverse logic
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Here's some other references to manipulating files:
http://www.experts-exchang e.com/Prog ramming/La nguages/Vi sual_Basic /VB_Script /Q_2324976 6.html
http://www.experts-exchang
'I've also used a subroutine like this to delete files older than 24 hours from our MFD scanner. It runs as a scheduled task once a day.
objStartFolder = "e:\scans" 'folder to monitor
intHoursToKeep = 24 'change this to modify the hours to keep
' ********************* Do not modify below this line *********************
strNOW = Now
'title
WScript.Echo("Folder,File,Last Modified,Present time,Status")
'Process files in root of start folder
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(objStartFolder)
Set colFiles = objFolder.Files
For Each objFile in colFiles
processFile objFile,objFolder
Next
'process file is all subfolders of the start folder (recursive)
ShowSubfolders objFSO.GetFolder(objStartFolder)
Sub ShowSubFolders(Folder)
For Each Subfolder in Folder.SubFolders
Set objFolder = objFSO.GetFolder(Subfolder.Path)
Set colFiles = objFolder.Files
For Each objFile in colFiles
processFile objFile,objFolder
Next
ShowSubFolders Subfolder
Next
End Sub
'deletes the file if older than hours to keep
sub processFile(objfile,objfolder)
If DateDiff("h", objFile.DateLastModified, Now) > intHoursToKeep Then
WScript.Echo(objfolder.path & "," & objfile.name & "," & objfile.DateLastModified & "," & strNOW & ",Deleted")
objFSO.DeleteFile objFile
Else
WScript.Echo(objfolder.path & "," & objfile.name & "," & objfile.DateLastModified & "," & strNOW& ",Not Deleted")
End if
End sub
ASKER
Perfect, thanks
Open in new window