Results 1 to 13 of 13

Thread: [RESOLVED] Manipulate Text File

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2006
    Posts
    102

    Resolved [RESOLVED] Manipulate Text File

    I saw this example in another thread and decided it would make a good example for me to learn VBs file manipulation syntex. That thread is closed so I was asked to start a new thread.

    Lets say this data was in a file and it had numerous records. I think I'm close down there, but am doing something wrong. Thanks!

    ps. I guess what I'm trying to come up with is a standard way to open and write a file, and use the middle area to manipulate the text.

    MyString.txt.

    1This is the string I want to keep.****12/1/2006 USG8503
    2This is the string I want to keep.****12/1/2006 USG8503
    3This is the string I want to keep.****12/1/2006 USG8503
    4This is the string I want to keep.****12/1/2006 USG8503

    [Highlight=VB]
    Private Sub cmdClick()

    Dim sFileInfo() As String
    Dim i As Long

    Open "C:\MyString.txt" For Input As #1
    sFileInfo(i) = Left$(i, Len(i) - 21)
    Close #1

    Open "C:\AllFixedUp.txt" For Append As #1
    Print #1, sFileInfo(i)
    Close #1

    Next
    End Sub

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Manipulate Text File

    Code that you posted (btw, you missed the ending [/Highlight] tag) makes almost no sense - it's either you posted incomplete copy or you got incomplete copy from elsewhere.
    Anyway, if you look in our VB FAQ forum you will find article demonstrating how to work with the files.

  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Manipulate Text File

    Open "C:\MyString.txt" For Input As #1
    sFileInfo(i) = Left$(i, Len(i) - 21)
    Close #1
    i is meaningless here as it has no value
    you need to get the text from the file using
    VB Code:
    1. mystring =  Input(LOF(1),#1)
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jul 2006
    Posts
    102

    Re: Manipulate Text File

    I read the FAQ and just don't get the syntex. This is where I'm at but it still doesn't work.

    VB Code:
    1. Private Sub cmd1Click()
    2.  
    3. Dim mystring() As String
    4.  
    5. 'open file
    6. Open "C:\MyString.txt" For Input As #1
    7. mystring = Input(LOF(1), #1)
    8. Close #1
    9.  
    10. 'manipulate data
    11. mystring() = Left$(mystring, Len(mystring) - 21)
    12.  
    13. 'create new file
    14. Open "C:\AllFixedUp.txt" For Append As #1
    15. Print #1, mystring()
    16. Close #1
    17.  
    18. Next
    19. End Sub

  5. #5
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Manipulate Text File

    Don't declare the mystring variable as an array. Remove the (). Also remove "Next". There is no loop in that code. Next is for loops.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jul 2006
    Posts
    102

    Re: Manipulate Text File

    I removed the () and Next. Still does not create the new file.

    VB Code:
    1. Private Sub cmd1Click()
    2.  
    3. Dim mystring As String
    4.  
    5. 'open file
    6. Open "C:\MyString.txt" For Input As #1
    7. mystring = Input(LOF(1), #1)
    8. Close #1
    9.  
    10. 'manipulate data
    11. mystring = Left$(mystring, Len(mystring) - 21)
    12.  
    13. 'create new file
    14. Open "C:\AllFixedUp.txt" For Append As #1
    15. Print #1, mystring
    16. Close #1
    17.  
    18. End Sub

  7. #7
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Manipulate Text File

    You may like to check these things:

    Is the text in your file less than 21 characters?
    Does the end of your file contain 21 or more white space characters (space, enter, tab etc.)?

    Pradeep
    Last edited by Pradeep1210; Dec 2nd, 2006 at 09:07 AM.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  8. #8
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Manipulate Text File

    Since you are learning now is a great time to build a good habit.

    VB6 has the ability to step through code a line at a time.

    Click on the F8 button to start RUNNING your program.

    You will notice that the first executable line of code is highlighted in yellow. As you click F8 repeatedly you will "step" through the code one line at a time.

    Even better - if you hover over variables as you step you get to see the values in them. This is a great way to really start understanding what's going on in the code.

    Plus it's a good habit - DEBUGGING is probably 80% of programming!

    PraDeep wouldn't have to ask you questions about what's in your file and whether it's 21 characters - you would already know.

    Learn about debugging - learn about setting breakpoints. Learn about using the immediate window.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Jul 2006
    Posts
    102

    Re: Manipulate Text File

    Thanks for the F8 tip. I didn't know it could be started that way. I have a VB CBT that has a lot on debugging, I'll review it again. Here was one of the problems:

    Private Sub cmd1Click() should be Private Sub cmd1_Click()

    Now what it is doing is only removing the last line's "****12/1/2006 USG8503".

    1This is the string I want to keep.****12/1/2006 USG8503
    2This is the string I want to keep.****12/1/2006 USG8503
    3This is the string I want to keep.****12/1/2006 USG8503
    4This is the string I want to keep.

    It is grabbing the entire file instead of line-by-line.

  10. #10
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Manipulate Text File

    VB Code:
    1. Private Sub cmd1Click()
    2.  
    3. Dim mystring As String,[B] mylines() \As String, i As Long[/B]
    4.  
    5. 'open file
    6. Open "C:\MyString.txt" For Input As #1
    7. mystring = Input(LOF(1), #1)
    8. Close #1
    9.  
    10. 'manipulate data
    11. mylines = Split(mystring, vbNewLine)  'split file into lines
    12. for i = 0 to Ubound(mylines)   'process each line
    13.     mylines(i) = Left$(mylines(i), Len(mylines(i)) - 21)
    14. Next
    15.  
    16. 'create new file
    17. Open "C:\AllFixedUp.txt" For Append As #1
    18. Print #1, Join(mylines, vbNewLine)  'join all lines into file
    19. Close #1
    20.  
    21. End Sub
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Jul 2006
    Posts
    102

    Re: Manipulate Text File

    Thanks Westconn1, that is a perfect template to use when a file needs to be opened, the text needs to be manipulated, then saved as another file name (or appended).

    I'm trying to learn this and do not understand what this does. You document it by calling it "process each line."

    For i = 0 To UBound(mylines)

    Thanks again. This will go a long way in helping me understand text manipulation.

  12. #12
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Manipulate Text File

    note, that is only one way to do it
    another way many people prefer to use line input to read the file one line at a time, process each line and write the processed line to the new file, for very large files that maybe better, as the whole file does not have to be in memory

    there are more choices, such as loading into a byte array, or Random access which is used for fixed length fields type text datafiles
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  13. #13
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: [RESOLVED] Manipulate Text File

    VB Code:
    1. Dim ff As Integer
    2. Dim mystring As String
    3.  
    4. ff = FreeFile
    5. Open "C:\MyString.txt" For Input As #ff
    6.    mystring = Input(LOF(ff), #ff)
    7.    mystring = Replace(mystring, Right(mystring, 21), "")
    8. Close #ff

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