Solved

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

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

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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 …
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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 …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

758 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

20 Experts available now in Live!

Get 1:1 Help Now