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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get a scirpt to read a CSV and build text 2

Status
Not open for further replies.

kaiske

Technical User
May 30, 2010
6
AU
Hi All,

Im very new to vbscript and finding it hard to figure out some code to do what i need to do.

im trying to get a script to read a csv file which contains information like
i.e
1)name,location,number
2)name,location,number
3)name,location,number
4)name,location,number

The output i want is in a text format which reads somthing like this

Example:
Hello (name)
Your location is (Location)
Your number you have selected is (number)
Hello (name)
Your location is (Location)
Your number you have selected is (number)
Hello (name)
Your location is (Location)
Your number you have selected is (number)

Any help or direction would be most great!
 
What is really appearing and what is your addition for explaining? Like "1)", "2)", ... do they appear in the csv? Like "(name)", "(Location)", ... do you want them to appear in the output file?
 
Oh sorry, i hope this makes it abit clearer..

im very new to vbs and still trying to get my head around how i can do this.

i was meant to say the data in the CSV is

name1,location1,number1
name2,location2,number2
name3,location3,number3
etc

what i need the vbs to do is read the contents of the csv and save it into 3 arrays, so it can loop and write to an external text the following.

Hello name1
Your location is name1
Your number you have selected is number1
Hello name2
Your location is location2
Your number you have selected is number2
Hello name3
Your location is location3
Your number you have selected is number3

 
The only part I don't know how to do is create the hello. Your location etc. but the rest is in the format you looking for. It's a start.

Code:
Option Explicit

Dim objFSO, strTextFile, strData, strLine, arrLines, f
CONST ForReading = 1, ForWriting = 2, ForAppending = 8
	Set objFSO = CreateObject("Scripting.FileSystemObject") 
	
'Location of your file to be written too
	Set f = objFSO.OpenTextFile("E:\testfile.txt",8, True) 
		
'Location of your file to be read
	strTextFile = "e:\yourfile.csv"

'Open the text file - strData now contains the whole file
	strData = objFSO.OpenTextFile(strTextFile,ForReading).ReadAll

'Split the text file into lines
	arrLines = Split(strData,",")

'Step through the lines Write Line To File (strLine) 		
	For Each strLine in arrLines
		f.WriteLine strLine 
	Next

'Cleanup
Set objFSO = Nothing

MCITP:EA/SA, MCSE, MCSA, MCDBA, MCTS, MCP+I, MCP
 
This is the outline. (I spare the check of file existence etc.)
[tt]
'your givens[green]
s_in="c:\pqr\abc.csv"
s_out="c:\pqr\xyz.txt"[/green]

apref=array("Hello", "Your location is", "Your number you have selected is")

set fso=createobject("scripting.filesystemobject")
set f_in=fso.opentextfile(s_in,1)
set f_out=fso.opentextfile(s_out,2,true)

do while not f_in.atendofstream
s=trim(f_in.readline)
if s="" then
'do nothing and ignore the line
else
a=split(s,",")
t=""
for i=0 to ubound(apref)
if i<=ubound(a) then
if trim(a(i))<>"" then
t=t & apref(i) & " " & trim(a(i))
else
t=t & apref(i) & " " & "?" 'supplement to your specification
end if
else
t=t & apref(i) & " " & "?" 'supplement to your specification
end if
if i<>ubound(apref) then
t=t & vbcrlf
end if
next
f_out.writeline t
end if
loop

f_out.close
set f_out=nothing

f_in.close
set f_in=nothing
set fso=nothing
[/tt]
 
wow i was way off!

i couldn't figure out how to split the data.. thanks guys.

ill go try this out!

 
tsuji I thought I would give you a star for something I definitely could not do. Those upper bound statements still get me.

MCITP:EA/SA, MCSE, MCSA, MCDBA, MCTS, MCP+I, MCP
 
tsuji your a legend!

I got it working!

Just to make things abit harder would you guys know how i would create the following result.

welcome
the name you have entered is name1
today
your you are in location1, location1 is, number1
welcome
the name you have entered is name2
today
your you are in location2, location2 is, number2

im just trying to learn how to get vbs to grab data and make an output to fit the requirements i may have.

 
Code:
s_in="c:\pqr\abc.csv"
s_out="c:\pqr\xyz.txt"

set fso=createobject("scripting.filesystemobject")
set f_in=fso.opentextfile(s_in,1)
set f_out=fso.opentextfile(s_out,2,true)

do while not f_in.atendofstream
  a=split(trim(f_in.readline),",")
  if ubound(a)=2 then
    f_out.writeline "welcome" & vbcrlf & "the name you have entered is " & a(0) & vbcrlf & "today"
    f_out.writeline "your you are in " & a(1) & ", " & a(1) & " is, " & a(2)
  end if
loop

f_out.close
set f_out=nothing

f_in.close
set f_in=nothing
set fso=nothing

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
oh, i see.. i guess it wasn't that hard at all..

thanks heaps guys!

any tips where i can start learning and building on my vbs skills?

wheres a good starting place besides posting more questions on here?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top