Solved

how to change the file name and add alert

Posted on 2016-08-16
16
44 Views
Last Modified: 2016-08-19
Hi All

I have this script which change for dates and change them.

I want to add two things:

1. Can we rename the file to A<date>_Orderno_Time.csv ( order number is the second field in the file)
2. it reads all the files in the folder can a message alert be added to show its been processed?

Thank you for your help.

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

Open in new window

0
Comment
Question by:ammartahir1978
  • 8
  • 8
16 Comments
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
It looks like files are currently named like:

A20160816_082104_CHESSPL.CSV

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
0
 

Author Comment

by:ammartahir1978
Comment Utility
Hi Bill,

well spotted thank you.

I want file name to be

A20160816_082104_CHESSPL.CSV to A20160816_<ORDERNO>_CHESSPL.CSV

yes doesnt matter if contents are changed or not file name should change.

Thank you
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
Give this a test and see how you like it.  I added a few messages along the way in the processing of each file, these can be adjusted as needed.

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

Open in new window

~bp
0
 

Author Comment

by:ammartahir1978
Comment Utility
Hi Bill that is great, can these messages be only for where there is a change or a log file which says

file name changed to newfilename and despatch date change from <Olddate> to <New date>

the reason i am asking is becasue with these alerts i wont be able to run it via schedule task?
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
So do you always want a line showing the name change of the file?  Or only if there were date changes in it?

And then in addition you want one line for every line of a file that had a date change?

~bp
0
 

Author Comment

by:ammartahir1978
Comment Utility
no no no

all i want is if the script run :

it changes the file name doesnt matter there is date change or not but if there is a date change it can log it separately in a log file i think you are the expert i will go with your recommendation.
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
We can have it log whatever you like, I just want to settle on what that is before I code it, rather than having to change it several times.  Let me see if I can clarify.

You only want to log when a line of a data file is changed, and you want to include the filename in the log, as well as the old date and new date that was changed?  I would also suggest we add the line number to this message.

Do you want these message written to a file, and if so how do you want this log file named?  Should the file be added to each time the script runs, or should a new log file be created from empty?  Do you want the current date/time as part of the log file name, or the same file name used each time?

~bp
0
 

Author Comment

by:ammartahir1978
Comment Utility
You are right in what i need.

log file can be created in log folder of the path which will end BedfordSO\Log and file can be created for a day so one file each day with the Datestamp.
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 51

Accepted Solution

by:
Bill Prew earned 500 total points
Comment Utility
Okay, give this a try.  It writes to a TXT file in the LOG folder under the data files folder.

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 ForAppending = 8
Const TriStateUseDefault = -2

' Define global variables
Dim objFSO
Dim strCheckFolder
Dim strLogFile
Dim objLogFile

' 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")

