We help IT Professionals succeed at work.
Get Started

how to change the file name and add alert

148 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

Comment
Watch Question
Test your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Distinguished Expert 2020
Commented:
This problem has been solved!
Unlock 1 Answer and 16 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE