Do not use on any
shared computer
September 6, 2008 06:10pm pdt
 
[x]
Attachment Details

Need script to autodelete files after a certain period of time and logs the names of the deleted files with time and date.

Tags: microsoft, server, 2003, scripting
Need script to autodelete files after a certain period of time and logs the names of the deleted files with time and date into a log file. Scriptlangauge doens't mind, but it has to be "nono friendly to edit" if time has to be adjusted or several dir's.
Start your free trial to view this solution
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

Question Stats
Zone: OS
Question Asked By: JSRhelpdesk
Solution Provided By: madperk
Participating Experts: 2
Solution Grade: A
Views: 11
Translate:
Loading Advertisement...
 
[+][-]Expert Comment by madperk
Expert Comment by madperk:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by madperk
Expert Comment by madperk:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by BillDL
Expert Comment by BillDL:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Author Comment by JSRhelpdesk
Author Comment by JSRhelpdesk:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by madperk
Expert Comment by madperk:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by madperk
Expert Comment by madperk:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Author Comment by JSRhelpdesk
Author Comment by JSRhelpdesk:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Accepted Solution by madperk
Accepted Solution by madperk:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by madperk
Expert Comment by madperk:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by madperk
Expert Comment by madperk:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Open Discussion
Open Discussion
 
Comment by JSRhelpdesk
@ MADPERK:

Is this tool also optimimised to scan within maps in the directory?
 
 
Comment by madperk
I am not sure what is meant by "within maps in the directory"?
 
 
Comment by JSRhelpdesk
Sory, translation issue, i ment folders, now we see that several users make folders to store there upload files in. So is it possible to adjust the script to also look into subfolders?

example:
c:\temp (thats where the files are)
Users creat like: c:\temp\mydownload etc etc
 
 
Comment by madperk
yes.  

One question. do you want to keep the folder structure inside the root directory or would you prefer to remove the folder structure?

FYI it will probably be this weekend before i can get back to it.  Valentines supper with my girl friend tonight.
 
 
Comment by JSRhelpdesk
Hahaha, girl before work, thats the way it should be :)

No, if a folder is empty because all files are outdated and deleted the folder can be trashed also.
This weekend is no problem, i allready am in debt because of your work!

So have a nice valentine and we'll meet again

Rowald
 
 
Comment by madperk
FYI having problems reading more than two folders deep.  here is what i have so far. The script will delete all files in temp and all files in FolderA or FolderB but no files in A1 or B1.

Still working on it.

Start Folder C:\temp
directory Structure
C:\temp
            \FolderA
                         \FolderA1
            \FolderB
                         \FolderB1
     
 
 
Comment by madperk
Doh!

Never Mind.  It helps if my test files in the A1 and B1 directories were older than 10 days.

I revamped the script.  I combined what i had written perviously with something I found on the web over the weekend.

The script will look into the folder structure.  I have not tested it more than 3 levels deep.  It WILL NOT delete empty folders.   I haven't figured out how to do this yet. I dropped the WMI calls.  The script should now run on most OS's. There are only 3 places that you need to adjust, startFolder, strLogFileName and OlderThanDate. everything should be straight forward.

 
 
Comment by madperk
Rem Script created: 18.02.2008 10:49
Rem Author: madisonp

Rem Script created: 14.02.2008 09:33

Dim fso, startFolder, OlderThanDate
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Set fso = CreateObject("Scripting.FileSystemObject")
startFolder = "c:\temp" 'Starting Directory scanned for files to be deleted.
strLogFileName = "file.log" 'name of the log file.
OlderThanDate = DateAdd("d", -10, Date)  ' 10 days (adjust as necessary).

Set fsoObj = CreateObject("Scripting.FileSystemObject")
Set fileLog = FsoObj.OpenTextFile(strLogFileName,8,True)
force_cscript
DeleteOldFiles startFolder, OlderThanDate

Function DeleteOldFiles(folderName, BeforeDate)
   Dim folder, file, fileCollection, folderCollection, subFolder

   Set folder = fso.GetFolder(folderName)
   Set fileCollection = folder.Files
   For Each file In fileCollection
         If file.DateLastModified < BeforeDate Then
            fileLog.Write folderName & "\" & file.Name
            fileLog.WriteLine ", Date Deleted: " & Now
            fso.DeleteFile(file.Path)
         End If
   Next

    Set folderCollection = folder.SubFolders
    For Each subFolder In folderCollection
       DeleteOldFiles subFolder.Path, BeforeDate
    Next
End Function

'Force a cscript host


