1.

Solve : Check several locations in a .vbs script to delete files by date.?

Answer»

I found a script on the forums by Sidewinder to delete files by date for a specific location, the code is below and I would like to be able to specify different locations and have it loop through for each location. Either by looking at an external file or by adding something in the current script and listing out each location. Can anyone assist me with this?

Code: [Select]Set FSO = CreateObject("Scripting.FileSystemObject")
fs = "c:\dvr_data"
ShowFiles(fs)

Sub ShowFiles(Fld)
Set k = fso.GetFolder(Fld)
Set s = k.SubFolders
Set kf = k.Files

For Each objFile In kf
If fso.GetExtensionName(objFile) = "avi" then
If objFile.DateCreated < date - 3 Then
WScript.Echo objFile & " " & objFile.DateCreated
'fso.DeleteFile objfile
End If
End if
Next

For Each SubFolder In s
ShowFiles SubFolder
Next
End Sub

I had a beautiful ELOQUENT response to this post, but just as I hit the post button, CH took a header into the mountain again. You'll have to deal with the salvaged wreakage:

If you don't have too many directories, you can hardcode an array otherwise you can use an external file.

Array:
Code: [Select]Set fso = CreateObject("Scripting.FileSystemObject")
arrDir = Array("c:\dvr_data","c:\dir2","c:\dir3") 'use correct directories

For Each fs In arrDir
ShowFiles(fs)
Next

Sub ShowFiles(Fld)
Set k = fso.GetFolder(Fld)
Set s = k.SubFolders
Set kf = k.Files

For Each objFile In kf
If fso.GetExtensionName(objFile) = "avi" then
If objFile.DateCreated < date - 3 Then ' age factor is days
WScript.Echo objFile & " " & objFile.DateCreated
'fso.DeleteFile objfile
End If
End if
Next

For Each SubFolder In s
ShowFiles SubFolder
Next
End Sub

External File:
Code: [Select]Const ForReading = 1
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("c:\input.txt", ForReading) 'point to correct file
Do While f.AtEndOfStream <> True
fs = f.ReadLine
ShowFiles(fs)
Loop
f.Close

Sub ShowFiles(Fld)
Set k = fso.GetFolder(Fld)
Set s = k.SubFolders
Set kf = k.Files

For Each objFile In kf
If fso.GetExtensionName(objFile) = "avi" then
If objFile.DateCreated < date - 3 Then ' age factor is days
WScript.Echo objFile & " " & objFile.DateCreated
'fso.DeleteFile objfile
End If
End if
Next

For Each SubFolder In s
ShowFiles SubFolder
Next
End Sub

Both snippets list files to be deleted. When satisfied, comment WScript.Echo objFile & " " & objFile.DateCreated and uncomment 'fso.DeleteFile objfile

If you use the array method, each directory must be quoted with either a space or comma as separator. If you use the external file method, the file should contain one directory per line.

Good luck. Ok gave it a shot with the external file and this is what was returned.

Quote

C:\delbydate.vbs(11, 4) Microsoft VBScript runtime error: Path not found

I am assuming it is stating 11 line and 4th character in there is an issue?

At that location is the following code.

Code: [Select] Set k = fso.GetFolder(Fld)The error is reproducible by entering an invalid directory name in the external file. I added some code for the snippet to crash and burn if a directory from the external file is not found.

Code: [Select]Const ForReading = 1
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("c:\temp\input.txt", ForReading) 'point to correct file
Do While f.AtEndOfStream <> True
fs = f.ReadLine
ShowFiles(fs)
Loop
f.Close

Sub ShowFiles(Fld)
If fso.FolderExists(fld) Then
Set k = fso.GetFolder(Fld)
Else
WScript.Echo "Invalid Directory:",fld
WScript.Quit
End if

Set s = k.SubFolders
Set kf = k.Files

For Each objFile In kf
If fso.GetExtensionName(objFile) = "txt" then
If objFile.DateCreated < date - 3 Then ' age factor is days
WScript.Echo objFile & " " & objFile.DateCreated
'fso.DeleteFile objfile
End If
End if
Next

For Each SubFolder In s
ShowFiles SubFolder
Next
End Sub




But those directories work fine if I use them in the first script, why would that happen?

The paths are UNC name btw not sure if that effects it differently in this version of the script.Your not giving us enough information. Did the last posted version of the script ISSUED an Invalid Directory message? Can you GIVE us an example of what is in the external file?

This script has been tested with directories containing embedded spaces and UNC paths, with no problems.



Are you STILL getting a VBScript error, or just no results at all? Keep in mind the script is filtering only txt extensions.That was it I was missing a letter in one of the paths, sorry about not giving as much detail. Looks like the script doesn't do anything once it runs into a bad path.

Thanks so much for you assistance Sidewinder!


Discussion

No Comment Found