Results 1 to 7 of 7
  1. #1
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Append to text file (VBS)

    I'm wondering what the most efficient method would be to prepend a single line to a text file, and append another single line. The text file itself might be in the megabyte range, and the Windows Script Help suggests that:
    "For large files, using the ReadAll method wastes memory resources. Other techniques should be used to input a file, such as reading a file line by line."
    Line by line seems rather inefficient too, since nothing will be modified. I was hoping to do something like create output.txt, containing just "prepend line", then use the equivalent of the DOS command

    COPY output.txt + original.txt output.txt

    Finally I could reopen output.txt and write the single "append line" to the end. Or maybe use a similar DOS-style command to tack append.txt on the (other) end.

    Alan

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Append to text file (VBS)

    Reading a megabyte file line by line would be S L O W. I'd create single-line files for the prepend line and the postpend line, and use the DOS copy command to concatenate the three files.

  3. #3
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Append to text file (VBS)

    Thanks Hans. I'd thought of that one, but (being totally ignorant on the subject) how would this work for an XP system; indeed how could it be coded so that it would work for any (unknown) Windows O/S? Is this an issue at all?

    Alan

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Append to text file (VBS)

    You can use Shell:

    Shell "Cmd /c copy /y a.txt+b.txt+c.txt d.txt"

  5. #5
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Append to text file (VBS)

    What I'm wondering about is using something like:
    <code>
    Shell Environ("ComSpec") & _
    " /c copy /y c:_prep.txt+" & FName & "+c:_app.txt Cutput.txt", vbHide
    </code>

    VBA in Office 2000 under 98SE returns "C:WINDOWSCOMMAND.COM" for Environ("ComSpec") . Would it return CMD or COMMAND in the case of XP? More to the point, would it work as intended? I can't seem to find any references to this situation.

    Alan

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Append to text file (VBS)

    On my Windows XP machine, Environ("ComSpec") returns

    C:WINDOWSsystem32cmd.exe

    So I think it'll work correctly in all Windows versions.

  7. #7
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Append to text file (VBS)

    Thanks Hans. <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16>

    Alan

Posting Permissions

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