sub force_cscript
    dim args : args=""
    dim i, wshshell
    If right(lCase(wscript.fullname),11)= "wscript.exe" then
        for i=0 to wscript.arguments.count-1
            args = args & wscript.arguments(i) & " "
        next
        set wshshell=createobject("wscript.shell")
        wshshell.run wshshell.ExpandEnvironmentStrings("%comspec%") & _
            " /c cscript.exe //nologo """ & wscript.scriptfullname & """" & args    'change to /k for stay
        set wshshell=nothing
        wscript.quit
    end if
end sub

'Closes the log file
fileLog.Close
 
 
Comment by JSRhelpdesk
Hi there! Thanks again MADPERK, we will test this now. Unfortunatly i was sick last week.
 
 
Comment by JSRhelpdesk
MADPERK: The script runs as it should do...but..
If a user places a file back into a directory what was cleared by the script earlier, it wil get cleared again.

Example: in the directory WORK i created work.avi, after 10 days work.avi gets deleted. Now i make another work.avi and place it in the same directory WORK, the script runs the next day and deletes work.avi again... Is it possible to ad a scriptcode to delete empty folders after the 10 day files are shreded?
 
 
Comment by madperk
So they are moving a file that is more than 10 days old and the file gets deleted the next day or the file gets deleted 10 days after the file gets put back in the folder?

so you want to delete folders only if they are empty? - yes.  i found one that should work.  let me see what i need to do to trim it down a little bit and add it to your script.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
'==================================== 
' VBScript Source File 
' NAME: DeleteEmptySubdirs.vbs 
' AUTHOR: Chay Harley 
' DATE  : v1.0: 28/10/2005  ; v1.1: 30/10/2005 ; v1.2 31/10/2005 
' Usage: 
'   cscript scriptname.vbs /d:C:\SomePath 
'   cscript scriptname.vbs /d:"C:\SomePath To Clean" 
'==================================== 
   Option Explicit 
 
   Dim WSHShell, RightMargin, LastParentFolder 
 
   Set WSHShell = CreateObject("Wscript.Shell") 
    
   If WScript.Arguments.Named.Count = 0 Then Usage() 
    
   Dim ExceptionList 
   'To exclude some folders, add to this list 
   ExceptionList = Array("Audio_TS", "My Documents", "My Pictures", "My Received Files") 
    
   Dim Path 
   Path = WScript.Arguments.Named.Item("D") 
   If Right(Path,1) <> "\" Then Path = Path & "\" 
   WScript.echo "Option [D]: " & UCase(Path) 
 
   Dim  objFSO, objRootFolder 
   Set objFSO = CreateObject("Scripting.FileSystemObject") 
    
   Set objRootFolder = objFSO.GetFolder(Path) 
   ProcessChildFolders objRootFolder 
    
   Sub ProcessingDirError(DirToCheck, ErrMsg) 
      'If we get 'permission denied', there is a read/delete block on the folder. Ignore it... 
      If ucase(ErrMsg) <> "PERMISSION DENIED" Then 
         WScript.Echo "Error [" & ErrMsg & "]. Please manually action """ & DirToCheck & """" 
      End if 
      Err.Clear 
   End Sub 
    
   Sub CheckAndDeleteEmptyFolder(objFolder) 
      On Error Resume Next 
      if objFolder.Size = 0 Then 
         If Err.Number Then 
            ProcessingDirError objFolder.Path, Err.Description 
         else          
            DeleteEmptyFolder objFolder, objFolder.Size 
         End If 
      End If 
   End Sub 
 
   Sub ProcessChildFolders(objRootFolder) 
      On Error Resume Next 
      Dim objParentFolder, objChildFolder 
      Set objParentFolder = objFSO.GetFolder(objRootFolder.Path) 
      If Err.Number Then 
         ProcessingDirError objRootFolder.Path, Err.Description 
      Else 
         If objParentFolder.SubFolders.Count > 0 Then 
            'process a folder branch 
            For Each objChildFolder In objParentFolder.SubFolders 
               ProcessChildFolders objChildFolder 
            Next 
            CheckAndDeleteEmptyFolder objParentFolder 
         Else 
            'Process a folder leaf (final folder) 
            if Left(objParentFolder.Path, InStr(InStr(objParentFolder.Path,"\")+1, objParentFolder.Path,"\")) <> LastParentFolder Then 
               WScript.Echo "[Progress] " & objParentFolder.Path 
            End If 
            CheckAndDeleteEmptyFolder objParentFolder 
            LastParentFolder = Left(objParentFolder.Path, InStr(InStr(objParentFolder.Path,"\")+1,objParentFolder.Path,"\")) 
         End If 
      End if 
   End Sub 
    
   Sub DeleteEmptyFolder(objDeleteFolder, FolderSize) 
      Dim ExceptionItem 
      For Each ExceptionItem In ExceptionList                     'skip exceptions ;) 
         If InStr(UCase(objDeleteFolder.Path),  Ucase(ExceptionItem)) Then 
            WScript.Echo "[Skipping] " & objDeleteFolder.Path 
            Exit Sub 
         End If 
      Next 
      DisplayDirectoryName objDeleteFolder.Path, FolderSize 
      Err.Clear 
      objDeleteFolder.Delete() 
      If Err.Number Then ProcessingDirError objDeleteFolder.Path, Err.Description 
   End Sub 
    
   Sub DisplayDirectoryName(DisplayPath, Size) 
      Dim TempNumber 
      TempNumber = Len(DisplayPath) 
      'set the right margin to make the output look good 
      If TempNumber >  RightMargin Then RightMargin = TempNumber 
      'yet, let's shift it back to the left if there's a huge difference (say, 30 chars) 
      If RightMargin - 30 > TempNumber Then RightMargin = TempNumber 
      WScript.Echo "*Deleting* " & DisplayPath & Space(RightMargin - TempNumber + 4 ) & "Size: " & Size 
   End Sub 
    
   Sub Usage() 
      WScript.Echo VbCrLf 
      WScript.Echo "=====================================" 
      WScript.Echo "Empty Folder/Directory Remover Script" 
      WScript.Echo "=====================================" 
      WScript.Echo VbCrLf & "Usage:" 
      WScript.Echo VbCrLf & " cscript scriptname.vbs /d:C:\SomePath " 
      WScript.Echo VbCrLf & " cscript scriptname.vbs /d:""C:\SomePath To Clean"" " 
      WScript.Quit 
   End Sub 
Open in New Window Select All
 
 
20080723-EE-VQP-34 / EE_QW_2_20070628