+ Post New Thread
Results 1 to 9 of 9
Scripts Thread, File Aging Script Needed in Coding and Web Development; Hi All, I have decided that I need to do a big cleanup of our storage areas and want to ...
  1. #1
    ICTNUT's Avatar
    Join Date
    Jul 2005
    Location
    Hereford
    Posts
    1,419
    Thank Post
    196
    Thanked 249 Times in 122 Posts
    Rep Power
    62

    File Aging Script Needed

    Hi All,

    I have decided that I need to do a big cleanup of our storage areas and want to do the following:

    1 - Search out and identify and file that has not been "Accessed" in 12 months or over.
    2 - Move this file to another location ready for archiving onto DVD.

    Does anyone know of a script or program that can do this?

  2. #2
    Joedetic's Avatar
    Join Date
    Jan 2006
    Location
    Walsall
    Posts
    1,316
    Thank Post
    6
    Thanked 13 Times in 13 Posts
    Rep Power
    22
    Your first point is something we'd like to do as something of a software usage audit. Find out the last time an application was run on the machines around the network so that we can then say to staff "well that's not been used since such and such a date, do we a) need it installed b) need to keep paying for licensing/that many licenses" etc.

    But we have no idea how to do it (unfortunately) so if anyone has any ideas for Ozan that I can adapt that'd be great! (Sorry if this hijacks...I'll get the thread split if it does).

  3. #3


    Join Date
    Jan 2006
    Posts
    8,202
    Thank Post
    442
    Thanked 1,032 Times in 812 Posts
    Rep Power
    339
    Code:
    find /original/path -atime +365 -exec mv {} /path/to/new/file/ \;
    edit: +365 not -365 !
    Last edited by CyberNerd; 1st September 2008 at 09:18 PM.

  4. #4
    ICTNUT's Avatar
    Join Date
    Jul 2005
    Location
    Hereford
    Posts
    1,419
    Thank Post
    196
    Thanked 249 Times in 122 Posts
    Rep Power
    62
    @cybernerd: What are you using for this and will it run on a windows box??

  5. #5


    Join Date
    Jan 2006
    Posts
    8,202
    Thank Post
    442
    Thanked 1,032 Times in 812 Posts
    Rep Power
    339
    "find" and "mv" are standard *nix commands. they are available via cygwin for windows but I'm not sure if it will work exactly the same. I'm sure there is a windows alternative if not.
    Cygwin Information and Installation

  6. #6

    powdarrmonkey's Avatar
    Join Date
    Feb 2008
    Location
    Alcester, Warwickshire
    Posts
    4,859
    Thank Post
    412
    Thanked 777 Times in 650 Posts
    Rep Power
    182
    No, it won't run on Windows without cygwin and even then I'm not sure if the syntax for accessed is the same. It may be though.

    @Joedetic: if you have proper application installers, the add/remove programs applet will give you a last used date, but I can't vouch for its reliability. If you use properly written MSIs with advertising, it's probably more dependable.

  7. #7
    ICTNUT's Avatar
    Join Date
    Jul 2005
    Location
    Hereford
    Posts
    1,419
    Thank Post
    196
    Thanked 249 Times in 122 Posts
    Rep Power
    62
    The following script will allow you to get the last time a file was accessed in X number of days for a given directory and all those under it.

    I will see if I can mod it to move the files over for archiving

    Code:
    '====================
    '
    ' NAME:    AccessedX.vbs
    '
    ' COMMENT: AccessedX will search the directory that you tell it to for files that have not been accessed in over days
    '        
    ' USAGE:    CScript AccessedX.vbs <rootfolder> <AccessedX> [/L:logfile]
    '
    ' <rootfolder> - Specifies the directory to start purging files.
    ' <accessedx> - Specifies how old a file can be in Days before it is purged.
    ' [/L:logfile] - Specifies a file to output the results to.
    ' /? - Displays USAGE message.
    '
    '====================
    
    Const EVENT_SUCCESS = 0
    Const EVENT_ERROR = 1
    Const EVENT_WARNING = 2
    Const EVENT_INFORMATION = 4
    
    Dim objFSO, objWSH, objLogFile
    Dim argCount, iArg, flg
    Dim RootFolder, AccessedX, Logfile
    
    On Error Resume Next
    
    Set objWSH = CreateObject("WScript.Shell")
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    
    argCount = WScript.Arguments.Count
    iArg = 0
    
    ' Check to see if the user is asking for help.
    If argCount > 0 Then
       flg = LCase(WScript.Arguments(0))
       If (flg="help") Or (flg="/h") Or (flg="\h") Or (flg="-h") Or (flg="h") Or _
        (flg="/?") Or (flg="\?") Or (flg="-?") Or (flg="?") Then
           Call ShowUsage
       End If
    End If
    
    ' Make sure that we have at least 2 arguments.    
    If argCount < 2 Then
       Call Fail("Usage: " & WScript.ScriptName & " /? for help.")
    End If
    
    
    ' Verify that the first argument is a folder and it exists.
    RootFolder = NextArgument
    If Not objFSO.FolderExists(RootFolder) Then
       Call Fail("Can't find " & chr(34) & RootFolder & chr(34) & ".")
    End If
    
    ' Verify that the second argument is a valid number of days.
    AccessedX = NextArgument
    If IsNumeric(AccessedX) Then
       If (AccessedX >= 1) Then
           AccessedX = Int(AccessedX)
       Else
           Call Fail("Invalid entry.")
       End If
    Else
       Call Fail("Invalid entry.")
    End If
    
    ' If we have more than 2 arguments then error out.
    If Not IsEmpty(NextArgument)Then
       Call Fail("Usage: " & WScript.ScriptName & " /? for help.")
    End If
    
    ' Open the logfile if it was an option.
    If Not IsEmpty(Logfile) Then
       Set objLogFile = objFSO.CreateTextFile(Logfile, True)
       Call CheckError
    End If
    
    ' Everything should be ok so proceed with the scanning.
    Call LogEvent("Files that have not been accessed in at least " & AccessedX & " days" & vbCrLf)
    Call ScanFolder(RootFolder)
    
    ' Clean up.
    Set objWSH = Nothing
    Set objFSO = Nothing
    If Not IsEmpty(Logfile) Then objLogFile.Close
    
    ' Exit the script.
    WScript.Quit 0
    
    '====================
    Sub ScanFolder(sFolderName)
    
    Dim objFolder, objSubFolders, objFiles
    Dim Folder, File
    Dim ret
    
    On Error Resume Next
    
    Set objFolder = objFSO.GetFolder(sFolderName)
    Set objSubFolders = objFolder.SubFolders
    Set objFiles = objFolder.Files
    
    For Each Folder In objSubFolders
       ScanFolder (Folder.Path)
    Next
    
    For Each File In objFiles
       ret = DaysOld(File.Path)
       ' If the file older then the wanted # of days then log it
       If ret => AccessedX Then
               If StrComp(File.Path, Logfile, vbTextCompare) <> 0 Then
                   Call LogEvent(File.Path & " has not been accessed in " & ret & " days.")
                   Call CheckError
               End If
       End If
    Next
    
    End Sub
    
    '====================
    Function DaysOld(sFileName)
    
    Dim objFile
    
    
    On Error Resume Next
    
    Set objFile = objFSO.GetFile(sFileName)
    Call CheckError
    
    ' Return the difference in days.
    DaysOld = Int(Now() - objFile.DateLastAccessed)
    
    End Function
    
    '====================
    ' Extract argument value from command line, processing any option flags.
    Function NextArgument
    
    Dim arg
    
    Do ' Loop to pull in option flags until an argument value is found.
       If iArg >=argCount Then Exit Function
       arg = WScript.Arguments(iArg)
       iArg = iArg + 1
       If (AscW(arg) <> AscW("/")) And (AscW(arg) <> AscW("-")) Then Exit Do
       Select Case UCase(Right(arg, Len(arg) -1))
           Case Else
               If UCase(Mid(arg, 2, 2)) = "L:" Then
                   LogFile = Mid(arg,4)
               Else
                   Call Fail("Invalid option flag: " & arg)
               End If
       End Select
    Loop
    
    NextArgument = arg
    
    End Function
    
    '====================
    Sub ShowUsage
    
    WScript.Echo "CScript " & WScript.ScriptName & " <rootfolder> <AccessedX>" & vbCrLf & _
                vbTab & vbTab & vbTab & vbTab & vbTab & " [/L:logfile]" & vbCrLf & vbCrLf & _
                " rootfolder" & vbTab & "Specifies the directory to start searching in." & vbCrLf & _
                " AccessedX" & vbTab & "Specifies how many days since it has been accessed." & vbCrLf & _
                " /L:logfile" & vbTab & vbTab & "Specifies a file to output the results to." & _
                " /?" & vbTab & vbTab & "Displays this message."
    WScript.Quit 1
    
    End Sub
    
    '====================
    Sub CheckError()
    
    Dim msg
    
    If Err.Number = 0 Then Exit Sub
    msg = Err.Source & " " & Hex(Err) & ": " & Err.Description
    
    Call Fail(msg)
    
    End Sub
    
    '====================
    Sub LogEvent(sMessage)
    
    If IsObject(objLogFile) Then objLogFile.WriteLine(sMessage)
    
    End Sub
    
    '====================
    Sub LogToEventLog(sMessage, nValue)
    
    If bEventLog Then objWSH.LogEvent nValue, WScript.ScriptName & ": " & sMessage
    
    End Sub
    
    '====================
    Sub Fail(sMessage)
    
    If bEventLog Then LogToEventLog sMessage, EVENT_ERROR
    If IsObject(objLogFile) Then
       objLogFile.WriteLine(WScript.ScriptName & ": " & sMessage)
       objLogFile.Close
    End If
    WScript.Echo WScript.ScriptName & ": " & sMessage
    
    WScript.Quit(1)
    
    End Sub
    Last edited by ICTNUT; 1st September 2008 at 09:41 PM.

  8. #8
    Joedetic's Avatar
    Join Date
    Jan 2006
    Location
    Walsall
    Posts
    1,316
    Thank Post
    6
    Thanked 13 Times in 13 Posts
    Rep Power
    22
    @powdarrmonkey I want to gather information from all the machines on site though, so I'd want to be able to either remotely poll this information or dump it onto a server using a startup script and then produce a graph using excel or something. That's why I was thinking something along the lines of ICTNUT's needs.

  9. #9
    box_l's Avatar
    Join Date
    May 2007
    Location
    Herefordshire
    Posts
    427
    Thank Post
    68
    Thanked 90 Times in 75 Posts
    Rep Power
    61

SHARE:
+ Post New Thread

Similar Threads

  1. Replies: 5
    Last Post: 1st July 2014, 08:34 AM
  2. *nix Script needed for backup job
    By ICTNUT in forum *nix
    Replies: 2
    Last Post: 14th November 2007, 09:59 AM
  3. Help needed writing a pkinvoice script
    By Ric_ in forum Scripts
    Replies: 6
    Last Post: 20th April 2007, 12:15 PM
  4. Replies: 4
    Last Post: 23rd March 2006, 06:27 AM
  5. Replies: 12
    Last Post: 12th September 2005, 10:47 AM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •