Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

echo value after comma

Status
Not open for further replies.

g3ko

IS-IT--Management
Jul 18, 2013
1
GB
hi all,

I'm quite new to scripting and hoping you might be able to help out. I currently have a VB script which echos the last line of a csv file, the issue I have is I can't figure out how to echo the 2nd value after the comma:

1,2
3,4
5,6

My script currently will echo both values, any guidance would be much appreciated

Code:
Option Explicit

Dim oFSO, oRegEx, sFileName, sText, oMatches, number, comma



Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oRegEx = CreateObject("VBScript.RegExp")
oRegEx.Multiline = True
oRegEx.Global = True

sFileName = "C:\pandora\faxmulti.csv"

sText = oFSO.OpenTextFile(sFileName, 1).ReadAll
oRegEx.Pattern = "^([^\s]+)$"
Set oMatches = oRegEx.Execute(sText)
Set number = oMatches(oMatches.Count - 1)



Wscript.Echo number
 
Have a look at the Split function.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi [thumbsup]
This is a general script dealing with Content of a CSV file into a 2D Array
NB: The delimiter in this script is ";" so be careful if you choose your delimiter as ",". You have to change the script at line N°15 and N°26, and don't forget to change the PathFile at line N°3

Code:
Const ForReading = 1, ForWriting = 2 
Dim oFso, f
PathFile = "c:\Test\1.csv"
Set oFso = CreateObject("Scripting.FileSystemObject")
If Not OFso.FileExists(PathFile) Then
MsgBox "ATTENTION ! The File " & Dblquote(PathFile) & " Dosen't Exists !",48,"The File " & Dblquote(PathFile) & " Dosen't Exists !"
Wscript.Quit
End If
Set f = oFso.OpenTextFile(PathFile, ForReading)
ln=-1
cl=0
 
while Not f.AtEndOfStream '1ère itération pour définir les limites
    ln=ln+1 'définition indice lignes
    Tab=Split(f.ReadLine,";")
    If cl < UBound(Tab) Then cl = UBound(Tab) 
Wend
f.Close
MsgBox "indice lignes  = " & ln & Vbcr & "indice colonnes = " & cl,64,"Information"
 
Dim Tab2()
ReDim Tab2(ln,cl)
Set f = oFso.OpenTextFile(PathFile, ForReading)
i=0
while Not f.AtEndOfStream ' 2ème itération pour remplir le tableau
    Tab = Split(f.ReadLine,";")
    For j = 0 to UBound(Tab)
        Tab2(i,j) = Tab(j)
    Next
    i=i+1
Wend
f.Close
 
Dim OutPut : OutPut = "OutPut_Resultat.txt"
Set f = oFso.OpenTextFile(OutPut,ForWriting,True)
Set ws = CreateObject("Wscript.Shell")
 
For i=0 to UBound(Tab2,1) ' vérification
    For j=0 to UBound(Tab2,2)
        If j = UBound(Tab2,2) Then exit For
        f.WriteLine "The Value of Tab2("&i&","&j&") = " & Tab2(i,j) & " And the Value of Tab2("&i&","&j+1&") = "& Tab2(i,j+1) 
        MsgBox "The Value of Tab2("&i&","&j&") = " & Tab2(i,j) & " And the Value of Tab2("&i&","&j+1&") = "& Tab2(i,j+1),64,"Information"
    Next
Next
f.WriteLine VbNewline & String(75,"*")
f.WriteLine "Exemple : The Value of Tab2(1,1) ===> "& Tab2(1,1)
ws.run "Notepad " & OutPut

Function Dblquote(str)
	Dblquote = chr(34) & str & chr(34)
End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top