Link to home
Start Free TrialLog in
Avatar of ammartahir1978
ammartahir1978Flag for United Kingdom of Great Britain and Northern Ireland

asked on

how to read a csv file and change the delivery date where needed

Hi All,

I have these CSV files attached.

In these files i have my sales order details.

How what happen is normally the delivery date in these files will be NEXT DATE so if order is placed today 26/07/2016 then the delivery date will be 27/07/2016 or any forward date.

Now some of the orders are on back order which means when the order was placed there was no stock available and the delivery date was set to next day delivery, but when order is release which means stock is received those orders with that specific Product will then automatically be released for depatch.

Now these orders are failing because the delivery date is still the original delivery date which was when this order was placed.

so for example order A placed on 21/07/2016 >>>> delivery date >>22/07/2016 so becasue there is no stock available order was put on hold. now on 26/07/2016 we received stock for this product for Order A. this order will now be release with original date 21/07/2016 >>>> delivery date 22/07/2016, when this order is transferred to WMS system it fails becasue the delivery date is old date.

So i want to read these files and change them if delivery date needs changing.

I have attached few files for you. Files can have multiple lines as well.

Thank you so much
A20160726_170116_CHESSPL.CSV
A20160726_170113_CHESSPL.CSV
Avatar of Bill Prew
Bill Prew

Which column is the date to change, and what value do you want to change it to?

~bp
Avatar of ammartahir1978

ASKER

Its the 7 Column.

So if the date in 7 column is LESS then TODAYS_DATE+1 then set it to TODAYS_DATE+1.

for example in the files the 7th column which is the delivery date of the order is 20160720 and today is 27th so delivery date should be set to 28th if that makes sense.

showing in my algorithm above:

20160720 is less then 20160728 (TRUE)

Set 2016072016 = 20160728

Exit
there will be lots of files in a folder with the same format i have attached. these are only examples, so each order has its own file.

we need to check each file.
Okay, this seems to work in a test here, save as a VBS and run after changing the base folder path in the code.

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 = "B:\EE\EE28959745\files"

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

   ' Read the file contents into an array for processing
   Set objFile = objFSO.OpenTextFile(objCheckFile.Path, ForReading, False, TriStateUseDefault)
   strData = objFile.ReadAll
   arrLines = Split(strData, vbCrLf)
   objFile.Close
   Set objFile = Nothing

   ' 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
         If DateDiff("d", Now, StringToDate(Trim(arrFields(6)))) < 1 Then
            arrFields(6) = DateToString(DateAdd("d", 1, Now))
            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 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

Open in new window

~bp
hiBill,

this is the error i got.
SODateChange.png
Are you able to share the file that this error occurred while processing?

~bp
I ran a test with those here and no error, are you sure there are not other files in the folder, perhaps that are not of this format?

I may have found a small bug with one of the files, but it didn't cause an error, just didn't update all the dates, I will look into that separate issue.

~bp
i have script in same folder as files would that cause anything issue?
they are all csv files same files which i uploaded here
Yes, having the script in the same folder is a problem, it will see itself as a file in the folder and try to read itself as data, generating an error.

~bp
this Bill,

script works fine but it only read the first but not the rest o the lines.

i-e file A20160610_104102_CHESSPL.CSV

shall i change the read file loop ?
Right, I had noticed that bug.  It does seem right as I reread it though, it reads the whole file, then splits all lines into an array that it processes all of.

I am out for the evening but will debug further tomorrow morning.

~bp
thanks Bill
I have notice one more think that the script is not changing dates for its 4 days back

so can you today's date should be changed to Todays+1

thank you

AT
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Bill this is great it works brilliant
WOOOHOOOO

I MEAN HE IS BEST EXPERT I HAVE EVER COME ACROSS. VERY PROFESSIONAL AND GREAT HUMAN
Glad that helped, and thanks for the kind words.

~bp