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

Read csv file and append to string

Status
Not open for further replies.

kriemer

Technical User
Nov 2, 2007
8
CA
I have a csv file, "List.csv", comprised of a single item per record. I would like a VBScript that will read each record and append it to a standard string. All strings will be saved to a single new file "Output.csv".

For example:

List.csv contains:
aaa
bbb
ccc

Output.csv
XXXXXaaaYYYYYYaaa
XXXXXbbbYYYYYYbbb
XXXXXcccYYYYYYccc


Any suggestions as to code?

Thanks

k

 
The code I have will add a prefix string to the left each csv file record.

The code so far will append a fixed string to each record of the file.
Code:
Const ForReading = 1
 Dim oFS     : Set oFS     = CreateObject( "Scripting.FileSystemObject" )
 Dim sDir    : sDir        = "z:\test"   'Target Files Directory <<<
 Dim oRECut : Set oRECut = New RegExp

 oRECut.Pattern = "TEST.csv" 'Target File
 Dim oRERpl : Set oRERpl = New RegExp
 oRERpl.Global    = True
 oRERpl.Multiline = True
 oRERpl.Pattern   = vbCrLf
 Dim oFile
 For Each oFile In oFS.GetFolder( sDir ).Files
     Dim oMTS : Set oMTS = oRECut.Execute( oFile.Name )
     If 1 = oMTS.Count Then
        With oMTS( 0 )

	sText =   "" _
                  & oRERpl.Replace(   oFile.OpenAsTextStream( ForReading ).ReadAll() _
                                    , vbCrLf & "STRING TO THE RIGHT" )

	oFS.CreateTextFile( oFile.Path, True ).Write sText
          sFina = oFile.Path
        End With
    End If
    Next

What is missing is adding a suffix string to the right of each record. Even more confusing to me is the right-hand string has to repeat the original record string


Thanks

k
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top