-
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?
-
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).
-
Code:
find /original/path -atime +365 -exec mv {} /path/to/new/file/ \;
edit: +365 not -365 !
-
@cybernerd: What are you using for this and will it run on a windows box??
-
"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
-
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.
-
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
-
@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.
-