Solved

VB script, delete files older than 7 days, but skip specific files

Posted on 2009-05-20
5
893 Views
Last Modified: 2012-05-07
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


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

Open in new window

0
Comment
Question by:bmlguy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
5 Comments
 
LVL 26

Expert Comment

by:Pber
ID: 24433379
Try this:
strPath = "C:\Temp\"
iDays = 7
mask = "-init.log"
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
 
Set objFolder = objFSO.GetFolder(strPath)
For Each objItem In objFolder.Files
   If objItem.DateLastModified < date() - iDays then
		WScript.Echo(objitem.Name)
		If InStr(1,lcase(objitem.Name),lcase(mask)) Then
			WScript.Echo(objitem.Name & " Deleted.")
			objFSO.DeleteFile objItem
		End if     
   End If
Next

Open in new window

0
 
LVL 26

Expert Comment

by:Pber
ID: 24433388
woops, hang on, reverse logic
 
0
 
LVL 26

Accepted Solution

by:
Pber earned 500 total points
ID: 24433401
Try this:
 

strPath = "C:\Temp\"
iDays = 7
mask = "-init.log"
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
 
Set objFolder = objFSO.GetFolder(strPath)
For Each objItem In objFolder.Files
   If objItem.DateLastModified < date() - iDays then
		WScript.Echo(objitem.Name)
		If InStr(1,lcase(objitem.Name),lcase(mask)) = 0 Then
			WScript.Echo(objitem.Name & " Deleted.")
			objFSO.DeleteFile objItem
		Else
			WScript.Echo(objitem.Name & " not Deleted.")
		End if     
   End If
Next

Open in new window

0
 
LVL 26

Expert Comment

by:Pber
ID: 24433641
Here's some other references to manipulating files:
http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_23249766.html
 
 

'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

Open in new window

0
 

Author Closing Comment

by:bmlguy
ID: 31583563
Perfect, thanks
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question