Results 1 to 8 of 8

Thread: Partial string replacement in text file

  1. #1

    Thread Starter
    Hyperactive Member Jason Badon's Avatar
    Join Date
    Feb 2001
    Location
    Colorado
    Posts
    329

    Partial string replacement in text file

    I have a text file with data like this

    -1100,1,0,0,100.00
    2050,1,0,0,100.00
    -23600,4,0,0,10.00
    -174,1,0,0,20.00
    0,-1,0,0,0

    I’m having a hard time trying to figure out how to replace
    a certain number on a specific line.


    example:
    I want to replace the first number on line three -23600
    to -24000.

    results:

    -1100,1,0,0,100.00
    2050,1,0,0,100.00
    -24000,4,0,0,10.00
    -174,1,0,0,20.00
    0,-1,0,0,0

    thank you for your help.
    Jason

  2. #2
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046
    didnt test this, but give it a try

    VB Code:
    1. Sourcefilename = "c:\test.whatever"
    2. FF = FreeFile
    3. Open Sourcefilename For Binary As #FF
    4. strfile = Space$(LOF(FF))
    5. Get #FF, 1, strfile
    6. Close #FF
    7. strarray1()=split(strfile,vbcrlf)
    8. strarray2()=split(strarray1(2),",")
    9. strarray2(0)="-24000"
    10. strarray1(2)=join(strarray2,",")
    11. strfile=join(strarray1,vbcrlf)
    12. open SourceFilename for output as #FF
    13. print #FF, strfile
    14. close #FF

  3. #3
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046
    or genrealize the problem like so (still not tested):

    VB Code:
    1. sub ReplaceNumber(RowNumber as Integer, ColumnNumber as Integer)
    2. Dim strfile as string, strarray1() as string, strarray2() as string
    3. Dim FF as Integer
    4. Sourcefilename = "c:\test.whatever"
    5. FF = FreeFile
    6. Open Sourcefilename For Binary As #FF
    7. strfile = Space$(LOF(FF))
    8. Get #FF, 1, strfile
    9. Close #FF
    10. strarray1()=split(strfile,vbcrlf)
    11. strarray2()=split(strarray1(RowNumber-1),",")
    12. strarray2(ColumnNumber-1)="-24000"
    13. strarray1(RowNumber-1)=join(strarray2,",")
    14. strfile=join(strarray1,vbcrlf)
    15. open SourceFilename for output as #FF
    16. print #FF, strfile
    17. close #FF

  4. #4
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046
    actually prolly better as (still not tested):

    VB Code:
    1. sub ReplaceNumber(RowNumber as Integer, ColumnNumber as Integer, RplString as String)
    2. Dim strfile as string, strarray1() as string, strarray2() as string
    3. Dim FF as Integer
    4. Sourcefilename = "c:\test.whatever"
    5. FF = FreeFile
    6. Open Sourcefilename For Binary As #FF
    7. strfile = Space$(LOF(FF))
    8. Get #FF, 1, strfile
    9. Close #FF
    10. strarray1()=split(strfile,vbcrlf)
    11. strarray2()=split(strarray1(RowNumber-1),",")
    12. strarray2(ColumnNumber-1)=ReplString
    13. strarray1(RowNumber-1)=join(strarray2,",")
    14. strfile=join(strarray1,vbcrlf)
    15. open SourceFilename for output as #FF
    16. print #FF, strfile
    17. close #FF
    18. End Sub

  5. #5
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046
    or even (still not tested):

    VB Code:
    1. sub ReplaceNumber(AFilename as String, RowNumber as Integer, ColumnNumber as Integer, RplString as String)
    2. Dim strfile as string, strarray1() as string, strarray2() as string
    3. Dim FF as Integer
    4. FF = FreeFile
    5. Open Afilename For Binary As #FF
    6. strfile = Space$(LOF(FF))
    7. Get #FF, 1, strfile
    8. Close #FF
    9. strarray1()=split(strfile,vbcrlf)
    10. strarray2()=split(strarray1(RowNumber-1),",")
    11. strarray2(ColumnNumber-1)=ReplString
    12. strarray1(RowNumber-1)=join(strarray2,",")
    13. strfile=join(strarray1,vbcrlf)
    14. open SourceFilename for output as #FF
    15. print #FF, strfile
    16. close #FF
    17. End Sub

  6. #6

    Thread Starter
    Hyperactive Member Jason Badon's Avatar
    Join Date
    Feb 2001
    Location
    Colorado
    Posts
    329
    Muddy thank you for your help. Code works great. If you wouldn't mind
    Could you add commits to the code so I learn what the code is doing?


    Thank you very much for your help..

    Jason

  7. #7
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046
    OK, Ill make a big admission here ... Im not much of a commenter. Heres my best shot. Hope it helps!

    btw are you sure this worked? I just found an error corrected below:

    VB Code:
    1. sub ReplaceNumber(AFilename as String, RowNumber as Integer, ColumnNumber as Integer, RplString as String)
    2. Dim strfile as string, strarray1() as string, strarray2() as string
    3. Dim FF as Integer
    4. FF = FreeFile
    5. Open Afilename For Binary As #FF
    6. strfile = Space$(LOF(FF))
    7. Get #FF, 1, strfile 'read the entire cotents of the file into a variable
    8. Close #FF
    9. strarray1()=split(strfile,vbcrlf) 'split the file up by rows into strarray1
    10. strarray2()=split(strarray1(RowNumber-1),",") 'split the selected row up into strarray2 using comma as delimiter
    11. strarray2(ColumnNumber-1)=ReplString ' replace the selected row and column data
    12. strarray1(RowNumber-1)=join(strarray2,",") 'join the selected row back up
    13. strfile=join(strarray1,vbcrlf) ' join the whole file back up
    14. open AFilename for output as #FF
    15. print #FF, strfile ' write it to the file
    16. close #FF
    17. End Sub

  8. #8

    Thread Starter
    Hyperactive Member Jason Badon's Avatar
    Join Date
    Feb 2001
    Location
    Colorado
    Posts
    329
    there were two typo errors in the code but were easy fixes.

    VB Code:
    1. sub ReplaceNumber(AFilename as String, RowNumber as Integer, ColumnNumber as Integer, RplString as String)
    2.  
    3.  
    4. ' had tochange RplString with ReplString
    5.  
    6. sub ReplaceNumber(AFilename as String, RowNumber as Integer, ColumnNumber as Integer, ReplString as String)
    7.  
    8.  
    9. '||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||\
    10.  
    11. open SourceFilename for output as #FF
    12. print #FF, strfile
    13. close #FF
    14.  
    15. ' had tochange SourceFilename with AFilename
    16.  
    17. open AFilename for output as #FF
    18. print #FF, strfile
    19. close #FF


    Thanks for the comments I understand it a little now..

    Jason

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width