Option Explicit
' ==============================================================================
' 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 TriStateUseDefault = -2
' Define global variables
Dim objFSO
Dim strCheckFolder
' Specify path to folder of files to process
strCheckFolder = "D:\LABELS\ChessODBCFiles\BedfordSO"
' ==============================================================================
' I N I T I A L I Z A T I O N
' ==============================================================================
' Create filesystem object
Set objFSO = CreateObject("Scripting.FileSystemObject")
' ==============================================================================
' M A I N L O G I C
' ==============================================================================
' Process the base folder
ProcessFolder strCheckFolder
' ==============================================================================
' W R A P U P
' ==============================================================================
' Done, cleanup and exit
Wscript.Quit
' ==============================================================================
' S U B R O U T I N E S & F U N C T I O N S
' ==============================================================================
Sub ProcessFolder(strCheckFolder)
' Define local variables
Dim objCheckFolder
Dim objCheckFile
' Make sure the folder exists
If Not objFSO.FolderExists(strCheckFolder) Then
Wscript.Echo "*ERROR* Folder [" & strCheckFolder & "] does not exist, quitting."
Wscript.Quit
End If
' Access the folder
Set objCheckFolder = objFSO.GetFolder(strCheckFolder)
' Look at all files in this folder, process each one
For Each objCheckFile In objCheckFolder.Files
ProcessFile objCheckFile
Next
End Sub
Sub ProcessFile(objCheckFile)
' Define local varaibles
Dim objFile
Dim strData
Dim arrLines
Dim arrFields
Dim blnFileChanged
Dim i
Dim datNextFutureWorkingDay
Dim strDelim
' Read the file contents into an array for processing
Set objFile = objFSO.OpenTextFile(objCheckFile.Path, ForReading, False, TriStateUseDefault)
strData = objFile.ReadAll
objFile.Close
Set objFile = Nothing
' Determine what is used in the file for line delimiters, and split file into lines
If InStr(strData, vbCrLf) > 0 Then
strDelim = vbCrLf
ElseIf InStr(strData, vbLf) > 0 Then
strDelim = vbLf
ElseIf InStr(strData, vbCr) > 0 Then
strDelim = vbCr
End If
arrLines = Split(strData, strDelim)
' Assume no changes needed to this file
blnFileChanged = False
' Look at each line of the file and check the date field
For i = 0 To UBound(arrLines)
' Split the fields of this line apart into an array (comma delim)
arrFields = Split(arrLines(i), ",")
' Make sure we found at least 7 columns (array indexes are 0 based...)
If UBound(arrFields) > 5 Then
' Check if 7th column is in the future, if not set it to tomorrows date
datNextFutureWorkingDay = NextFutureWorkingDay()
If DateDiff("d", datNextFutureWorkingDay, StringToDate(Trim(arrFields(6)))) < 0 Then
arrFields(6) = DateToString(datNextFutureWorkingDay)
arrLines(i) = Join(arrFields, ",")
blnFileChanged = True
End If
End If
Next
' If we changed any data in the file, then rewrite it now
If blnFileChanged Then
strData = Join(arrLines, vbCrLf)
Set objFile = objFSO.OpenTextFile(objCheckFile.Path, ForWriting, True)
objFile.Write(strData)
objFile.Close
Set objFile = Nothing
End If
End Sub
Function NextFutureWorkingDay()
' Calculate the next working day (no weekends, no holidays) in the future
NextFutureWorkingDay = DateAdd("d", 1, Now)
Do Until (Weekday(NextFutureWorkingDay) <> vbSaturday) And (Weekday(NextFutureWorkingDay) <> vbSunday) And (Not IsHoliday(NextFutureWorkingDay))
NextFutureWorkingDay = DateAdd("d", 1, NextFutureWorkingDay)
Loop
End Function
Function StringToDate(strDate)
' Convert a string inb format YYYYMMDD to a date type value
StringToDate = CDate(Mid(strDate, 5, 2) & "/" & Mid(strDate, 7, 2) & "/" & Mid(strDate, 1, 4))
End Function
Function DateToString(datDate)
' Convert a date type value to a text string in format YYYYMMDD
DateToString = Year(datDate) & LPad(Month(datDate), 2, "0") & LPad(Day(datDate), 2, "0")
End Function
Function LPad( strText, intLen, chrPad )
' Left pad a string to any length with a specified character
LPad = Right( String( intLen, chrPad ) & strText, intLen )
End Function
Function IsHoliday(dDate)
'Checks to see if passed date is a holiday
Dim iDay, iTmpDay, i
IsHoliday = 0
iDay = Day(dDate)
'Check if valid date first
If IsDate(dDate) Then
Select Case Month(dDate)
Case 1 'Jan
If iDay = 1 Then 'New Years
IsHoliday = 1
Else
If iDay = 2 Then 'Make sure new years doesn't fall on sunday.
'If so, today is a holiday.
If Weekday(DateAdd("d", -1, dDate)) = 1 Then
IsHoliday = 1
End If
Else
For i = 0 To 30 'Martin Luther King B-Day
If Weekday(DateAdd("d", i, CDate("1/1/" & Year(dDate)))) _
= 2 Then
If CDate(dDate) = CDate(DateAdd("d", i + 14, _
CDate("1/1/" & Year(dDate)))) Then
IsHoliday = 1
End If
Exit For 'PG 1/28
End If
Next
End If
End If
Case 2 'Feb
For i = 0 To 27 'President's Day
If Weekday(DateAdd("d", i, CDate("2/1/" & Year(dDate)))) = 2 _
Then
If CDate(dDate) = CDate(DateAdd("d", i + 14, _
CDate("2/1/" & Year(dDate)))) Then
IsHoliday = 1
End If
Exit For
End If
Next
Case 3 'Mar
Case 4 'Apr
Case 5 'May
For i = 1 To 7 'Memorial Day
If Weekday(DateAdd("d", "-" & i, _
CDate("5/31/" & Year(dDate)))) = 2 Then
If CDate(dDate) = CDate(DateAdd("d", "-" & i, _
CDate("5/31/" & Year(dDate)))) Then
IsHoliday = 1
End If
Exit For
End If
Next
Case 6 'Jun
Case 7 'Jul
If iDay = 4 Then 'Independence Day
IsHoliday = 1
Else
If iDay = 3 Then 'Make sure Independence Day doesn't
'fall on saturday. If so, Friday is a holiday.
If Weekday(DateAdd("d", 1, dDate)) = 7 Then
IsHoliday = 1
End If
Else
If iDay = 5 Then 'Make sure Independence
'Day doesn't fall on sunday. If so, Monday is a holiday.
If Weekday(DateAdd("d", -1, dDate)) = 1 Then
IsHoliday = 1
End If
End If
End If
End If
Case 8 'Aug
Case 9 'Sep
For i = 0 To 13 'Labor Day
If Weekday(DateAdd("d", i, CDate("9/1/" & _
Year(dDate)))) = 2 Then
If CDate(dDate) = CDate(DateAdd("d", i, _
CDate("9/1/" & Year(dDate)))) Then
IsHoliday = 1
End If
Exit For
End If
Next
Case 10 'Oct
For i = 0 To 13 'Columbus Day
If Weekday(DateAdd("d", i, CDate("10/1/" & _
Year(dDate)))) = 2 Then
If CDate(dDate) = CDate(DateAdd("d", i + 7, CDate("10/1/" & _
Year(dDate)))) Then
IsHoliday = 1
End If
Exit For
End If
Next
Case 11 'Nov
If iDay = 11 Then 'Veteran's Day
IsHoliday = 1
Else
If iDay = 10 Then 'Make sure Veterans Day doesn't fall
'on saturday. If so, Friday is a holiday.
If Weekday(DateAdd("d", 1, dDate)) = 7 Then
IsHoliday = 1
End If
Else
If iDay = 12 Then 'Make sure Veterans Day doesn't
'fall on sunday. If so, Monday is a holiday.
If Weekday(DateAdd("d", -1, dDate)) = 1 Then
IsHoliday = 1
End If
Else
For i = 0 To 28 'Thanksgiving & the Day After
If Weekday(DateAdd("d", i, CDate("11/1/" & _
Year(dDate)))) = 5 Then 'this is the first
'thursday of the month
If DateDiff("d", dDate, DateAdd("d", i + 21, _
CDate("11/1/" & Year(dDate)))) = 0 Then 'add 3
'weeks to the first to get the 4th (thanksgiving)
IsHoliday = 1
Exit For
End If
End If
If Weekday(DateAdd("d", i, CDate("11/1/" & _
Year(dDate)))) = 6 Then 'this is the day
'after thanksgiving
If DateDiff("d", dDate, DateAdd("d", i + 21, CDate("11/1/" & Year(dDate)))) = 0 Then
IsHoliday = 1
Exit For
End If
End If
Next
End If
End If
End If
Case 12 'Dec
If iDay = 25 Then 'Christmas
IsHoliday = 1
Else
If iDay = 24 Then 'Make sure Christmas Day doesn't
'fall on saturday. If so, Friday is a holiday.
If Weekday(DateAdd("d", 1, dDate)) = 7 Then
IsHoliday = 1
End If
Else
If iDay = 26 Then 'Make sure Christmas
'Day doesn't fall on sunday. If so, Monday is a holiday.
If Weekday(DateAdd("d", -1, dDate)) = 1 Then
IsHoliday = 1
End If
Else
If iDay = 31 Then 'Make sure new years
'doesn't fall on saturday. If so, today is a holiday.
If Weekday(DateAdd("d", 1, dDate)) = 7 Then
IsHoliday = 1
End If
End If
End If
End If
End If
Case Else
'Do nothing but return false
End Select
End If
End Function
Option Explicit
' ==============================================================================
' 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 TriStateUseDefault = -2
' Define global variables
Dim objFSO
Dim strCheckFolder
' Specify path to folder of files to process
strCheckFolder = "D:\LABELS\ChessODBCFiles\BedfordSO"
' ==============================================================================
' I N I T I A L I Z A T I O N
' ==============================================================================
' Create filesystem object
Set objFSO = CreateObject("Scripting.FileSystemObject")
' ==============================================================================
' M A I N L O G I C
' ==============================================================================
' Process the base folder
ProcessFolder strCheckFolder
' ==============================================================================
' W R A P U P
' ==============================================================================
' Done, cleanup and exit
Wscript.Quit
' ==============================================================================
' S U B R O U T I N E S & F U N C T I O N S
' ==============================================================================
Sub ProcessFolder(strCheckFolder)
' Define local variables
Dim objCheckFolder
Dim objCheckFile
' Make sure the folder exists
If Not objFSO.FolderExists(strCheckFolder) Then
Wscript.Echo "*ERROR* Folder [" & strCheckFolder & "] does not exist, quitting."
Wscript.Quit
End If
' Access the folder
Set objCheckFolder = objFSO.GetFolder(strCheckFolder)
' Look at all files in this folder, process each one
For Each objCheckFile In objCheckFolder.Files
ProcessFile objCheckFile
Next
End Sub
Sub ProcessFile(objCheckFile)
' Define local varaibles
Dim objFile
Dim strData
Dim arrLines
Dim arrFields
Dim blnFileChanged
Dim i
Dim datNextFutureWorkingDay
Dim strDelim
Dim strOrder
Wscript.Echo "Checking file [" & objCheckFile.Name & "]."
' Read the file contents into an array for processing
Set objFile = objFSO.OpenTextFile(objCheckFile.Path, ForReading, False, TriStateUseDefault)
strData = objFile.ReadAll
objFile.Close
Set objFile = Nothing
' Determine what is used in the file for line delimiters, and split file into lines
If InStr(strData, vbCrLf) > 0 Then
strDelim = vbCrLf
ElseIf InStr(strData, vbLf) > 0 Then
strDelim = vbLf
ElseIf InStr(strData, vbCr) > 0 Then
strDelim = vbCr
End If
arrLines = Split(strData, strDelim)
' Assume no changes needed to this file
blnFileChanged = False
strOrder = ""
' Look at each line of the file and check the date field
For i = 0 To UBound(arrLines)
' Split the fields of this line apart into an array (comma delim)
arrFields = Split(arrLines(i), ",")
' Get order number from first line of data file
If strOrder = "" Then
strOrder = Trim(arrFields(1))
End If
' Make sure we found at least 7 columns (array indexes are 0 based...)
If UBound(arrFields) > 5 Then
' Check if 7th column is in the future, if not set it to tomorrows date
datNextFutureWorkingDay = NextFutureWorkingDay()
If DateDiff("d", datNextFutureWorkingDay, StringToDate(Trim(arrFields(6)))) < 0 Then
arrFields(6) = DateToString(datNextFutureWorkingDay)
arrLines(i) = Join(arrFields, ",")
blnFileChanged = True
End If
End If
Next
' If we changed any data in the file, then rewrite it now
If blnFileChanged Then
Wscript.Echo " Dates updated in [" & objCheckFile.Name & "]."
strData = Join(arrLines, vbCrLf)
Set objFile = objFSO.OpenTextFile(objCheckFile.Path, ForWriting, True)
objFile.Write(strData)
objFile.Close
Set objFile = Nothing
Else
Wscript.Echo " No dates to update in [" & objCheckFile.Name & "]."
End If
' Rename file with order number
Wscript.Echo " Renaming file from [" & objCheckFile.Name & "] to [" & NewFileName(objCheckFile.Name, strOrder) & "]."
objCheckFile.Name = NewFileName(objCheckFile.Name, strOrder)
End Sub
Function NewFileName(strName, strOrder)
Dim arrTokens
arrTokens = Split(strName, "_")
NewFileName = arrTokens(0) & "_" & strOrder & "_" & arrTokens(2)
End Function
Function NextFutureWorkingDay()
' Calculate the next working day (no weekends, no holidays) in the future
NextFutureWorkingDay = DateAdd("d", 1, Now)
Do Until (Weekday(NextFutureWorkingDay) <> vbSaturday) And (Weekday(NextFutureWorkingDay) <> vbSunday) And (Not IsHoliday(NextFutureWorkingDay))
NextFutureWorkingDay = DateAdd("d", 1, NextFutureWorkingDay)
Loop
End Function
Function StringToDate(strDate)
' Convert a string inb format YYYYMMDD to a date type value
StringToDate = CDate(Mid(strDate, 5, 2) & "/" & Mid(strDate, 7, 2) & "/" & Mid(strDate, 1, 4))
End Function
Function DateToString(datDate)
' Convert a date type value to a text string in format YYYYMMDD
DateToString = Year(datDate) & LPad(Month(datDate), 2, "0") & LPad(Day(datDate), 2, "0")
End Function
Function LPad( strText, intLen, chrPad )
' Left pad a string to any length with a specified character
LPad = Right( String( intLen, chrPad ) & strText, intLen )
End Function
Function IsHoliday(dDate)
'Checks to see if passed date is a holiday
Dim iDay, iTmpDay, i
IsHoliday = 0
iDay = Day(dDate)
'Check if valid date first
If IsDate(dDate) Then
Select Case Month(dDate)
Case 1 'Jan
If iDay = 1 Then 'New Years
IsHoliday = 1
Else
If iDay = 2 Then 'Make sure new years doesn't fall on sunday.
'If so, today is a holiday.
If Weekday(DateAdd("d", -1, dDate)) = 1 Then
IsHoliday = 1
End If
Else
For i = 0 To 30 'Martin Luther King B-Day
If Weekday(DateAdd("d", i, CDate("1/1/" & Year(dDate)))) _
= 2 Then
If CDate(dDate) = CDate(DateAdd("d", i + 14, _
CDate("1/1/" & Year(dDate)))) Then
IsHoliday = 1
End If
Exit For 'PG 1/28
End If
Next
End If
End If
Case 2 'Feb
For i = 0 To 27 'President's Day
If Weekday(DateAdd("d", i, CDate("2/1/" & Year(dDate)))) = 2 _
Then
If CDate(dDate) = CDate(DateAdd("d", i + 14, _
CDate("2/1/" & Year(dDate)))) Then
IsHoliday = 1
End If
Exit For
End If
Next
Case 3 'Mar
Case 4 'Apr
Case 5 'May
For i = 1 To 7 'Memorial Day
If Weekday(DateAdd("d", "-" & i, _
CDate("5/31/" & Year(dDate)))) = 2 Then
If CDate(dDate) = CDate(DateAdd("d", "-" & i, _
CDate("5/31/" & Year(dDate)))) Then
IsHoliday = 1
End If
Exit For
End If
Next
Case 6 'Jun
Case 7 'Jul
If iDay = 4 Then 'Independence Day
IsHoliday = 1
Else
If iDay = 3 Then 'Make sure Independence Day doesn't
'fall on saturday. If so, Friday is a holiday.
If Weekday(DateAdd("d", 1, dDate)) = 7 Then
IsHoliday = 1
End If
Else
If iDay = 5 Then 'Make sure Independence
'Day doesn't fall on sunday. If so, Monday is a holiday.
If Weekday(DateAdd("d", -1, dDate)) = 1 Then
IsHoliday = 1
End If
End If
End If
End If
Case 8 'Aug
Case 9 'Sep
For i = 0 To 13 'Labor Day
If Weekday(DateAdd("d", i, CDate("9/1/" & _
Year(dDate)))) = 2 Then
If CDate(dDate) = CDate(DateAdd("d", i, _
CDate("9/1/" & Year(dDate)))) Then
IsHoliday = 1
End If
Exit For
End If
Next
Case 10 'Oct
For i = 0 To 13 'Columbus Day
If Weekday(DateAdd("d", i, CDate("10/1/" & _
Year(dDate)))) = 2 Then
If CDate(dDate) = CDate(DateAdd("d", i + 7, CDate("10/1/" & _
Year(dDate)))) Then
IsHoliday = 1
End If
Exit For
End If
Next
Case 11 'Nov
If iDay = 11 Then 'Veteran's Day
IsHoliday = 1
Else
If iDay = 10 Then 'Make sure Veterans Day doesn't fall
'on saturday. If so, Friday is a holiday.
If Weekday(DateAdd("d", 1, dDate)) = 7 Then
IsHoliday = 1
End If
Else
If iDay = 12 Then 'Make sure Veterans Day doesn't
'fall on sunday. If so, Monday is a holiday.
If Weekday(DateAdd("d", -1, dDate)) = 1 Then
IsHoliday = 1
End If
Else
For i = 0 To 28 'Thanksgiving & the Day After
If Weekday(DateAdd("d", i, CDate("11/1/" & _
Year(dDate)))) = 5 Then 'this is the first
'thursday of the month
If DateDiff("d", dDate, DateAdd("d", i + 21, _
CDate("11/1/" & Year(dDate)))) = 0 Then 'add 3
'weeks to the first to get the 4th (thanksgiving)
IsHoliday = 1
Exit For
End If
End If
If Weekday(DateAdd("d", i, CDate("11/1/" & _
Year(dDate)))) = 6 Then 'this is the day
'after thanksgiving
If DateDiff("d", dDate, DateAdd("d", i + 21, CDate("11/1/" & Year(dDate)))) = 0 Then
IsHoliday = 1
Exit For
End If
End If
Next
End If
End If
End If
Case 12 'Dec
If iDay = 25 Then 'Christmas
IsHoliday = 1
Else
If iDay = 24 Then 'Make sure Christmas Day doesn't
'fall on saturday. If so, Friday is a holiday.
If Weekday(DateAdd("d", 1, dDate)) = 7 Then
IsHoliday = 1
End If
Else
If iDay = 26 Then 'Make sure Christmas
'Day doesn't fall on sunday. If so, Monday is a holiday.
If Weekday(DateAdd("d", -1, dDate)) = 1 Then
IsHoliday = 1
End If
Else
If iDay = 31 Then 'Make sure new years
'doesn't fall on saturday. If so, today is a holiday.
If Weekday(DateAdd("d", 1, dDate)) = 7 Then
IsHoliday = 1
End If
End If
End If
End If
End If
Case Else
'Do nothing but return false
End Select
End If
End Function
~bp
File newname:"A20160816_2121382_CHESSPL.C SV", oldname:"A20160816_103569_ CHESSPL.CS V", line:1, olddate:20160817, newdate:20160818
File newname:"A20160816_2121351_CHESSPL.C SV", oldname:"A20160816_105115_ CHESSPL.CS V", line:1, olddate:20160817, newdate:20160818
File newname:"A20160816_2121351_CHESSPL.C SV", oldname:"A20160816_105115_ CHESSPL.CS V", line:2, olddate:20160817, newdate:20160818
File newname:"A20160816_2121351_CHESSPL.C SV", oldname:"A20160816_105115_ CHESSPL.CS V", line:3, olddate:20160817, newdate:20160818
File newname:"A20160816_2121351_CHESSPL.C SV", oldname:"A20160816_105115_ CHESSPL.CS V", line:4, olddate:20160817, newdate:20160818
File newname:"A20160816_2121351_CHESSPL.C SV", oldname:"A20160816_105115_ CHESSPL.CS V", line:5, olddate:20160817, newdate:20160818
File newname:"A20160816_2121351_CHESSPL.C SV", oldname:"A20160816_105115_ CHESSPL.CS V", line:6, olddate:20160817, newdate:20160818
File newname:"A20160816_2121331_CHESSPL.C SV", oldname:"A20160816_11254_C HESSPL.CSV ", line:1, olddate:20160817, newdate:20160818
File newname:"A20160816_2121331_CHESSPL.C SV", oldname:"A20160816_11254_C HESSPL.CSV ", line:2, olddate:20160817, newdate:20160818
File newname:"A20160816_2121490_CHESSPL.C SV", oldname:"A20160816_114852_ CHESSPL.CS V", line:1, olddate:20160815, newdate:20160818
File newname:"A20160816_2121490_CHESSPL.C SV", oldname:"A20160816_114852_ CHESSPL.CS V", line:2, olddate:20160817, newdate:20160818
File newname:"A20160816_2121433_CHESSPL.C SV", oldname:"A20160816_115125_ CHESSPL.CS V", line:1, olddate:20160817, newdate:20160818
File newname:"A20160816_2121433_CHESSPL.C SV", oldname:"A20160816_115125_ CHESSPL.CS V", line:2, olddate:20160817, newdate:20160818
File newname:"A20160816_2121433_CHESSPL.C SV", oldname:"A20160816_115125_ CHESSPL.CS V", line:3, olddate:20160817, newdate:20160818
File newname:"A20160816_2121466_CHESSPL.C SV", oldname:"A20160816_125458_ CHESSPL.CS V", line:1, olddate:20160817, newdate:20160818
File newname:"A20160816_2121466_CHESSPL.C SV", oldname:"A20160816_125458_ CHESSPL.CS V", line:2, olddate:20160817, newdate:20160818
File newname:"A20160816_2121490_CHESSPL.C SV", oldname:"A20160816_110254_ CHESSPL.CS V", line:1, olddate:20160817, newdate:20160818
File newname:"A20160816_2121490_CHESSPL.C SV", oldname:"A20160816_110254_ CHESSPL.CS V", line:2, olddate:20160817, newdate:20160818
objLogFile.WriteLine Now() & " - File newname:""" & strNewFileName & """, oldname:""" & objCheckFile.Name & """, line:" & i+1 & ", olddate:" & arrFields(6) & ", newdate:" & DateToString(datNextFutureWorkingDay)
~bp
W,2117626,CI/1940000/9148,0001/2117626/001,RICHARD ROURKE ,20160808,20160809,0001/2117626/001,0100,343921WT,1 ,1 ,UNIT,C
and it did change the date, and this was logged:8/19/2016 8:18:18 AM - File newname:"A20160815_2117626_CHESSPL.CSV", oldname:"A20160815_184101_CHESSPL.CSV", line:1, olddate:20160809, newdate:20160822
~bp
A20160816_082104_CHESSPL.C
Do you want to use the date and time from the existing file name, and add the order number from the contents of the file, or do you want a new data and time to be used that is the date and time when the script was executed?
If the file contents are not changed, do you still want the file name changed anyway?
~bp