Solved

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

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
AWK - Blank file output zero bytes why? 18 43
Debug script powershell wmi 3 25
Assign a value in Excel V-Basic 3 38
TypeError jquery issues with site - CSS conflicts 3 46
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
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)

739 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