Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

how to change the file name and add alert

Posted on 2016-08-16
16
Medium Priority
?
98 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 8
16 Comments
 
LVL 57

Expert Comment

by:Bill Prew
ID: 41757749
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
ID: 41757761
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 57

Expert Comment

by:Bill Prew
ID: 41757792
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
Linux Academy Android App Now Supports Chromecast

We have some fantastic news for our Android fans. We’re so excited to announce that the Linux Academy Android app is now available with Chromecast support. That’s right – simply download the latest update of the Linux Academy App and start casting your favorite course videos!

 

Author Comment

by:ammartahir1978
ID: 41757821
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 57

Expert Comment

by:Bill Prew
ID: 41757885
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
ID: 41757899
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 57

Expert Comment

by:Bill Prew
ID: 41757974
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
ID: 41757983
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
 
LVL 57

Accepted Solution

by:
Bill Prew earned 2000 total points
ID: 41758317
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
ID: 41758852
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
ID: 41758855
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 57

Expert Comment

by:Bill Prew
ID: 41759164
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
ID: 41762089
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 57

Expert Comment

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

~bp
0
 
LVL 57

Expert Comment

by:Bill Prew
ID: 41762372
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
ID: 41762381
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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

722 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