' Define name for log file using todays day as "YYYYMMDD.txt", and open it
If Not objFSO.FolderExists(strCheckFolder & "\Log\") Then
   objFSO.CreateFolder(strCheckFolder & "\Log\")
End If
strLogFile = strCheckFolder & "\Log\" & Year(Now) & Right("0" & Month(Now), 2) & Right("0" & Day(Now), 2) & ".txt"
Set objLogFile = objFSO.OpenTextFile(strLogFile, ForAppending, True)

' ==============================================================================
' 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
   Dim strNewFileName

   ' 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))
         strNewFileName = NewFileName(objCheckFile.Name, strOrder)
      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
            ' Log this change
            objLogFile.WriteLine "File newname:""" & strNewFileName & """, oldname:""" & objCheckFile.Name & """, line:" & i+1 & ", olddate:" & arrFields(6) & ", newdate:" & DateToString(datNextFutureWorkingDay)
            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

   ' Rename file with order number
   objCheckFile.Name = strNewFileName

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

Open in new window

~bp
0
 

Author Comment

by:ammartahir1978
Comment Utility
thanks Bill this is perfect can i add the timestamp in log file? so i can identify what time which file was edited or amended?

File newname:"A20160816_2121382_CHESSPL.CSV", oldname:"A20160816_103569_CHESSPL.CSV", line:1, olddate:20160817, newdate:20160818
File newname:"A20160816_2121351_CHESSPL.CSV", oldname:"A20160816_105115_CHESSPL.CSV", line:1, olddate:20160817, newdate:20160818
File newname:"A20160816_2121351_CHESSPL.CSV", oldname:"A20160816_105115_CHESSPL.CSV", line:2, olddate:20160817, newdate:20160818
File newname:"A20160816_2121351_CHESSPL.CSV", oldname:"A20160816_105115_CHESSPL.CSV", line:3, olddate:20160817, newdate:20160818
File newname:"A20160816_2121351_CHESSPL.CSV", oldname:"A20160816_105115_CHESSPL.CSV", line:4, olddate:20160817, newdate:20160818
File newname:"A20160816_2121351_CHESSPL.CSV", oldname:"A20160816_105115_CHESSPL.CSV", line:5, olddate:20160817, newdate:20160818
File newname:"A20160816_2121351_CHESSPL.CSV", oldname:"A20160816_105115_CHESSPL.CSV", line:6, olddate:20160817, newdate:20160818
File newname:"A20160816_2121331_CHESSPL.CSV", oldname:"A20160816_11254_CHESSPL.CSV", line:1, olddate:20160817, newdate:20160818
File newname:"A20160816_2121331_CHESSPL.CSV", oldname:"A20160816_11254_CHESSPL.CSV", line:2, olddate:20160817, newdate:20160818
File newname:"A20160816_2121490_CHESSPL.CSV", oldname:"A20160816_114852_CHESSPL.CSV", line:1, olddate:20160815, newdate:20160818
File newname:"A20160816_2121490_CHESSPL.CSV", oldname:"A20160816_114852_CHESSPL.CSV", line:2, olddate:20160817, newdate:20160818
File newname:"A20160816_2121433_CHESSPL.CSV", oldname:"A20160816_115125_CHESSPL.CSV", line:1, olddate:20160817, newdate:20160818
File newname:"A20160816_2121433_CHESSPL.CSV", oldname:"A20160816_115125_CHESSPL.CSV", line:2, olddate:20160817, newdate:20160818
File newname:"A20160816_2121433_CHESSPL.CSV", oldname:"A20160816_115125_CHESSPL.CSV", line:3, olddate:20160817, newdate:20160818
File newname:"A20160816_2121466_CHESSPL.CSV", oldname:"A20160816_125458_CHESSPL.CSV", line:1, olddate:20160817, newdate:20160818
File newname:"A20160816_2121466_CHESSPL.CSV", oldname:"A20160816_125458_CHESSPL.CSV", line:2, olddate:20160817, newdate:20160818
File newname:"A20160816_2121490_CHESSPL.CSV", oldname:"A20160816_110254_CHESSPL.CSV", line:1, olddate:20160817, newdate:20160818
File newname:"A20160816_2121490_CHESSPL.CSV", oldname:"A20160816_110254_CHESSPL.CSV", line:2, olddate:20160817, newdate:20160818
0
 

Author Closing Comment

by:ammartahir1978
Comment Utility
He is the best expert on EE i would like to hire Bill and i think i will soon THANK YOU VERY MUCH SO ALL YOUR HELP AND SUPPORT.
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
Yes, adjust line 128 as follows:

objLogFile.WriteLine Now() & " - File newname:""" & strNewFileName & """, oldname:""" & objCheckFile.Name & """, line:" & i+1 & ", olddate:" & arrFields(6) & ", newdate:" & DateToString(datNextFutureWorkingDay)

Open in new window

~bp
1
 

Author Comment

by:ammartahir1978
Comment Utility
HI Bill,

The script is missing some files without changing the date, i had 6 files which got missed, is there anything you can do?
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
Can you provide the files that were not processed, and the date you fan the script please.

~bp
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
I ran a test with this data line:

W,2117626,CI/1940000/9148,0001/2117626/001,RICHARD ROURKE  ,20160808,20160809,0001/2117626/001,0100,343921WT,1        ,1        ,UNIT,C

Open in new window

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

Open in new window

~bp
0
 

Author Comment

by:ammartahir1978
Comment Utility
i dont know when i run the script it missed 6-10 files but i know you are right must be something else.

I have emailed you as well
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Modern/Metro styled message box and input box that directly can replace MsgBox() and InputBox()in Microsoft Access 2013 and later. Also included is a preconfigured error box to be used in error handling.
This article will show, step by step, how to integrate R code into a R Sweave document
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

771 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

10 Experts available now in Live!

Get 1:1 Help Now