Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2015-01-15
5
Medium Priority
?
106 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
5 Comments
 
LVL 12

Accepted Solution

by:
jkaios earned 2000 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 49

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

URL rewriting in AWS CloudFront

A quick how-to guide to implement with a Lambda function!

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
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…

715 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