Results 1 to 12 of 12

Thread: Text file manipulation

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2001
    Location
    everywhere
    Posts
    111

    Question Text file manipulation

    Ok guys, I'm sure this isn't a tough one either...

    I need to open a text file, search for a version number, increment the version number, then close that file. Is there an easy way to do this. The version number could be anywhere in the file and the text file is fairly large, so I'm not sure if I can fit it all in one large string. Let me know what you guys think. Thanks a ton!!

    Jacob

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Jul 2001
    Location
    everywhere
    Posts
    111

    Cool ...one more thing

    I forgot to add that my program has a log file so it knows the old version number to search for. Thankk you, any help will be appreciated!!

    Jacob

  3. #3
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046
    do until EOF

    Open file1 for input as #1
    Open file2 for output as #2
    Line Input #1, some_string_variable
    if left(some_string_var, 7)="version" then
    print #2, "Version =" & new_version_number
    else
    print #2, some_string_var
    end if

    Loop


    Close #1
    Close #2

    kill file1
    rename file2 file1


    This is an algorithm (not real code) and assumes the word Version precedes the version number and is the first word of whatever line it is on.

    If the version id can occurr anywhere on the line (not just the first word), then you have a different provblem. In this case you might have to read in one word at a time till you find the version #.

    Hope this helps
    Last edited by Muddy; Jul 21st, 2001 at 08:14 PM.

  4. #4
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046
    btw be careful with the kill statement.

    It might be smarter to rename file1.whatever to file1.bak instead just in case things don't go smooth (you will have a copy of the original file)

  5. #5
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046
    just thought of something easier

    add the richtextbox control to your project

    VB Code:
    1. richtextbox1.loadfile ("C:\filename")
    2. richtextbox1.find ("version")
    3. richtextbox1.sellength=x  'make sure x is just enough to
    4.                                           'cover the number of characters you
    5.                                           'want to change
    6.  
    7. richtextbox1.seltext = "Version xxx"  'xxx is the new version
    8.                                                            'number

    now the richtext box contains the updated filetext which you can use to write a new file. Doesnt matter where Version Number is. You might run into a problem is the version numbers can have different nyumbers of characters (1, 10, 100, etc)

    anyway another option for you. hope it helps!

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jul 2001
    Location
    everywhere
    Posts
    111

    Thumbs up Thank you

    I really liked the way that the richtextbox control worked, however, when I save the file and open it again with notepad, it has all kinds of "junk" characters that I cannot allow to be in the file, for the file I'm modifing is the batch file that I plan on running once I update the version number. Any ideas on how to prevent VB from writing the extra crap to the file, or any other ideas?

    I really appreciate your help,

    thank you,

    Jacob

  7. #7
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046
    are you using something like this to save the updated file:


    Print #1 richtextbox1.text




    ??

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jul 2001
    Location
    everywhere
    Posts
    111
    Well, should I be using the print statement?

    What I did was this:

    RichTextBox1.SaveFile (sFile1)

    When I tried the print statement, it just printed to the screen and not the file.

    Thank you,

    Jacob

  9. #9
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046
    try this

    filename = "C\:whatever.txt"
    open filename for output as #1
    print #1, richtextbox1.text
    close


    this is off the top of my head so there may be syntax errors.
    let me know if it works.

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Jul 2001
    Location
    everywhere
    Posts
    111

    Talking Success

    Great, thanks a lot, that did the trick. Thank you!

    Jacob

  11. #11
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046
    glad to help!

    I guess you noticed that the last line of code should have been

    Close #1

    instead of

    Close

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Jul 2001
    Location
    everywhere
    Posts
    111
    yeah, i got that, thank you!

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