Luis Diaz
asked on
VB script: Files verification based on last date modified and prefix with loop number and pause param v2
Hello experts,
I used the following procedure reported at:
https://www.experts-exchange.com/questions/28984940/VB-Script-Bat-check-if-a-file-has-been-modified-based-on-prefix.html?anchorAnswerId=41932552#a41932552
In order to check if a file exist in a reference folder and it has been modified.
I would like to add some param in the procedure:
1- Add a loop number param which will allows to repeat the verification action based on the reported number.
2-Pause loop number is the second between the launch of each loop number
Example:
loop_number=3
pause_loop number=15
This means that the procedure should be applied 3 times and the pause number between the relaunch is15 second.
The log file should report when every file verification has been launched and when the procedure has finished.
3-Re-adapat the datediff condition in order to report it at the beginning of the script
datestamp_threshold= (d for days, h for hours, m for minutes, n for seconds)
age_threshold=(numeric based on the reported datestamp threshold
example:
datestamp_threshold=d
age_threshold=15
If you have questions, please contact me.
Thank you very much for your help.
I used the following procedure reported at:
https://www.experts-exchange.com/questions/28984940/VB-Script-Bat-check-if-a-file-has-been-modified-based-on-prefix.html?anchorAnswerId=41932552#a41932552
FilesVerifications arrPrefix, strBaseDir
'----------------------------------------------------------------------------
' Check if a file exist and if it was modified today
'----------------------------------------------------------------------------
Sub FilesVerifications(darrFile, dDir)
' Assume no files found that were modified today
blnErrors = True
' Access the desired folder
Set objFolder = objFSO.GetFolder(dDir)
' Look at each file in this folder
For Each objFile In objFolder.Files
' See if this file matches any of the prefixes in the selection array
For Each strFile In darrFile
' See if this file matches a prefix
If LCase(Left(objFile.Name, Len(strFile))) = LCase(strFile) Then
' It matches, now see if it was modified today
If DateDiff("d", objFile.DateLastModified, Now) = 0 Then
' Modified today, flag it
blnErrors = False
End If
End If
Next
Next
' See if any of the files checked didn't exist, or were not modified today
If blnErrors Then
objLog.WriteLine Now & " ==> ERROR: Files located at: " & dDir & " were not modified today"
Else
' No errors, all files good, log that
objLog.WriteLine Now & " ==> SUCCESS: Files related to the following folder " & dDir & " exist and were modified today."
' Write to log file
Wscript.Sleep(200)
strStamp = TimeStamp(Now())
strComputerName = CreateObject("WScript.Network").ComputerName
Set objLogFile = objFSO.CreateTextFile(dDir & "\" & strComputerName & "_" & strStamp, True)
objLogFile.WriteLine strStamp & " procedure has been launched."
objLogFile.Close
Set objLogFile = Nothing
End If
End Sub
'----------------------------------------------------------------------------
' Build string of current date time (DDMMYYYY_HHMMSS)
'----------------------------------------------------------------------------
Function TimeStamp(dtmDateTime)
TimeStamp = Year(dtmDateTime) & Right("0" & Month(dtmDateTime), 2) & Right("0" & Day(dtmDateTime), 2) & "_" & Right("0" & Hour(dtmDateTime), 2) & Right("0" & Minute(dtmDateTime), 2) & Right("0" & Second(dtmDateTime), 2)
End Function
In order to check if a file exist in a reference folder and it has been modified.
I would like to add some param in the procedure:
1- Add a loop number param which will allows to repeat the verification action based on the reported number.
2-Pause loop number is the second between the launch of each loop number
Example:
loop_number=3
pause_loop number=15
This means that the procedure should be applied 3 times and the pause number between the relaunch is15 second.
The log file should report when every file verification has been launched and when the procedure has finished.
3-Re-adapat the datediff condition in order to report it at the beginning of the script
datestamp_threshold= (d for days, h for hours, m for minutes, n for seconds)
age_threshold=(numeric based on the reported datestamp threshold
example:
datestamp_threshold=d
age_threshold=15
If you have questions, please contact me.
Thank you very much for your help.
ASKER
Exactly, the params proposal seems to be good for me.
Okay, check this over, I think it meets what you asked for. I couldn't easily test it here so please do so and make any corrections required.
FilesVerifications arrPrefix, strBaseDir, intLoopCount, intLoopDelay, strThresholdUnit, intThesholdNumber
'----------------------------------------------------------------------------
' Check if a file exist and if it was modified today
'----------------------------------------------------------------------------
Sub FilesVerifications(darrFile, dDir, intLoopCount, intLoopDelay, strThresholdUnit, intThesholdNumber)
' Assume no files found that were modified today
blnErrors = True
intCount = 1
' Access the desired folder
Set objFolder = objFSO.GetFolder(dDir)
Do While (blnErrors = True) And (intCount <= intLoopCount)
objLog.WriteLine Now & " ==> INFO: FilesVerification() - Looking for modified files, loop [" & intCount & "] of [" & intLoopCount & "]."
' Look at each file in this folder
For Each objFile In objFolder.Files
' See if this file matches any of the prefixes in the selection array
For Each strFile In darrFile
' See if this file matches a prefix
If LCase(Left(objFile.Name, Len(strFile))) = LCase(strFile) Then
' It matches, now see if it was modified today
If DateDiff(strThresholdUnit, objFile.DateLastModified, Now) >= intThreshNumber Then
' Modified today, flag it
blnErrors = False
End If
End If
Next
Next
If blnErrors = True Then
WScript.Sleep intLoopDelay * 1000
End If
intCount = intCount + 1
Loop
' See if any of the files checked didn't exist, or were not modified today
If blnErrors Then
objLog.WriteLine Now & " ==> ERROR: Files located at: " & dDir & " were not modified today"
Else
' No errors, all files good, log that
objLog.WriteLine Now & " ==> SUCCESS: Files related to the following folder " & dDir & " exist and were modified today."
' Write to log file
Wscript.Sleep(200)
strStamp = TimeStamp(Now())
strComputerName = CreateObject("WScript.Network").ComputerName
Set objLogFile = objFSO.CreateTextFile(dDir & "\" & strComputerName & "_" & strStamp, True)
objLogFile.WriteLine strStamp & " procedure has been launched."
objLogFile.Close
Set objLogFile = Nothing
End If
End Sub
'----------------------------------------------------------------------------
' Build string of current date time (DDMMYYYY_HHMMSS)
'----------------------------------------------------------------------------
Function TimeStamp(dtmDateTime)
TimeStamp = Year(dtmDateTime) & Right("0" & Month(dtmDateTime), 2) & Right("0" & Day(dtmDateTime), 2) & "_" & Right("0" & Hour(dtmDateTime), 2) & Right("0" & Minute(dtmDateTime), 2) & Right("0" & Second(dtmDateTime), 2)
End Function
~bp
ASKER
I am sorry, unable to test it today.
I will take the time to test it this week.
I will take the time to test it this week.
ASKER
Sorry for the delay. I will test it tomorrow as I don't have access to a PC right now.
Thank you for your comprehension.
Thank you for your comprehension.
ASKER
Hello Bill,
I tested the last proposal and adapat the script in order to call the function and I have some remarks.
The log file report that the file to verify in array has been modified today and this is not the case.
Additionnally the log file doesn't track the loop and the pause thought I reported values as constant.
What should we change in order to correct this?
I attached the reference folder called in the procedure.
Thank you very much for your help.
28984940_file_verification-based-on-.zip
I tested the last proposal and adapat the script in order to call the function and I have some remarks.
The log file report that the file to verify in array has been modified today and this is not the case.
Additionnally the log file doesn't track the loop and the pause thought I reported values as constant.
What should we change in order to correct this?
' ==============================================================================
' C O N S T A N T S & V A R I A B L E S
' ==============================================================================
' Define needed constants
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Const TriStateUseDefault = -2
' Specify path to folder of files to process and log file
WorkingDir = Replace(WScript.ScriptFullName, WScript.ScriptName, "")
strLogFile = WorkingDir & "\log-file.txt"
strStamp = TimeStamp(Now)
' ==============================================================================
' I N I T I A L I Z A T I O N
' ==============================================================================
' Create filesystem object
Set objFSO = CreateObject("Scripting.FileSystemObject")
' Open log file (append)
Set objLog = objFSO.OpenTextFile(strLogFile, ForAppending, True)
' ==============================================================================
' M A I N L O G I C
' ==============================================================================
FilesVerifications Array("commons"), "F:\2-Files-Storage-Reference\4.Script-templates\1.Scripts\2.VBS-BAT\2.1Reference-Folders\28984940_file_verification-based-on-prefix", 4, 2, "d", 4
' ==============================================================================
' W R A P U P
' ==============================================================================
' Done, cleanup and exit
objLog.Close
Wscript.Quit
'----------------------------------------------------------------------------
' Check if a file exist and if it was modified today
'----------------------------------------------------------------------------
Sub FilesVerifications(darrFile, dDir, intLoopCount, intLoopDelay, strThresholdUnit, intThreshNumber)
' Assume no files found that were modified today
blnErrors = True
intCount = 1
' Access the desired folder
Set objFolder = objFSO.GetFolder(dDir)
Do While (blnErrors = True) And (intCount <= intLoopCount)
objLog.WriteLine Now & " ==> INFO: FilesVerification() - Looking for modified files, loop [" & intCount & "] of [" & intLoopCount & "]."
' Look at each file in this folder
For Each objFile In objFolder.Files
' See if this file matches any of the prefixes in the selection array
For Each strFile In darrFile
' See if this file matches a prefix
If LCase(Left(objFile.Name, Len(strFile))) = LCase(strFile) Then
' It matches, now see if it was modified today
If DateDiff(strThresholdUnit, objFile.DateLastModified, Now) >= intThreshNumber Then
' Modified today, flag it
blnErrors = False
End If
End If
Next
Next
If blnErrors = True Then
WScript.Sleep intLoopDelay * 1000
End If
intCount = intCount + 1
Loop
' See if any of the files checked didn't exist, or were not modified today
If blnErrors Then
objLog.WriteLine Now & " ==> ERROR: Files located at: " & dDir & " were not modified today"
Else
' No errors, all files good, log that
objLog.WriteLine Now & " ==> SUCCESS: Files related to the following folder " & dDir & " exist and were modified today."
' Write to log file
Wscript.Sleep(200)
strStamp = TimeStamp(Now())
strComputerName = CreateObject("WScript.Network").ComputerName
Set objLogFile = objFSO.CreateTextFile(dDir & "\" & strComputerName & "_" & strStamp, True)
objLogFile.WriteLine strStamp & " procedure has been launched."
objLogFile.Close
Set objLogFile = Nothing
End If
End Sub
'----------------------------------------------------------------------------
' Build string of current date time (DDMMYYYY_HHMMSS)
'----------------------------------------------------------------------------
Function TimeStamp(dtmDateTime)
TimeStamp = Year(dtmDateTime) & Right("0" & Month(dtmDateTime), 2) & Right("0" & Day(dtmDateTime), 2) & "_" & Right("0" & Hour(dtmDateTime), 2) & Right("0" & Minute(dtmDateTime), 2) & Right("0" & Second(dtmDateTime), 2)
End Function
I attached the reference folder called in the procedure.
Thank you very much for your help.
28984940_file_verification-based-on-.zip
Okay, I think I interpretted the file selection criteria wrong. I was looking for files older than the interval, I think you want ones newer than it. If so, then try this, change:
to:
~bp
If DateDiff(strThresholdUnit, objFile.DateLastModified, Now) >= intThreshNumber Then
to:
If DateDiff(strThresholdUnit, objFile.DateLastModified, Now) < intThreshNumber Then
~bp
ASKER
Thank you very much for this correction.
Is it possible that you add in the the proposal: https://www.experts-exchan ge.com/que stions/289 98574/VB-s cript-File s-verifica tion-based -on-last-d ate-modifi ed-and-pre fix-with-l oop-number -and-pause -param-v2. html#a4198 5712
The last modification concerning DateDiff so I can accept as a best solution.
Thank you again for your help.
Is it possible that you add in the the proposal: https://www.experts-exchan
The last modification concerning DateDiff so I can accept as a best solution.
Thank you again for your help.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you again for your help.
Sub FilesVerifications(darrFil
~bp