[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[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!

7.9

VBScript to Delete Files x Days Old from a Txt File List of Directories

Asked by cbruss in Scripting Languages, Automation, Windows Batch Scripting

Hello,


My company is currently using a script below in a scheduled task run against various folders on our ftp server. This currently needs to be a scheduled task on EVERY individual folder to run and delete files older than 90 days.

What I want to do is make this a little easier to administrate by feeding a txt file of directories to be included in the 90 day deletion as some folders need to be completely excluded per the user's request. Then I can run one scheduled task and edit one file to add/remove folders from file deletion.

I'm not much of a vbscript guru so I am hoping someone can help.
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:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
'First, you will need to setup a scheduled task and run it
'daily.  Just change the number to the amount of days you
'want to keep file in that directory.  Any files in the
'directory will be deleted.   
' specify the directory you wish to clean as the first and only parameter to this script! 
' MODIFIED AND ENHANCED BY RUSSELL JUNGWIRTH 
'  FROM AN ORIGINAL SCRIPT Courtesty of monsterjta @ tek-tips.com 
Option Explicit
on error resume next
    Dim iDaysOld
    Dim WshShell
    Dim sDirectoryPath
    Dim DesktopFolder
    Dim LogFilePath
    Dim fso
    Dim LogFile
    Dim oArgs
    Dim bWriteLog
    Dim bDoDeletes 
    'Definitions
    ' change this value to specify the number days old the file must be to qualify for deletion
    iDaysOld = 90
    ' set this to 1 to log activity, set to 0 to prevent logging 
    ' THE LOG FILE LIVES IN THE SAME DIRECTORY AS THE SCRIPT
    bWriteLog = 1
    ' set to 1 to do the deletions, set to 0 to simply report intended deletions instead of doing them
    '   note that setting this to 1 will automatically write a log
    bDoDeletes = 1 
    if bDoDeletes = 0 and bWriteLog = 0 then bWriteLog = 1 
    Set WshShell = CreateObject("WScript.Shell")
    OpenLog()
    WriteLog("run " & fso.GetBaseName(Wscript.ScriptName))
    ' GET EXECUTION ARGUMENTS TO DETERMINE WHAT DIR TREE TO SCAN
    Set oArgs = WScript.Arguments
    sDirectoryPath = oArgs(0)
    if sDirectoryPath = "" then
      'wscript.stderr.writeline("no directory specified")
      'wscript.quit(1)
      sDirectoryPath = WshShell.CurrentDirectory
    end if
    ' CALL THE RECURSIVE SUB TO DO THE WORK
    call DeleteTree(sDirectoryPath)
    If Err.Number <> 0 Then
      writelog("subfolder err " & Err.Number & " - "  & Err.Description)
      on error goto 0
      Err.Clear
    end if
    CloseLog()
'Clean up
    set fso = Nothing
    set WshShell = Nothing   
    set LogFile = Nothing
    set oArgs = Nothing
'End 
 

Sub DeleteTree (ByVal sPath)
    on error resume next
  Dim l_oFile, l_oFolder, l_oFileCollection
  Dim l_oFSO, l_oFolderCollection
'  WriteLog("called DelTree with " & sPath)
  if mid(len(sPath),1) <> "\" then sPath = sPath & "\"
  Set l_oFSO = CreateObject("Scripting.FileSystemObject")
  set l_oFolder = l_oFSO.GetFolder(sPath)
  set l_oFileCollection = l_oFolder.Files
  For each l_oFile in l_oFileCollection
    If l_oFile.DateLastModified < (Date() - iDaysOld) Then
      WriteLog("delete " & l_oFile.Name)
      if bDoDeletes = 1 then  l_oFile.Delete(True)
    else
      WriteLog("preserve " & l_oFile.Name)
    End If
  Next
  set l_oFolderCollection = l_oFolder.SubFolders
    If Err.Number <> 0 Then
      writelog("subfolder err " & Err.Number & " - "  & Err.Description)
      on error goto 0
      Err.Clear
      exit sub
    else
      For each l_oFolder in l_oFolderCollection
    '   PERFORM RECURSIVE CALL TO DELETE OLD FILES IN LOWER DIRECTORIES
'       WriteLog("recurse " & l_oFolder.Path)
        DeleteTree(l_oFolder.Path)
        on error resume next
 '      WriteLog("return recurse " & l_oFolder.Path)
'       if l_oFolder.Files.Count = 0 and  l_oFolder.SubFolders.Count = 0 and            l_oFolder.DateLastModified < (Date() - iDaysOld) then 
'         WriteLog("delete empty folder " & l_oFolder.Path)
'         if bDoDeletes = 1 then l_oFolder.Delete(True)
'       else
'         WriteLog("preserve non-empty folder " & l_oFolder.Path)
'       end if
    '   On Error Goto 0   
      Next
    end if ' error trap 
  Set l_oFile = Nothing
  Set l_oFolder = Nothing
  Set l_oFileCollection = Nothing
  Set l_oFSO = Nothing
  Set l_oFolderCollection = Nothing
End Sub 
Sub OpenLog()
on error resume next
 if bWriteLog = 1 then 
 ' GET LOCATION FOR LOG FILE ON THE DESKTOP
 '  DesktopFolder = WshShell.RegRead("HKEY_CURRENT_USER\Software\" & _ 
 '     "Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Desktop")
    Set fso = CreateObject("Scripting.FileSystemObject")
    ' LogFilePath = DesktopFolder & "\" & fso.GetBaseName(Wscript.ScriptName) & ".log"
    LogFilePath = fso.GetParentFolderName(WScript.ScriptFullName) & "\" & fso.GetBaseName(  Wscript.ScriptName) & ".log"
    'wscript.stderr.writeline(logfilepath)
    if not fso.FileExists(LogFilePath) then
      Set LogFile = fso.CreateTextFile(LogFilePath)
    else
      Set LogFile = fso.OpenTextFile(LogFilePath, 8) ' open for appending = 8
      'Set LogFile = fso.OpenTextFile(LogFilePath, 2) ' open for writing = 2 
    end if 
  end if
End Sub 
Sub WriteLog(sLogEntry)
  on error resume next
  if bWriteLog = 1 then LogFile.WriteLine(Now &  " " & sLogEntry)
end Sub 
Sub CloseLog()
  on error resume next
  if bWriteLog = 1 then LogFile.Close()
End Sub
[+][-]11/05/09 07:14 AM, ID: 25750301Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zones: Scripting Languages, Automation, Windows Batch Scripting
Sign Up Now!
Solution Provided By: COF-PCTech
Participating Experts: 1
Solution Grade: A
 
[+][-]11/04/09 01:32 PM, ID: 25744176Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11/04/09 07:23 PM, ID: 25746509Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091118-EE-VQP-93 - Hierarchy / EE_QW_3_20080625