Solved

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

Posted on 2015-01-15
5
86 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 46

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…

911 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now