VBscript to append the text

Greeting,

I have a name.txt with the following one column data and would like to have a script to
create another txt with result:  'A','B','C','D', etc...

A
B
C
D

Please provide a sample code.
Thanks.
mrongAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

piattndCommented:
strSource = "C:\in.txt"
strDestination = "C:\out.txt"
Const ForReading = 1
Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objSource = objFSO.OpenTextFile(strSource,ForReading)
Set objDestination = objFSO.OpenTextFile(strDestination,ForWriting)

Do Until objSource.AtEndOfStream
	strReadText = objSource.ReadLine
	arrSplitData = Split(strReadText,",")
	For Each strData In arrSplitData
		objDestination.WriteLine(strData)
	Next
Loop

Open in new window

0
piattndCommented:
The sample code takes in.txt with data in comma separated format (1,2,3,4,5) and writes it to out.txt as each digit on one line:

1
2
3
4
5

If you had in.txt with the following data:

1,2,3,4,5
6,7,8,9,10

out.txt would look like:

1
2
3
4
5
6
7
8
9
10
0
mrongAuthor Commented:
piattnd,

Thank you for your post.

in.txt I have the following
A
B
C

out.txt I need the following
'A','B','C'
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

piattndCommented:
My mistake!  This code should do it for you.

strSource = "C:\in.txt"
strDestination = "C:\out.txt"
Const ForReading = 1
Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objSource = objFSO.OpenTextFile(strSource,ForReading)
Set objDestination = objFSO.OpenTextFile(strDestination,ForWriting)

i = 0

Do Until objSource.AtEndOfStream
	strReadText = objSource.ReadLine
	If i = 0 Then
		strWriteText = "'" + strReadText + "'"
	Else
		'append a comma
		strWriteText = ",'" + strReadText + "'"
	End If
	objDestination.Write(strWriteText)
	i = i + 1
Loop

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mrongAuthor Commented:
Tried your code and got the following error.

Line: 8
Char: 1
Error: File not found
Code: 800A0035
0
Bill PrewCommented:
Here's a slightly different approach, that uses REPLACE() to get the work done.  Save as a VBS file, and run as follows.  In the first example the script will read the in.txt file, and write the new version to the out.txt file.  In the second case it will read in.txt, make the changes, and then overwrite in.txt with the new version.

cscript EE28248298.vbs in.txt out.txt

or

cscript EE28248298.vbs in.txt

' Define needed constants
Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2

' Get input file name from command line parm, if 2 parms entered
' use second as new output file, else rewrite to input file
If (WScript.Arguments.Count > 0) Then
   strInFile = WScript.Arguments(0)
Else
   WScript.Echo "No filename specified."
   WScript.Quit
End If
If (WScript.Arguments.Count > 1) Then
   strOutFile = WScript.Arguments(1)
Else
   strOutFile = strInFile
End If

' Read file into an array
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strInFile, ForReading, False, TriStateUseDefault)
strData = objFile.ReadAll
objFile.Close

If Right(strData, 2) = vbCrLf Then strData = Left(strData, Len(strData) - 2)
strData = "'" & Replace(strData, vbCrLf, "','") & "'"

' Rewrite file with any changes made
Set objFile = objFSO.OpenTextFile(strOutFile, ForWriting, True)
objFile.Write strData
objFile.Close

Open in new window

~bp
0
piattndCommented:
The error you got means the remote file didn't exist.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.