Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Appending File Problem

snoburnssbsnoburnssb Posts: 11Member
i need to write a script to modify a fairly large file only in certain places using the file system object model. i was thinking something like this:

'read entire file into array
set fil1 = fso.getfile(file)
set ts = fil1.openastextstream(forreading)
do until ts.atendofstream = true
n = n + 1
indata(n) = ts.readline
loop
NumberOfLines = n
ts.close

'output file line by line, making changes as needed
set fil1 = fso.getfile(file)
set ts = fil1.openastextstream(forwriting)
for n = 1 to NumberOfLines
'script that determines if im at a point where a change is to be made
ts.writeline indata(n)
next n
ts.close

someone please tell me there is an easier way to do this. im kindof new to visual basic. thanks!

Comments

  • kapiloo7kapiloo7 Posts: 45Member
    : i need to write a script to modify a fairly large file only in certain places using the file system object model. i was thinking something like this:
    :
    : 'read entire file into array
    : set fil1 = fso.getfile(file)
    : set ts = fil1.openastextstream(forreading)
    : do until ts.atendofstream = true
    : n = n + 1
    : indata(n) = ts.readline
    : loop
    : NumberOfLines = n
    : ts.close
    :
    : 'output file line by line, making changes as needed
    : set fil1 = fso.getfile(file)
    : set ts = fil1.openastextstream(forwriting)
    : for n = 1 to NumberOfLines
    : 'script that determines if im at a point where a change is to be made
    : ts.writeline indata(n)
    : next n
    : ts.close
    :
    : someone please tell me there is an easier way to do this. im kindof new to visual basic. thanks!
    :

    u can do one thing. read entire file into a string (strFile suppose)
    .use REPLACE function, if u want to replace any text in file. if u just want to appent then conatenate new string to strFile and again write back the string (strFile) to file. and File System Object is not the only way to access files. u can use generic Open/Close functions.
    hope i gave something u wanted.

  • dokken2dokken2 Posts: 532Member
    : : i need to write a script to modify a fairly large file only in certain places using the file system object model. i was thinking something like this:
    : :
    : : 'read entire file into array
    : : set fil1 = fso.getfile(file)
    : : set ts = fil1.openastextstream(forreading)
    : : do until ts.atendofstream = true
    : : n = n + 1
    : : indata(n) = ts.readline
    : : loop
    : : NumberOfLines = n
    : : ts.close
    : :
    : : 'output file line by line, making changes as needed
    : : set fil1 = fso.getfile(file)
    : : set ts = fil1.openastextstream(forwriting)
    : : for n = 1 to NumberOfLines
    : : 'script that determines if im at a point where a change is to be made
    : : ts.writeline indata(n)
    : : next n
    : : ts.close
    : :
    : : someone please tell me there is an easier way to do this. im kindof new to visual basic. thanks!
    : :
    :
    : u can do one thing. read entire file into a string (strFile suppose)
    : .use REPLACE function, if u want to replace any text in file. if u just want to appent then conatenate new string to strFile and again write back the string (strFile) to file. and File System Object is not the only way to access files. u can use generic Open/Close functions.
    : hope i gave something u wanted.
    :
    :

    If you only need to append then don't even need to read the file.
    ie:

    Open fname For Append As filenum 'APPEND TO FILE
    Print #filenum, MyString
    Close filenum

  • snoburnssbsnoburnssb Posts: 11Member
    [b][red]This message was edited by snoburnssb at 2005-4-28 5:58:47[/red][/b][hr]
    : : : i need to write a script to modify a fairly large file only in certain places using the file system object model. i was thinking something like this:
    : : :
    : : : 'read entire file into array
    : : : set fil1 = fso.getfile(file)
    : : : set ts = fil1.openastextstream(forreading)
    : : : do until ts.atendofstream = true
    : : : n = n + 1
    : : : indata(n) = ts.readline
    : : : loop
    : : : NumberOfLines = n
    : : : ts.close
    : : :
    : : : 'output file line by line, making changes as needed
    : : : set fil1 = fso.getfile(file)
    : : : set ts = fil1.openastextstream(forwriting)
    : : : for n = 1 to NumberOfLines
    : : : 'script that determines if im at a point where a change is to be made
    : : : ts.writeline indata(n)
    : : : next n
    : : : ts.close
    : : :
    : : : someone please tell me there is an easier way to do this. im kindof new to visual basic. thanks!
    : : :
    : :
    : : u can do one thing. read entire file into a string (strFile suppose)
    : : .use REPLACE function, if u want to replace any text in file. if u just want to appent then conatenate new string to strFile and again write back the string (strFile) to file. and File System Object is not the only way to access files. u can use generic Open/Close functions.
    : : hope i gave something u wanted.
    : :
    : :
    :
    : If you only need to append then don't even need to read the file.
    : ie:
    :
    : Open fname For Append As filenum 'APPEND TO FILE
    : Print #filenum, MyString
    : Close filenum
    :
    :
    >>
    The REPLACE thing worked. Thanks!

Sign In or Register to comment.