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%\docs\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?
hbsrHelpdeskAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sirbountyCommented:
Schedule this to run daily on the server...


Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objOutputFile:Set objOutputFile = objFSO.CreateTextFile("C:\FilesRemoved.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)

DeleteFolder objFSO.GetFolder(strSourceFolder)

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
0
sirbountyCommented:
Save as DelOldFiles.vbs
0
LauraEHunterMVPCommented:
[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.FileSystemObject")
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=impersonate}!\\" & 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
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

hbsrHelpdeskAuthor Commented:
will this script delete only files located in the "scan" folder?  
0
sirbountyCommented:
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
0
hbsrHelpdeskAuthor Commented:
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

0
sirbountyCommented:
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.FileSystemObject")
Dim objOutputFile: Set objOutputFile = objFSO.CreateTextFile("C:\FilesRemoved.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(strSourceFolder)
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
0
hbsrHelpdeskAuthor Commented:
No prompts and the removal log is blank showing it ran for one second.
0
hbsrHelpdeskAuthor Commented:
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
0
sirbountyCommented:
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.FileSystemObject")
Dim objOutputFile: Set objOutputFile = objFSO.CreateTextFile("C:\FilesRemoved.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(strSourceFolder)
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
0
hbsrHelpdeskAuthor Commented:
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"...
0
sirbountyCommented:
Try this version...datelastmodified added...

'DelOldFiles.vbs  (output stored in FilesRemoved.Log)
Dim objFSO: Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objOutput: Set objOutput = objFSO.CreateTextFile("C:\FilesRemoved.log")
Dim dtOld
strSourceFolder = "G:\rfolders\"

With objOutput
  .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(strSourceFolder)
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 objOutput
  .WriteLine
  .WriteLine "Process completed at " & Now
  .WriteLine "==========================="
  .Close
End With

Set objOutput = Nothing
Set objFSO = Nothing
wscript.quit

Sub DeleteFiles(strSrc)
  For Each file In strSrc.Files
    wscript.echo "Reviewing " & file
    If DateDiff("d", file.datelastmodified, 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
      objOutput.WriteLine file.Path & " was removed at " & Now
      objFSO.DeleteFile file.Path
    End If
  Next
End Sub
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
hbsrHelpdeskAuthor Commented:
This works perfectly!  Which lines would have to be removed in order to allow the script to run without prompts?  
0
hbsrHelpdeskAuthor Commented:
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
0
sirbountyCommented:
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...
0
hbsrHelpdeskAuthor Commented:
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!
0
sirbountyCommented:
Yes, adding that line at the very top should prevent the 'hiccup'...
0
Computer101Commented:
Forced accept.

Computer101
EE Admin
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Server 2003

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.