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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

delete files but not all in a folder

Status
Not open for further replies.

scrsadmin

Technical User
May 3, 2004
62
US
I need help with some logic or more.

My code is suppose to delete files in a folder if they are older then 7 days and they are not a file listed in files_keep.txt.

I need it to read through the files_keep.txt file and insure its not in there before it deletes the file. I don't want to read through the files_keep.txt file each time its looking to delete a file. So i thought i would read it into an array and then test againt the array to see if the file is listed before deleting. But couldn't figure out the logic to get this done.



code:


Path1 = "C:\jboss-3.2.5\server\all\log"
file_keep = "C:\temp\files_keep.txt"


Dim fso, oFolder, oFile, oSubFolder, file_to_keep

ret_time = 7 'retention time

Set fso = createobject("Scripting.FileSystemObject")

Set oFolder = fso.GetFolder(Path1)

Set f1=fso_OpenTextFile(file_keep,1 )
MyArray = Split(f1.ReadAll, vbNewLine)

For Each oFile In oFolder.files
If DateDiff("d", oFile.datelastmodified,Now) > ret_time Then

If oFile <> Marray(iocount) Then
WScript.Echo "About to delete " & oFile
oFile.Delete True
End if


End If
Next
 
Wouldn't it easier to play with a Dictionary object ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
I could use a Dictionary object but still have a problem understanding the logic to whent to test for the file before deleting to insure it's not one of the ones listed in the files_keep.txt

I'm so confused.
 
problem understanding the logic to whent to test for the file
I'd use the Exists method.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Here is a thread in the VB 5 & 6 forum but you should be able to easily adapt the code.

thread222-1376747

Swi
 
Code:
For Each oFile In oFolder.files
       If DateDiff("d", oFile.datelastmodified,Now) > ret_time Then
       		matchCount = 0
       		For x = 0 To UBound(MyArray)
       			If LCase(oFile.Name) = LCase(myArray(x) Then
       				matchCount = 1
       			End If
       		Next
       		If matchCount = 1 Then     
            	WScript.Echo "About to delete " & oFile
                oFile.Delete True
            Else
            	'File is marked for saving, do nothing
            End if    
    End If
Next

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Thanks to everyone.

Marcdmac, your for loop is what i need to understand the logic, Thanks

I have attached my code that is working in production.

PHV and SWI, I didn't use Dictionary object as i don't know enough about it yet. But i am going to play with them and plan on using them in my updated code.


Path1 = "C:\jboss-3.2.5\server\all\log"
file_keep = "C:\cleanup\files_keep.txt"

Dim f1, ret_time, fso, oFolder, oFile, file_to_keep




Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colSettings = objWMIService.ExecQuery _
("Select * from Win32_ComputerSystem")
For Each objComputer in colSettings

strComputer = lCase(objComputer.Name)
'WScript.Echo strComputer

Next

Select Case strComputer
Case "applprod" ret_time = 120
Case "appldev" ret_time = 7
Case "appltest" ret_time = 30
Case Else
End select



Set fso = createobject("Scripting.FileSystemObject")

Set oFolder = fso.GetFolder(Path1)

Set f1=fso_OpenTextFile(file_keep,1 )
MyArray = Split(f1.ReadAll, vbNewLine)

For Each oFile In oFolder.files
If DateDiff("d", oFile.datelastmodified,Now) > ret_time Then
matchCount = 0
For x = 0 To UBound(MyArray)
If LCase(oFile.Name) = LCase(myArray(x)) Then
matchCount = 1
Exit For
End If

Next
If matchCount = 0 Then
'WScript.Echo "About to delete " & oFile
update_log("File to delete ")
oFile.Delete True
Else
'File is marked for saving, do nothing
'WScript.Echo "this file " & oFile & " is marked for keep"
End if
End If
Next





Set oSubFolder = Nothing
Set oFolder = Nothing
Set fso = Nothing

'********************************************************************


'********************************************************************

Sub update_log(current_process)

If (fso.FileExists("c:\cleanup\log\cleanup.log")) Then
Set outputfile = fso_OpenTextFile("c:\cleanup\log\cleanup.log",8)
'MsgBox "output file exists"
else
Set outputfile = fso_OpenTextFile("c:\cleanup\log\cleanup.log",2,True)
End If

' outputfile.WriteLine "File Name: " & objFile.FileName & "." & objFile.Extension

outputfile.WriteLine current_process & " " & oFile.Name & " " & Date & " " & Time
outputfile.Close

End Sub

'********************************************************************


 
If your array of files to compare against is large I would definitely look into the dictionary object because it will be much more efficient.

Swi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top