[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Text File Manipulating

Posted on 2004-12-01
4
Medium Priority
?
151 Views
Last Modified: 2010-05-18
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

0
Comment
Question by:sublimation
  • 2
4 Comments
 
LVL 6

Expert Comment

by:Marv-in
ID: 12719160
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
 
LVL 14

Accepted Solution

by:
Tommy Kinard earned 2000 total points
ID: 12719382
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
 
LVL 14

Expert Comment

by:Shiju Sasidharan
ID: 12719454
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
 
LVL 14

Expert Comment

by:Tommy Kinard
ID: 12725696
Thanks! Glad I could help :)

dragontooth

0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question