Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Create script to report on how old files in Dir are

Options
  • 21-10-2010 12:56pm
    #1
    Registered Users Posts: 56 ✭✭


    Hi,

    Scripting is far from my strong point so hence im posting here.

    Im looking to create a script that will report on how old files are in a directory.

    for example I have a dir D:\Volt Files\Data Files\ERD thats stores files (.ctl and .dat )files that should be processed and moved to another dir as soon as they are imported in the above dir. This process has failed on a number of occasions and i'd like to be alerted when it fails again.

    I want to create a script that I can schedule to run every hour and report back if any of the files .ctl, .dat are older than say 1 hour old which would indicate that the dataload process has failed.

    Thanks in advance


    Jonathan


Comments

  • Registered Users Posts: 2,426 ✭✭✭ressem


    Any preference on scripting language and alert method?

    Powershell? (adapted from http://powershell.com/cs/media/p/208.aspx)
    filter Limit-FileAge { 
        param( $maximumage = 0) 
     
        if ($_.PSIsContainer) { 
            # filter out folders  
        } elseif ($maximumage -gt 0) { 
            if ($_.LastWriteTime -ge (Get-Date).AddMinutes($maximumage * - 1)) { 
                $_ 
            } 
        } else { 
            $_ 
        }  
    } 
    
    filter Limit-FileExtension { 
     
        if ($_.PSIsContainer) { 
            # filter out folders 
        } elseif ($_.Extension -match "ctl") { 
                $_ 
             
        } elseif ($_.Extension -match "dat") { 
                $_ 
            
        } else { 
             
        }  
    } 
    
    
    $FileMatch=dir "D:\Volt Files\Data Files\ERD" | Limit-FileAge -maximumage 60 | Limit-FileExtension
    if ($FileMatch.count -ge 1) {"AAAgh Don't Panic"}
    else {"All is well with the world"}
    

    Then you can use something like
    http://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/0db29ac2-cb2a-47b9-89e3-e46083117754
    to send an email to yourself?

    Though I'd first check the event log on the machine to find whether there is an error that can be caught more rapidly and acted upon.
    E.g an error in VMCService in the last 5 minutes
    param( $maximumage = 5)  # Look for error in last 5 minutes
    Get-EventLog Application -After (Get-Date).AddMinutes($maximumage * - 1) -Source "VMCService" | where {$_.entryType -match "Error"}
    
    Other possible filters http://technet.microsoft.com/en-us/library/dd315250.aspx

    If you haven't used powershell before you might need to change the execution policy using Set-ExecutionPolicy RemoteSigned
    (Or to learn about how to sign scripts yourself).
    The free MS Windows Powershell ISE (Integrated Script Environment) is handy for testing.


  • Registered Users Posts: 1,530 ✭✭✭CptSternn


    Here it is in VBScript. Copy this and paste it into notepad and save it as a .VBS file. You can then use scheduler to have it run every hour. Make sure you modify the email settings to match your mail server.

    -S


    '---------------------------------------------------------------------------------------------------
    'VBScript name: CheckFolderFileDate.vbs
    'Author: Sternn (www.sternn.com)
    'Date 29-10-10
    '
    'This script will check the date on a file to see if it is older than an hour then send an email 
    'reporting the issue.
    '---------------------------------------------------------------------------------------------------
    
    On Error Resume Next 
    Const ForReading = 1
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    
    ' Set the path (folder) to check
    varPath = "D:\Volt Files\Data Files\ERD\"
    
     
    ' Open the folder and get a file list
    
    If objFSO.FolderExists(varPath) Then
        Set objFolder = objFSO.GetFolder(varPath)
        Set colFiles = objFolder.Files
    Else
        Wscript.Quit
    End if
    
    For Each strFile in colFiles
        Set objFile = objFSO.GetFile(strFile)
    	varModDate = objFile.DateLastModified
    	varCheckDate = DateAdd("h",-1,now())
        If DateDiff("h", varModDate, varCheckDate) > 0 Then
    		MailMessage = MailMessage & strFile.Name & ", Mod-Date: " & objFile.DateLastModified & " - is older than one hour." & vbNewLine 
    		strBadFileCheck = strBadFileCheck + 1
    	End If
        Set objFile = Nothing
    Next
    
    ' Put together the email if needed
    
    If strBadFileCheck > 0 Then
    
    	MailMessage = MailMessage & vbNewLine
    	strMailMessageTop = "The file check has found files older than one hour @ " & now & "." & vbNewLine & vbNewLine
    	strMailMessage = strMailMessageTop & MailMessage
    	strSubject = "File Date Check Script Has Found An Issue (" & now & ")"
    
    	' Send the email
    
    	sendUrl="http://schemas.microsoft.com/cdo/configuration/sendusing"
    	smtpUrl="http://schemas.microsoft.com/cdo/configuration/smtpserver"
    
    	' Set the mail server configuration
    	Set objConfig=CreateObject("CDO.Configuration")
    	objConfig.Fields.Item(sendUrl)=2 ' cdoSendUsingPort
    	objConfig.Fields.Item(smtpUrl)="YourMailServer"
    	objConfig.Fields.Update
    
    	Set objMail =CreateObject("CDO.Message") 
    
    	' Use the config object created above
    	Set objMail.Configuration=objConfig
    
    	objMail.From = """File Check Automated Email"" <filecheck@YourDomain.Com>"
    		objMail.Subject = strSubject
    		objMail.to = "YourEmail@YourDomain.Com"
    		objMail.TextBody = strMailMessage
    		objMail.Send
    
     End If
     
    ' EOF
    
    


  • Registered Users Posts: 7,606 ✭✭✭Jumpy


    Newschool and Oldschool in the same thread.

    Awesome :)


  • Registered Users Posts: 56 ✭✭Jonniealan


    Thanks all thats great and works brilliantly, when I edit script to send alert to my gmail email add, long story but theres no mail server in the Domain. Theres an external mail server I think i can use with an smtp engine so working on that now.
    The joys of working in IT


Advertisement