Scanner Folder Clean up
I have a vbs files that will scour a network share and delete any file over two days old. This has stopped working about a week ago. I've checked permission, passwords, accounts and even ran the script by hand. Were starting to think maybe theres to many folder and somthing in the script needs to be changed. Ive been readin alot online and im getting no where. wanted to ask for some help if possiable. heres the script.
On Error Resume Next
Dim fso, date_var, share, MyFile
Set fso = CreateObject("Scripting.FileSystemObject")
date_var = Month(Now) & "-" & Day(Now) & "-" & Year(Now) & "-" & Hour(Now) & "-" & Minute(Now) & "-" & Second(Now)
share = "C:\Scan_File_Delete"
Set MyFile = fso.CreateTextFile(share & "\Scan_Folder_Delete_Log_RIC.log", True)
MyFile.WriteLine "Start scan folder deletion: " & date_var
Recurse("\\ricfiles\Depts\Scandata")
date_var = ""
date_var = Month(Now) & "-" & Day(Now) & "-" & Year(Now) & "-" & Hour(Now) & "-" & Minute(Now) & "-" & Second(Now)
MyFile.WriteLine "End scan folder deletion: " & date_var
MyFile.Close 'closes MyFile
Sub Recurse(Path)
On Error Resume Next
Dim objFSO
Dim objRoot
Dim objFiles
Dim objFolders
Dim colFolder
Dim colFile
Dim intCounter
Dim FoldersArray()
Dim dteFileDate
Dim intRemainder
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objRoot = objFSO.getfolder(Path)
Set objFiles = objRoot.Files
Set objFolders = objRoot.SubFolders
ReDim FoldersArray(200)
intCounter = 0 'zero out the file count variable
'traverse through the subdirectories In the current directory
For Each colFolder In objFolders
intCounter = intCounter + 1
If intRemainder Mod 200 = 0 Then ReDim Preserve FoldersArray(intCounter + 200)
FoldersArray(intCounter) = colFolder.Path
Next
'traverse through the files In the current folder or subfolder
For Each colFile In objFiles
dteFileDate = colFile.DateLastModified
If Int(DateDiff("n",dteFileDate,Now)) > Int("1440") Then
objFSO.deletefile colFile.Path
End If
Next
'recurse through the current directory until
'all subfolders have been traversed
For intCounter = 1 To UBound(FoldersArray)
if FoldersArray(intCounter) <> "" Then
Recurse FoldersArray(intCounter)
Else
Exit For
End if
Next
End Sub
Answers (1)
Here's something I use with Powershell. If you want to give this a shot.
$Now = Get-Date #Enter number of days to check here. $Days = "2" #Path to File. $TargetFile = "\\server01\share_name\" $LastWrite = $Now.AddDays(-$Days) $Files = Get-Childitem $TargetFile -Recurse | Where {$_.LastWriteTime -le "$LastWrite"} foreach ($File in $Files) { if ($File -ne $NULL) { write-host "Deleting File $File" -ForegroundColor "DarkRed" Remove-Item $File.FullName | out-null } else { Write-Host "No more files to delete!" -foregroundcolor "Blue" } }
Comments:
-
Wow thanks! I'll give this a try and see how it works. Does it go into sub folders too? - DeepCover 12 years ago
-
With the -recurse command it should. I just use it for one specific folder so I've never tested that. - dugullett 12 years ago
-
ive been testing. Yes it does go into sub folders. This is way more simple the vbs too. Thanks again. Seems i need to keep trying to learn power shell :) - DeepCover 12 years ago
-
I sort of skipped over VB. I get it for the most part, but not like I do Powershell. These sites helped me.
http://www.itninja.com/link/learn-powershell
http://powershell.com/cs/blogs/ebookv2/default.aspx - dugullett 12 years ago
-
i think it will be the same for me. The more i work with powershell the more i like it. thanks again for the links! - DeepCover 12 years ago
-
Ive got the wierdest thing. I tested this out and everything was working fine. When i ran it on production servers i started getting error. The only different i can see what my test enviroment was full on hardware servers and i think the production is a NAS device.
"The item at Microsoft.Powershell.core\filesystem::\\server\path\folder has children and the recurse parapmeter was not specififed. if you continue all children will be romved with the item. are you sure you want to continue."
The Recusre is being call ive checked. And then the ones it did run on it deleted the folder. It looks like its treating each folder as a item and deleting them. - DeepCover 12 years ago-
This line basically says to get any items whose last write time is $LastWrite (days). There's nothing there that distinguishes a file or folder. There is probably something that can be added, but like I said I just use it in a couple of folders to delete log files. They do not contain other folders. I'll do some searching and see what I can come up with.
$Files = Get-Childitem $TargetFile -Recurse | Where {$_.LastWriteTime -le "$LastWrite"} - dugullett 12 years ago
-
yea ive been looking around some more too. it seems like it stops at the top of this folder structure. I believe this area could be a DFS. maybe it's not nesscary a NAS box or not. Thank you again for working me on this. - DeepCover 12 years ago
-
$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse | Where {$_.LastWriteTime -le "$LastWrite"} - DeepCover 12 years ago
-
meant to add this before that line
08
#----- define extension ----#
09
$Extension = "*.log" - DeepCover 12 years ago