Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Please help me modify this script... 2

Status
Not open for further replies.

omxcunn

Programmer
Sep 2, 2009
4
US
Can someone please help me change the following script here is what I would like to accomplish:

I would like to include all sub directories. And I would like have a total of 3 file extenstions. Please help.

Here is the script:

'************ Start of Code **********************

Option Explicit
On Error Resume Next
Dim oFSO, oFolder, sDirectoryPath
Dim oFileCollection, oFile, sDir
Dim iDaysOld

' Specify Directory Path From Where You want to clear the old files

sDirectoryPath = "C:\test"


' Specify Number of Days Old File to Delete

iDaysOld = 90

Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(sDirectoryPath)
Set oFileCollection = oFolder.Files

For each oFile in oFileCollection

'This section will filter the log file as I have used for for test case
'Specify the Extension of file that you want to delete
'and the number with Number of character in the file extension

If LCase(Right(Cstr(oFile.Name), 3)) = "txt" Then

If oFile.DateLastModified < (Date() - iDaysOld) Then
oFile.Delete(True)
End If

End If
Next

Set oFSO = Nothing
Set oFolder = Nothing
Set oFileCollection = Nothing
Set oFile = Nothing

'**************** End of Code *******************
 
perhaps try a Select Case for the file extentionsFor each
looks a simple approach but makes it difficult when you want to start your script with two parameters...list of file extentions to delete and the number of days..


For Each oFile in oFileCollection
Select Case LCase(Right(Cstr(oFile.Name), 3))
Case "txt", "doc", "mpg"
End Select
Next
 
for sub dir's you need to do some recursion, there are a few FAQ's and you will find numerous posts...
i love vbscript but i am sure you could find a one liner dos command to accomplish this task?
 
you could use a DIR command to find all *.txt files, export to a file and analyze each line. Validate the file timestamps without needing to build a collection object. Although, I not sure what repercussions (if any) exist.

CONST OBSOLETE_AGE = 90 'days

strLogFile = "C:\collection.log"
strPath = "C:\test"

set objFSO = CreateObject("Scripting.FileSystemObject")
set objShell = CreateObject("WScript.Shell")

'dump all *.txt file into a strLogFile
objShell.Run "dir *.txt /s /b > " & strLogFile

'open dump
set objLog = objFSO.OpenTextFile(strLogFile, 1, true)

'loop through each line of the file and compare timestamps.
do until (objLog.AtEndOfStream)
strFile = objLog.ReadLine
set objFile = objFSO.GetFile(strFile)
intAge = cint(now - objFile.DateLastModified)
if (intAge >= OBSOLETE_AGE) then objFile.Delete true
loop

-Geates
 
Could some one post the changes that i need to make and post the completed script. Thank you
 
I'm pretty sure I just did. Edit the dir command to include additional ext.

objShell.Run "dir *.txt *.doc *.mpg /s /b > " & strLogFile

-Geates

 
OK, so the code was conceptional. Here's executable code.

--------------------

on error resume next

CONST OBSOLETE_AGE = 90 'days

strLogFile = "C:\collection.log"
strPath = "C:\Users"

set objFSO = CreateObject("Scripting.FileSystemObject")
set objShell = CreateObject("WScript.Shell")

objShell.Run "%COMSPEC% /c dir " & strPath & "\*.txt " & strPath & "\*.exe " & strPath & "\*.mp3 /s /b > " & strLogFile, true

set objLog = objFSO.OpenTextFile(strLogFile, 1, true)
do until (objLog.AtEndOfStream)
strFile = objLog.ReadLine
set objFile = objFSO.GetFile(strFile)
intAge = cint(now - objFile.DateLastModified)
if (intAge >= OBSOLETE_AGE) then msgbox strFile & " is " & intAge & " days old"
loop
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top