Solved

Trying to write vbs script that searches directories to detect file modification

Posted on 2015-01-15
5
90 Views
Last Modified: 2015-02-11
I'm trying to write a vbs script that searches a directory that has a new folder created each day with the day's date in the name then determine if certain files within this folder's subdirectories have been updated within 60 min.   I need the results of the test to be written to a log for each file being tested.    I've created a script to detect file change within 60 min but I'm having trouble creating logic in the script to look for a new directory as the date changes.     An example of the directory structure being tested is below.

C:\Syslogd\Logs\YYYY-MM-DD\Device\FileName.zip


Below is the code I've written so far.   It searches the entire C:/Syslogd/Logs/2015-01-12/device/ directory so is not taking date name change into account plus I'd like to be more precise with the file search (name specific file).




Dim fso, folder, files
Dim strPath

Set fso = CreateObject("Scripting.FileSystemObject")

strPath = "C:/Syslogd/Logs/2015-01-12/Device/"

set folder = fso.GetFolder(strPath)
set files = folder.Files

CounterNum = 0

For each item in files
    If DateDiff("N",item.DateLastModified,Now()) > 60 Then
    FileUpdateStatus = "File has not been updated in the last hour"
    End If
Next

MsgBox FileUpdateStatus
0
Comment
Question by:T110E5
  • 2
5 Comments
 
LVL 12

Accepted Solution

by:
jkaios earned 500 total points
ID: 40552436
Try:

Dim newDate

newDate = year(date) & "-" & iif(month(date)<10, "0" & month(date), month(date)) & "-" & iif(day(date)<10, "0" & day(date), day(date))

strPath = "C:/Syslogd/Logs/" & newDate & "/Device/"

Function IIf(sExpr, truePart, falsePart)
   If sExpr Then IIf = truePart Else IIf = falsePart
End Function

Open in new window

0
 

Author Comment

by:T110E5
ID: 40552749
The above worked - thank you.  I added additional logic to choose the most recently mondified file in the folder to test.   Now all I need to do is create a log that appends the results of each test as follows:

<DATE> <TIME> "File for device xyz has been updated in the last hour"
<DATE> <TIME> "File for devicexyz has been updated in the last hour"
<DATE> <TIME> "File for devicexyz has been updated in the last hour"'
<DATE> <TIME> "File for devicexyz has NOT been updated in the last hour"'
....

What should I add to the code below to create the logging?




************************************************************************



Dim newDate
Dim fso, folder, subfolder, files, file, recentDate, recentFile
Dim strPath

Set fso = CreateObject("Scripting.FileSystemObject")

newDate = year(date) & "-" & iif(month(date)<10, "0" & month(date), month(date)) & "-" & iif(day(date)<10, "0" & day(date), day(date))

strPath = "C:/Syslogd/Logs/"&newDate&"/devicexyz/"


Function IIf(sExpr, truePart, falsePart)
   If sExpr Then IIf = truePart Else IIf = falsePart
End Function


set folder = fso.GetFolder(strPath)
set files = folder.Files
Set recentFile = Nothing


For Each file in fso.GetFolder(strPath).Files
  If (recentFile is Nothing) Then
    Set recentFile = file
  ElseIf (file.DateLastModified > recentFile.DateLastModified) Then
    Set recentFile = file
  End If
Next

If recentFile is Nothing Then
  WScript.Echo "no recent files"
Else
   If DateDiff("N",recentFile.DateLastModified,Now()) > 60 Then
   FileUpdateStatus = "File for devicexyz has not been updated in the last hour"
   End If

MsgBox FileUpdateStatus
End If
0
 
LVL 12

Expert Comment

by:jkaios
ID: 40552781
Please see lines 4, 7, 37 below regarding logging:

Dim newDate
Dim fso, folder, subfolder, files, file, recentDate, recentFile
Dim strPath
Dim log

Set fso = CreateObject("Scripting.FileSystemObject")
Set log = fso.CreateTextFile("c:\temp\mylog.txt")

newDate = year(date) & "-" & iif(month(date)<10, "0" & month(date), month(date)) & "-" & iif(day(date)<10, "0" & day(date), day(date))

strPath = "C:/Syslogd/Logs/"&newDate&"/devicexyz/"


Function IIf(sExpr, truePart, falsePart)
   If sExpr Then IIf = truePart Else IIf = falsePart
End Function


set folder = fso.GetFolder(strPath)
set files = folder.Files
Set recentFile = Nothing


For Each file in fso.GetFolder(strPath).Files
  If (recentFile is Nothing) Then
    Set recentFile = file
  ElseIf (file.DateLastModified > recentFile.DateLastModified) Then
    Set recentFile = file
  End If
Next

If recentFile is Nothing Then
  WScript.Echo "no recent files"
Else
   If DateDiff("N",recentFile.DateLastModified,Now()) > 60 Then
      FileUpdateStatus = "File for devicexyz has not been updated in the last hour"
      log.WriteLine now() & " " & FileUpdateStatus
   End If

MsgBox FileUpdateStatus
End If 

Open in new window

0
 
LVL 47

Expert Comment

by:Martin Liss
ID: 40602756
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question