Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.
Const ForReading = 1Const ForWriting = 2Set objFSO = CreateObject("Scripting.FileSystemObject")Set ReadFile = objFSO.OpenTextFile (wscript.arguments(0), ForReading)Set objFile = objFSO.GetFile(wscript.arguments(0))wscript.echo "Ready to create file: "& VBCRLF & objFile.ParentFolder & "\Download.csv"Set Writefile = objFSO.CreateTextFile (objFile.ParentFolder & "\Download.csv", ForWriting)' Read input file all at oncestrText = ReadFile.ReadAllReadfile.Close' Split input file into lines and store in arrayarrFileLines = Split(strText, vbCrLf)' Process first (headers) LineStrHeaderLine = arrFileLines(0)arrHeaderFields = Split(StrHeaderline, ",", -1, 1)strNewHeaderLine=""For k=0 To 16 strNewHeaderLine = strNewHeaderLine & arrHeaderFields(k) & ","Next'Rem modified Header 17strNewHeaderLine = strNewHeaderLine & " Postage and Packing" & ","For k=18 To 27 strNewHeaderLine = strNewHeaderLine & arrHeaderFields(k) & ","Next' New Header 28strNewHeaderLine = strNewHeaderLine & " Escrow ID" & ","' New Header 29strNewHeaderLine = strNewHeaderLine & " Invoice ID" & ","For k=28 To 32 strNewHeaderLine = strNewHeaderLine & arrHeaderFields(k) & ","Next' Modified Header 33strNewHeaderLine = strNewHeaderLine & " Address Line 2/District" & ","For k=34 To 37 strNewHeaderLine = strNewHeaderLine & arrHeaderFields(k) & ","NextstrNewHeaderLine = strNewHeaderLine & arrHeaderFields(38)Writefile.writeline strNewHeaderLineFor j = 1 To (UBound(arrFileLines)-1) ReadLine = arrFileLines (j) arrFields = Split(Readline, Chr(34), -1, 1) OutputLine="" ' Split data lines at quote marks ' Even numbered lines starting at 0 will be commas ' odd numbered lines will be the fields ' commas in fields are OK now ' Process up To & including Invoice Number For k=1 To 63 step 2 outPutLine=OutputLine & Chr(34) & arrFields(k) & Chr(34) & "," Next ' Add the new blank fields ' Escrow ID outPutLine=OutputLine & Chr(34) & "" & Chr(34) & "," ' Invoice ID outPutLine=OutputLine & Chr(34) & "" & Chr(34) & "," ' Add all remaining fields except last For k= 65 To 75 step 2 outPutLine=OutputLine & Chr(34) & arrFields(k) & Chr(34) & "," Next ' Add last field without trailing comma outPutLine=OutputLine & Chr(34) & arrFields(77) & Chr(34) Writefile.writeline outPutLineNextWritefile.Closewscript.echo "Download.csv written - processing completed"
"Import failed because the file does not have delimited first line"So I added a comma to the end of the first (header) line in download.csv and tried importing it again and it worked !!
Const ForReading = 1Const ForWriting = 2Set objFSO = CreateObject("Scripting.FileSystemObject")Set ReadFile = objFSO.OpenTextFile (wscript.arguments(0), ForReading)Set objFile = objFSO.GetFile(wscript.arguments(0))wscript.echo "Ready to create file: "& VBCRLF & objFile.ParentFolder & "\Download.csv"Set Writefile = objFSO.CreateTextFile (objFile.ParentFolder & "\Download.csv", ForWriting)' Read input file all at oncestrText = ReadFile.ReadAllReadfile.Close' Split input file into lines and store in arrayarrFileLines = Split(strText, vbCrLf)' Process first (headers) LineStrHeaderLine = arrFileLines(0)arrHeaderFields = Split(StrHeaderline, ",", -1, 1)strNewHeaderLine=""For k=0 To 16 strNewHeaderLine = strNewHeaderLine & arrHeaderFields(k) & ","Next'Rem modified Header 17strNewHeaderLine = strNewHeaderLine & " Postage and Packing" & ","For k=18 To 27 strNewHeaderLine = strNewHeaderLine & arrHeaderFields(k) & ","Next' New Header 28strNewHeaderLine = strNewHeaderLine & " Escrow ID" & ","' New Header 29strNewHeaderLine = strNewHeaderLine & " Invoice ID" & ","For k=28 To 32 strNewHeaderLine = strNewHeaderLine & arrHeaderFields(k) & ","Next' Modified Header 33strNewHeaderLine = strNewHeaderLine & " Address Line 2/District" & ","For k=34 To 37 strNewHeaderLine = strNewHeaderLine & arrHeaderFields(k) & ","Next'' AS PROMISED HEADER NOW ENDS WITH A COMMA 'strNewHeaderLine = strNewHeaderLine & arrHeaderFields(38) & ","Writefile.writeline strNewHeaderLineFor j = 1 To (UBound(arrFileLines)-1) ReadLine = arrFileLines (j) arrFields = Split(Readline, Chr(34), -1, 1) OutputLine="" ' Split data lines at quote marks ' Even numbered lines starting at 0 will be commas ' odd numbered lines will be the fields ' commas in fields are OK now ' Process up To & including Invoice Number For k=1 To 63 step 2 outPutLine=OutputLine & Chr(34) & arrFields(k) & Chr(34) & "," Next ' Add the new blank fields ' Escrow ID outPutLine=OutputLine & Chr(34) & "" & Chr(34) & "," ' Invoice ID outPutLine=OutputLine & Chr(34) & "" & Chr(34) & "," ' Add all remaining fields except last For k= 65 To 75 step 2 outPutLine=OutputLine & Chr(34) & arrFields(k) & Chr(34) & "," Next ' ' Add last field *** with *** trailing comma ' outPutLine=OutputLine & Chr(34) & arrFields(77) & Chr(34) & "," Writefile.writeline outPutLineNextWritefile.Closewscript.echo "Download.csv written - processing completed"
I notice that if you click the .vbs icon without dropping a file on it, then you get a MS script error popup, as expected. Is it possible to replace that with a Warning popup instead so if she does click it, it will remind her the icon will only work when you drop a .csv file on it ?
Const ForReading = 1Const ForWriting = 2MsgTitle = "CSV Transformer Utility"If wscript.arguments.Count = 0 Then dummy = MsgBox("There was an error:" & vbcrlf & "You need to drop a file!" , vbOKOnly + vbCritical, MsgTitle) wscript.QuitEnd If Set objFSO = CreateObject("Scripting.FileSystemObject")Set ReadFile = objFSO.OpenTextFile (wscript.arguments(0), ForReading)Set objFile = objFSO.GetFile(wscript.arguments(0))InputFileName=objFile.Pathdummy = MsgBox("Input file: " & vbcrlf & InputFileName & vbcrlf & vbcrlf & "Ready to create file: "& VBCRLF & objFile.ParentFolder & "\Download.csv" , vbInformation, MsgTitle)Set Writefile = objFSO.CreateTextFile (objFile.ParentFolder & "\Download.csv", ForWriting)' Read input file all at oncestrText = ReadFile.ReadAllReadfile.Close' Split input file into lines and store in arrayarrFileLines = Split(strText, vbCrLf)' Process first (headers) LineStrHeaderLine = arrFileLines(0)arrHeaderFields = Split(StrHeaderline, ",", -1, 1)strNewHeaderLine=""For k=0 To 16 strNewHeaderLine = strNewHeaderLine & arrHeaderFields(k) & ","Next'Rem modified Header 17strNewHeaderLine = strNewHeaderLine & " Postage and Packing" & ","For k=18 To 27 strNewHeaderLine = strNewHeaderLine & arrHeaderFields(k) & ","Next' New Header 28strNewHeaderLine = strNewHeaderLine & " Escrow ID" & ","' New Header 29strNewHeaderLine = strNewHeaderLine & " Invoice ID" & ","For k=28 To 32 strNewHeaderLine = strNewHeaderLine & arrHeaderFields(k) & ","Next' Modified Header 33strNewHeaderLine = strNewHeaderLine & " Address Line 2/District" & ","For k=34 To 37 strNewHeaderLine = strNewHeaderLine & arrHeaderFields(k) & ","Next'' AS PROMISED HEADER NOW ENDS WITH A COMMA 'strNewHeaderLine = strNewHeaderLine & arrHeaderFields(38) & ","Writefile.writeline strNewHeaderLineFor j = 1 To (UBound(arrFileLines)-1) ReadLine = arrFileLines (j) arrFields = Split(Readline, Chr(34), -1, 1) OutputLine="" ' Split data lines at quote marks ' Even numbered lines starting at 0 will be commas ' odd numbered lines will be the fields ' commas in fields are OK now ' Process up To & including Invoice Number For k=1 To 63 step 2 outPutLine=OutputLine & Chr(34) & arrFields(k) & Chr(34) & "," Next ' Add the new blank fields ' Escrow ID outPutLine=OutputLine & Chr(34) & "" & Chr(34) & "," ' Invoice ID outPutLine=OutputLine & Chr(34) & "" & Chr(34) & "," ' Add all remaining fields except last For k= 65 To 75 step 2 outPutLine=OutputLine & Chr(34) & arrFields(k) & Chr(34) & "," Next ' ' Add last field *** with *** trailing comma ' outPutLine=OutputLine & Chr(34) & arrFields(77) & Chr(34) & "," Writefile.writeline outPutLineNextWritefile.Closedummy = MsgBox("Processing completed" , vbInformation, MsgTitle)