asked on
ASKER
ASKER
ASKER
ASKER
ASKER
ENTITY;PARTNER;ACCOUNT;Custom1;Custom2;Custom3;Custom4;TRANSID;SUBID;DATE;TRANSCURR;TRANSAMT;ENTCURRAMT;RATE;REFID;COMMENT1;COMMENT2,
,AT0900;A14110;CLO;10/25/2017;1800000055;[NONE];1800000055;1800000055;07/21/2017;EUR;29934.43;29934.43;1.00000;1119997294;;,
ASKER
' Define needed I/O constants
Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2
' Get input file name from command line parm
If (WScript.Arguments.Count > 0) Then
strInFile = WScript.Arguments(0)
Else
WScript.Echo "No input filename specified."
WScript.Quit
End If
' Get output file name from command line parm (if none, overwrite input file)
If (WScript.Arguments.Count > 1) Then
strOutFile = WScript.Arguments(1)
Else
strOutFile = strInFile
End If
' Create filesystem object
Set objFSO = CreateObject("Scripting.FileSystemObject")
' Make sure input file exists
If Not objFSO.FileExists(strInFile) Then
Wscript.Echo "ERROR: Input file """ & strInFile & """ does not exist."
Wscript.Quit
End If
' Read entire file contents
Set objFile = objFSO.OpenTextFile(strInFile, ForReading, False, TriStateUseDefault)
strData = objFile.ReadAll
objFile.Close
' Split input data at line breaks
arrData = Split(strData, vbCrLf)
' Look at each line and reformat dates as needed
For i = 0 To UBound(arrData)
' Split fields at semi-colons
arrField = Split(arrData(i), ";")
' Check 4th field and if valid date reformat it
If UBound(arrField) > 2 Then
If IsDate(arrField(3)) Then
arrField(3) = DoFormat(arrField(3))
End If
End If
' Check 9th field and if valid date reformat it
If UBound(arrField) > 7 Then
If IsDate(arrField(8)) Then
arrField(8) = DoFormat(arrField(8))
End If
End If
' Check 7th field and force to "[NONE]"
If UBound(arrField) > 5 Then
If arrField(6) <> "Custom4" Then
arrField(6) = "[NONE]"
End If
End If
' Update input line with any changes
arrData(i) = Join(arrField, ";")
Next
' Write output file with any changes
Set objFile = objFSO.OpenTextFile(strOutFile, ForWriting, True)
objFile.Write Join(arrData, vbCrLf)
objFile.Close
Function DoFormat(strDate)
' Format date string to DD MMMM YY format
datDate = CDate(strDate)
DoFormat = ZPad(Day(datDate), 2) & " " & MonthName(Month(datDate), False) & " " & Right(Year(datDate), 2)
End Function
Function ZPad(strText, intLen)
' Pad with leading zeros on the left to specified length
ZPad = Right(String(intLen, "0") & strText, intLen)
End Function
ASKER
ASKER
!COLUMN_ORDER = ENTITY;PARTNER;ACCOUNT;Custom1;Custom2;Custom3;Custom4;TRANSID;SUBID;DATE;TRANSCURR;TRANSAMT;ENTCURRAMT;RATE;REFID;COMMENT1;COMMENT2
RU0701;AT0902;CLO;11/25/2017;5100256069;[NONE];595758;1108722675;08/02/2017;EUR;138.00-;9760.96-;70.73170;1121909579;;
RU0701;AT0902;L34110;CLO;11/25/2017;5100256374;[NONE];595923;1108624316;08/17/2017;EUR;56676.00-;3967081.96-;69.99580;1122022536;;
RU0701;AT0902;CLO;11/25/2017;5100256069;[NONE];595758;1108722675;08/02/2017;EUR;138.00-;9760.96-;70.73170;1121909579;;
RU0701;AT0902;L34110;CLO;11/25/2017;5100256374;[NONE];595923;1108624316;08/17/2017;EUR;56676.00-;3967081.96-;69.99580;1122022536;;
But the "format" of these two lines seems to be different. Notice that in the first one the "CLO" in in field 2 (relative to zero), while in the second line the "CLO" is in field 3. This seems inconsistent. I'm hoping this is just a typo, and I adjusted the two lines to be this for my testing. But if they really are different like that then we have a problem.RU0701;AT0902;CLO;11/25/2017;5100256069;[NONE];595758;1108722675;08/02/2017;EUR;138.00-;9760.96-;70.73170;1121909579;;
RU0701;AT0902;CLO;11/25/2017;5100256374;[NONE];595923;1108624316;08/17/2017;EUR;56676.00-;3967081.96-;69.99580;1122022536;;
The revised script code:' Require variables to be defined before usage
Option Explicit
' Define needed I/O constants
Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2
Dim objFSO, strInfile, strOutFile, objFile
Dim strData, arrData, arrField, i, intChanges
' Get input file name from command line parm
If (WScript.Arguments.Count > 0) Then
strInFile = WScript.Arguments(0)
Else
WScript.Echo "No input filename specified."
WScript.Quit
End If
' Get output file name from command line parm (if none, overwrite input file)
If (WScript.Arguments.Count > 1) Then
strOutFile = WScript.Arguments(1)
Else
strOutFile = strInFile
End If
' Create filesystem object
Set objFSO = CreateObject("Scripting.FileSystemObject")
' Make sure input file exists
If Not objFSO.FileExists(strInFile) Then
Wscript.Echo "ERROR: Input file """ & strInFile & """ does not exist."
Wscript.Quit
End If
' Read entire file contents
Set objFile = objFSO.OpenTextFile(strInFile, ForReading, False, TriStateUseDefault)
strData = objFile.ReadAll
objFile.Close
' Split input data at line breaks
arrData = Split(strData, vbCrLf)
' Look at each line and reformat dates as needed
For i = 0 To UBound(arrData)
' Split fields at semi-colons
arrField = Split(arrData(i), ";")
' Make any needed adjustments to the various fields (columns)
' NOTE! field numbers are zero based
intChanges = 0
intChanges = intChanges + AdjustDate(arrField, 3)
intChanges = intChanges + AdjustNone(arrField, 6)
intChanges = intChanges + AdjustDate(arrField, 8)
intChanges = intChanges + AdjustNumber(arrField, 11)
intChanges = intChanges + AdjustNumber(arrField, 12)
intChanges = intChanges + AdjustNumber(arrField, 13)
' Update input line if any changes made
If intChanges > 0 Then
arrData(i) = Join(arrField, ";")
End If
Next
' Write output file with any changes
Set objFile = objFSO.OpenTextFile(strOutFile, ForWriting, True)
objFile.Write Join(arrData, vbCrLf)
objFile.Close
' Check field and if valid date reformat it
Function AdjustDate(arrField(), intIndex)
Dim datDate
' Assume no change made to this field
AdjustDate = 0
' Make sure it exists in the array
If UBound(arrField) >= intIndex Then
' Make sure it is a valid date value
If IsDate(arrField(intIndex)) Then
' Format date string to DD MMMM YY format
datDate = CDate(arrField(intIndex))
arrField(intIndex) = ZPad(Day(datDate), 2) & " " & MonthName(Month(datDate), False) & " " & Right(Year(datDate), 2)
' Indicate we made a change to this field
AdjustDate = 1
End If
End If
End Function
' Check field and force it to "[NONE]"
Function AdjustNone(arrField(), intIndex)
' Assume no change made to this field
AdjustNone = 0
' Make sure it exists in the array
If UBound(arrField) >= intIndex Then
' If not the header row force it to "[NONE]"
If arrField(intIndex) <> "Custom4" Then
arrField(intIndex) = "[NONE]"
' Indicate we made a change to this field
AdjustNone = 1
End If
End If
End Function
' Check field and adjust sign position of number
Function AdjustNumber(arrField(), intIndex)
' Assume no change made to this field
AdjustNumber = 0
' Make sure it exists in the array
If UBound(arrField) >= intIndex Then
' If negative sign on right, move it to left
If Right(arrField(intIndex), 1) = "-" Then
arrField(intIndex) = "-" & Left(arrField(intIndex), Len(arrField(intIndex))-1)
' Indicate we made a change to this field
AdjustNumber = 1
End If
End If
End Function
Function ZPad(strText, intLen)
' Pad with leading zeros on the left to specified length
ZPad = Right(String(intLen, "0") & strText, intLen)
End Function
ASKER
ASKER
VBScript (Visual Basic Scripting Edition) is an interpreted scripting language developed by Microsoft that is modeled on Visual Basic, but with some important differences. VBScript is commonly used for automating administrative and other tasks in Windows operating systems (by means of the Windows Script Host) and for server-side scripting in ASP web applications. It is also used for client-side scripting in Internet Explorer, specifically in intranet web applications.
TRUSTED BY
Use the format function:
Open in new window