Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Scheduled file deletion

Posted on 2007-04-09
19
Medium Priority
?
352 Views
Last Modified: 2010-08-05
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?
0
Comment
Question by:hbsr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
19 Comments
 
LVL 67

Expert Comment

by:sirbounty
ID: 18877890
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
 
LVL 67

Expert Comment

by:sirbounty
ID: 18877891
Save as DelOldFiles.vbs
0
 
LVL 30

Expert Comment

by:LauraEHunterMVP
ID: 18877908
[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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:hbsr
ID: 18877913
will this script delete only files located in the "scan" folder?  
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18877926
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
 

Author Comment

by:hbsr
ID: 18877946
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
 
LVL 67

Expert Comment

by:sirbounty
ID: 18878388
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
 

Author Comment

by:hbsr
ID: 18878599
No prompts and the removal log is blank showing it ran for one second.
0
 

Author Comment

by:hbsr
ID: 18878676
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
 
LVL 67

Expert Comment

by:sirbounty
ID: 18878769
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
 

Author Comment

by:hbsr
ID: 18878848
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
 
LVL 67

Accepted Solution

by:
sirbounty earned 1000 total points
ID: 18878902
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
 

Author Comment

by:hbsr
ID: 18889723
This works perfectly!  Which lines would have to be removed in order to allow the script to run without prompts?  
0
 

Author Comment

by:hbsr
ID: 18889902
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
 
LVL 67

Expert Comment

by:sirbounty
ID: 18890345
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
 

Author Comment

by:hbsr
ID: 18893306
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
 
LVL 67

Expert Comment

by:sirbounty
ID: 18893324
Yes, adding that line at the very top should prevent the 'hiccup'...
0
 
LVL 1

Expert Comment

by:Computer101
ID: 20729447
Forced accept.

Computer101
EE Admin
0

Featured Post

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The HP utility "HP Lights-Out Online Configuration Utility for Windows Server 2003/2008" could be of great use when it comes to remotely configure a HP servers ILO WITHOUT rebooting the server. We would only need to create and run scripts using thi…
While rebooting windows server 2003 server , it's showing "active directory rebuilding indices please wait" at startup. It took a little while for this process to complete and once we logged on not all the services were started so another reboot is …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question