hbsr
asked on
Scheduled file deletion
Windows 2003 File Server - We redirect users "My Documents" to this file server via group policy. Users scan documents to their my documents folder on this file server. I need to delete the scanned documents from each user's "my docs" folder every 7 days. The "my docs" folder path on this server is as follows:
G:\\rfolders\username\docs \scan
I need to delete all files from this scan folder every 7 days for all users so I need to setup something like this on the file server:
del g:\\rfolders\%username%\do cs\scan\*. *
Unfortunately, the %username% only works with the account logged into the server. I need to hit each user's folder "%username%". Is there a way to do this without adding a line to a login script?
G:\\rfolders\username\docs
I need to delete all files from this scan folder every 7 days for all users so I need to setup something like this on the file server:
del g:\\rfolders\%username%\do
Unfortunately, the %username% only works with the account logged into the server. I need to hit each user's folder "%username%". Is there a way to do this without adding a line to a login script?
Save as DelOldFiles.vbs
[1] Are there any sub-folders underneath that \RFOLDERS parent that you would -not- want to delete the contents of?
[2] Once you CD down to ~\%username%\, is that \docs\scan\*.* path always the same?
If so, you could do this using VBScript as a scheduled task. The following VBScript code will enumerate every sub-folder of a given parent folder:
Set objFSO = CreateObject("Scripting.Fi leSystemOb ject")
Set objFolder = objFSO.GetFolder("C:\FSO")
Set colSubfolders = objFolder.Subfolders
For Each objSubfolder in colSubfolders
Wscript.Echo objSubfolder.Name, objSubfolder.Size
Next
Then combine that with the following code snippet, which will delete the contents of a given folder:
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=imper sonate}!\\ " & strComputer & "\root\cimv2")
Set colFileList = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Directory.Name='C:\ Scripts'} Where " _
& "ResultClass = CIM_DataFile")
For Each objFile In colFileList
Wscript.Echo objFile.Name
Next
Hope this helps.
Laura E. Hunter - Microsoft MVP: Windows Server - Networking
[2] Once you CD down to ~\%username%\, is that \docs\scan\*.* path always the same?
If so, you could do this using VBScript as a scheduled task. The following VBScript code will enumerate every sub-folder of a given parent folder:
Set objFSO = CreateObject("Scripting.Fi
Set objFolder = objFSO.GetFolder("C:\FSO")
Set colSubfolders = objFolder.Subfolders
For Each objSubfolder in colSubfolders
Wscript.Echo objSubfolder.Name, objSubfolder.Size
Next
Then combine that with the following code snippet, which will delete the contents of a given folder:
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=imper
Set colFileList = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Directory.Name='C:\
& "ResultClass = CIM_DataFile")
For Each objFile In colFileList
Wscript.Echo objFile.Name
Next
Hope this helps.
Laura E. Hunter - Microsoft MVP: Windows Server - Networking
ASKER
will this script delete only files located in the "scan" folder?
Sorry, change this sub:
Sub DeleteFiles(strSrc)
For Each file In strSrc.Files
If Instr(lcase(file.path), "\docs\scan\") > 0 Then
If DateDiff("d", file.datelastaccessed, dtOld) >= intDel Then
objOutputFile.WriteLine file.Path & " was removed at " & Now
objFSO.DeleteFile file.Path
End If
End If
Next
End Sub
Sub DeleteFiles(strSrc)
For Each file In strSrc.Files
If Instr(lcase(file.path), "\docs\scan\") > 0 Then
If DateDiff("d", file.datelastaccessed, dtOld) >= intDel Then
objOutputFile.WriteLine file.Path & " was removed at " & Now
objFSO.DeleteFile file.Path
End If
End If
Next
End Sub
ASKER
I don't have the scripting knowledge but here is the EXACT path for each user. I used the above path as an example. All I want to do is delete the files located in the same "scan soft" folder that are older than 7 days. The folder structure is identical for all users.
On the file server - G:\rfolders\username\my paper port docs\scansoft
On the file server - G:\rfolders\username\my paper port docs\scansoft
I'll put some 'stops' in here, so that you can step through it...
If you see it's about to delete a file you didn't expect answer "no" to the "Continue prompt" and it'll stop the script - I'll comment these lines so that you can easily remove them once you've confirmed it works.
This should be setup exactly like you requested...let me know.
'DelOldFiles.vbs (output stored in FilesRemoved.Log)
Dim objFSO: Set objFSO = CreateObject("Scripting.Fi leSystemOb ject")
Dim objOutputFile: Set objOutputFile = objFSO.CreateTextFile("C:\ FilesRemov ed.log")
Dim dtOld
strSourceFolder = "G:\rfolders\"
With objOutputFile
.WriteLine "========================= =="
.WriteLine "Removal summary for " & Date
.WriteLine "=-=-=-=-=-=-=-=-=-=-=-=-= -="
.WriteLine
End With
'intDel is the number of days old you want to check for
intDel = 7
dtOld = DateAdd("d", -intDel, Date)
Dim objFolder: Set objFolder = objFSO.GetFolder(strSource Folder)
For Each subfld In objFolder.SubFolders
DeleteFiles objFSO.GetFolder(subfld & "\my paper port docs\scansoft")
Next
With objOutputFile
.WriteLine
.WriteLine "Process completed at " & Now
.WriteLine "========================= =="
.Close
End With
Set objOutputFile = Nothing
Set objFSO = Nothing
wscript.quit
Sub DeleteFiles(strSrc)
For Each file In strSrc.Files
If DateDiff("d", file.datelastaccessed, dtOld) >= intDel Then
strResp=Msgbox("About to delete " & file, vbYesNo, "Continue?")
If strResp = vbNo Then wscript.quit
objOutputFile.WriteLine file.Path & " was removed at " & Now
objFSO.DeleteFile file.Path
End If
Next
End Sub
If you see it's about to delete a file you didn't expect answer "no" to the "Continue prompt" and it'll stop the script - I'll comment these lines so that you can easily remove them once you've confirmed it works.
This should be setup exactly like you requested...let me know.
'DelOldFiles.vbs (output stored in FilesRemoved.Log)
Dim objFSO: Set objFSO = CreateObject("Scripting.Fi
Dim objOutputFile: Set objOutputFile = objFSO.CreateTextFile("C:\
Dim dtOld
strSourceFolder = "G:\rfolders\"
With objOutputFile
.WriteLine "=========================
.WriteLine "Removal summary for " & Date
.WriteLine "=-=-=-=-=-=-=-=-=-=-=-=-=
.WriteLine
End With
'intDel is the number of days old you want to check for
intDel = 7
dtOld = DateAdd("d", -intDel, Date)
Dim objFolder: Set objFolder = objFSO.GetFolder(strSource
For Each subfld In objFolder.SubFolders
DeleteFiles objFSO.GetFolder(subfld & "\my paper port docs\scansoft")
Next
With objOutputFile
.WriteLine
.WriteLine "Process completed at " & Now
.WriteLine "=========================
.Close
End With
Set objOutputFile = Nothing
Set objFSO = Nothing
wscript.quit
Sub DeleteFiles(strSrc)
For Each file In strSrc.Files
If DateDiff("d", file.datelastaccessed, dtOld) >= intDel Then
strResp=Msgbox("About to delete " & file, vbYesNo, "Continue?")
If strResp = vbNo Then wscript.quit
objOutputFile.WriteLine file.Path & " was removed at " & Now
objFSO.DeleteFile file.Path
End If
Next
End Sub
ASKER
No prompts and the removal log is blank showing it ran for one second.
ASKER
It started working. Thanks for adding the prompts. It seemed to skip over four user names (that begin with the letter A) that do have docs in the "scansoft" folder that contain files older than 7 days. After doing about three users, it returned an error:
line: 22
Char: 3
Error: Path not found
Code: 800A004c
line: 22
Char: 3
Error: Path not found
Code: 800A004c
Okay - this version's a little more 'chatty'...difficult to say what the problem is without 'being' there, so please post the output log here:
Granted - there are 3 essential dates to use with this script. This one uses the last accessed version - meaning the file can't have been 'touched' in more than 7 days...
You also have the option of created date and last modified date...would one of those work better?
'DelOldFiles.vbs (output stored in FilesRemoved.Log)
Dim objFSO: Set objFSO = CreateObject("Scripting.Fi leSystemOb ject")
Dim objOutputFile: Set objOutputFile = objFSO.CreateTextFile("C:\ FilesRemov ed.log")
Dim dtOld
strSourceFolder = "G:\rfolders\"
With objOutputFile
.WriteLine "========================= =="
.WriteLine "Removal summary for " & Date
.WriteLine "=-=-=-=-=-=-=-=-=-=-=-=-= -="
.WriteLine
End With
'intDel is the number of days old you want to check for
intDel = 7
dtOld = DateAdd("d", -intDel, Date)
Dim objFolder: Set objFolder = objFSO.GetFolder(strSource Folder)
For Each subfld In objFolder.SubFolders
objOutput.WriteLine "Beginning scan of " & subfld & "\my paper port docs\scansoft"
DeleteFiles objFSO.GetFolder(subfld & "\my paper port docs\scansoft")
Next
With objOutputFile
.WriteLine
.WriteLine "Process completed at " & Now
.WriteLine "========================= =="
.Close
End With
Set objOutputFile = Nothing
Set objFSO = Nothing
wscript.quit
Sub DeleteFiles(strSrc)
For Each file In strSrc.Files
wscript.echo "Reviewing " & file
If DateDiff("d", file.datelastaccessed, dtOld) >= intDel Then
wscript.echo "File is older than 7 days: " & file
strResp=Msgbox("About to delete " & file, vbYesNo, "Continue?")
If strResp = vbNo Then wscript.quit
objOutputFile.WriteLine file.Path & " was removed at " & Now
objFSO.DeleteFile file.Path
End If
Next
End Sub
Granted - there are 3 essential dates to use with this script. This one uses the last accessed version - meaning the file can't have been 'touched' in more than 7 days...
You also have the option of created date and last modified date...would one of those work better?
'DelOldFiles.vbs (output stored in FilesRemoved.Log)
Dim objFSO: Set objFSO = CreateObject("Scripting.Fi
Dim objOutputFile: Set objOutputFile = objFSO.CreateTextFile("C:\
Dim dtOld
strSourceFolder = "G:\rfolders\"
With objOutputFile
.WriteLine "=========================
.WriteLine "Removal summary for " & Date
.WriteLine "=-=-=-=-=-=-=-=-=-=-=-=-=
.WriteLine
End With
'intDel is the number of days old you want to check for
intDel = 7
dtOld = DateAdd("d", -intDel, Date)
Dim objFolder: Set objFolder = objFSO.GetFolder(strSource
For Each subfld In objFolder.SubFolders
objOutput.WriteLine "Beginning scan of " & subfld & "\my paper port docs\scansoft"
DeleteFiles objFSO.GetFolder(subfld & "\my paper port docs\scansoft")
Next
With objOutputFile
.WriteLine
.WriteLine "Process completed at " & Now
.WriteLine "=========================
.Close
End With
Set objOutputFile = Nothing
Set objFSO = Nothing
wscript.quit
Sub DeleteFiles(strSrc)
For Each file In strSrc.Files
wscript.echo "Reviewing " & file
If DateDiff("d", file.datelastaccessed, dtOld) >= intDel Then
wscript.echo "File is older than 7 days: " & file
strResp=Msgbox("About to delete " & file, vbYesNo, "Continue?")
If strResp = vbNo Then wscript.quit
objOutputFile.WriteLine file.Path & " was removed at " & Now
objFSO.DeleteFile file.Path
End If
Next
End Sub
ASKER
Last modified would be best. Same type of result as before execept this time I received the error as soon as the script was run:
Line: 20
Char: 3
Error: Object required: 'objOutput'
Code: 800A01A8
I copied one of the folder paths
G:\RFOLDERS\BLim\My PaperPort Documents\ScanSoft
Where "BLim" would be the %username% - We only need to delete the files in scansoft that have a last modified date older than 7 days and there are no subfolders within "scansoft"...
Line: 20
Char: 3
Error: Object required: 'objOutput'
Code: 800A01A8
I copied one of the folder paths
G:\RFOLDERS\BLim\My PaperPort Documents\ScanSoft
Where "BLim" would be the %username% - We only need to delete the files in scansoft that have a last modified date older than 7 days and there are no subfolders within "scansoft"...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
This works perfectly! Which lines would have to be removed in order to allow the script to run without prompts?
ASKER
I may have jumped the gun. The script is working properly but it seems to stop every once in a while on any random file that has the same ntfs perms as the previous ones that were deleted. The user and domain admins have full access on the file (I am logged into the server as a domain admin). The error is:
line: 43
char: 7
Error: Permission Denied
Code: 800A0046
line: 43
char: 7
Error: Permission Denied
Code: 800A0046
Those errors are tough to diagnose, unfortunately - is the file read-only, perhaps?
You can 'skip' it so that your script continues running by placing an
On Error Resume Next
at the start of the script...
You can 'skip' it so that your script continues running by placing an
On Error Resume Next
at the start of the script...
ASKER
It was read-only....so, what lines are required to be removed in order to allow this script to run without prompts? Also, do I just add "On Error Resume Next" to the very first line. Pardon my lack of knowledge with scripting!
Yes, adding that line at the very top should prevent the 'hiccup'...
Forced accept.
Computer101
EE Admin
Computer101
EE Admin
Dim objFSO : Set objFSO = CreateObject("Scripting.Fi
Dim objOutputFile:Set objOutputFile = objFSO.CreateTextFile("C:\
Dim dtOld
strSourceFolder = "G:\rfolders\"
With objOutputFile
.WriteLine "=========================
.WriteLine "Removal summary for " & Date
.WriteLine "=-=-=-=-=-=-=-=-=-=-=-=-=
.WriteLine
End With
'intDel is the number of days old you want to check for
intDel = 7
dtOld = DateAdd("d", -intDel, Date)
DeleteFolder objFSO.GetFolder(strSource
With objOutputFile
.WriteLine
.WriteLine "Process completed at " & Now
.WriteLine "=========================
.Close
End With
Set objOutputFile = Nothing
Set objFSO = Nothing
wscript.quit
Sub DeleteFolder(strSource)
DeleteFiles strSource
For Each fld In strSource.SubFolders
DeleteFolder fld
Next
End Sub
Sub DeleteFiles(strSrc)
For Each file In strSrc.Files
If DateDiff("d", file.datelastaccessed, dtOld) >= intDel Then
objOutputFile.WriteLine file.Path & " was removed at " & Now
objFSO.DeleteFile file.Path
End If
Next
End Sub