Text File Manipulating

Hi, I have a text file that looks something like this:

1,"wewe",0.0004,"rtre5yt3e5yt",334
2,"wewe",0.0004,"rt356e5ytrts",53
3,"wewe",0.0004,"rtwerytwetyrtd",6
4,"wewe",0.0004,"rtwtw4trtb",8
5,"wewe",0.0004,"rtw4tw4trta",63333333

how can i go to each line and change the 4th item (i.e fourth comma away) "rtre5yt3e5yt" and change its value.   Do this for each row.  I cant use left function as the strings vary in length from end.  I want to basically, change the 4th value which is a string and change it to the value = Left((value), 2) & Right((value), 2).

so the fourth value on each line would read

rtyt
rtts
rttd
rttf
rttg

hope this aint too complicated and i hope it makes sense.

Thanks

LVL 4
sublimationAsked:
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.

Marv-inCommented:
you can use the split finction


myArray = split(value,",")
myArray(3) = new string

then
for L = 0 to ubound(myArray)
  newline = newline & myArray(L)
next
write newline to files
0
Tommy KinardCommented:
Hi sublimation,

Expaning on Marv-in's post:

Private Sub RdFlFix()
    Dim MyInfo As String
    Dim strData() As String
    'contents of csv.csv
    '1,"wewe",0.0004,"rtre5yt3e5yt",334
    '2,"wewe",0.0004,"rt356e5ytrts",53
    '3,"wewe",0.0004,"rtwerytwetyrtd",6
    '4,"wewe",0.0004,"rtwtw4trtb",8
    '5,"wewe",0.0004,"rtw4tw4trta",63333333
    Open "c:\csv.csv" For Input As #1
    Open "c:\Newcsv.csv" For Output As #2
    While Not EOF(1)
        Line Input #1, MyInfo
        strData = Split(Replace(MyInfo, Chr(34), vbNullString), ",")
        strData(3) = Left(strData(3), 2) & Right(strData(3), 2)
        Write #2, Val(strData(0)), strData(1), Val(strData(2)), strData(3), Val(strData(4))
    Wend
    Close #1
    Close #2
End Sub

HTH
dragontooth

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
Shiju SasidharanAssoc Project ManagerCommented:
Hi try this

'Dont forget to Include references
' Project Menu -> References
'
'  Microsoft VBScript Regular Expressions
'  Microsoft Scripting Runtime
'-----------------------------------------------------------
Private Sub ExtractFileWords(sFileName As String, sNewFileName As String)
Dim objRegExp As New RegExp
Dim objMatchCol As MatchCollection
Dim objFso As New FileSystemObject
Dim objTextStream As TextStream
Dim sContent As String
    With objRegExp
        .IgnoreCase = True
        .Global = True
        .Pattern = "(\d+,""\w+"",[\.\d]+,"")([a-z]{2})\w+([a-z]{2}"",\d+)"
    End With
    Set objTextStream = objFso.OpenTextFile(sFileName)
    sContent = objTextStream.ReadAll
    objTextStream.Close
    Set objTextStream = Nothing
    Set objMatchCol = objRegExp.Execute(sContent)
    If objMatchCol.Count > 0 Then
        sContent = objRegExp.Replace(sContent, "$1" & "$2" & "$3")
        Set objTextStream = objFso.CreateTextFile(sNewFileName, True)
        objTextStream.Write (sContent)
        objTextStream.Close
        Set objTextStream = Nothing
    End If
    Set objFso = Nothing
    Set objRegExp = Nothing
End Sub
'Usage
'Private Sub Command6_Click()
'    Call ExtractFileWords("C:\SourceFile.txt", "C:\DestinationFile.txt")
'End Sub
'----------------------------------------------------------------
;-)
Shiju


0
Tommy KinardCommented:
Thanks! Glad I could help :)

dragontooth

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
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.