Page 1 of 1

Multiple files appending

Posted: Thu Sep 29, 2005 5:16 am
by cryptid
Well i have to edit multiple files situated in diffrent locations in the computer i know it can be done using the command [open "C:\file.txt" for append as #1] but my problem is that i have to append the same lines in the other files also so is there a way that i can append the same lines at a time to all the files [i am a lazy person i dont like to type much & any way i need my program to be small and fast] so is there a way to open multiple files and append all of the at once i tried doing thins but it didnt work

open "C:\file.txt" for append as #1
open "D:\file123.txt" for append as #1
Print #1, "hi Blha Blha BLHA"
close #1

but the ompiler returned some error

Posted: Thu Sep 29, 2005 6:21 am
by Z!re

Code: Select all

stri$ = "Hello, World!"
open "C:\file.txt" for append as #1
open "D:\file123.txt" for append as #2
Print #1, stri$
Print #2, stri$
close #1 , #2
Or:

Code: Select all

dim flist(5) as string

flist(0) = "C:\file.txt"
flist(1) = "C:\blarga"
flist(2) = "C:\poof\tjoff"
flist(3) = "C:\Yadda"
flist(4) = "C:\random\random\whatever\file.txt"
flist(5) = "D:\otherhd\blipp"

stri$ = "Hello, World!"

for a = 0 to 5
  open flist(a) for append as #1
  Print #1, stri$
  close #1
next
I would recomend the second way..

Posted: Thu Sep 29, 2005 9:55 am
by Xerol
I think this works better:

Code: Select all

dim flist(6) as string 

flist(1) = "C:\file.txt" 
flist(2) = "C:\blarga" 
flist(3) = "C:\poof\tjoff" 
flist(4) = "C:\Yadda" 
flist(5) = "C:\random\random\whatever\file.txt" 
flist(6) = "D:\otherhd\blipp" 

stri$ = "Hello, World!" 

for a = 1 to 6
  open flist(a) for append as #a
next a

for a = 1 to 6
  Print #a, stri$ 
next a

'do other stuff here. you can post to all the files as much as you want using the above method

for a = 1 to 6
  close #a
next a
I use it a lot. I don't think many people know that you can use a variable after the # when referring to files.

Image

Posted: Thu Sep 29, 2005 11:43 am
by Z!re
You should use another array and store the file id in it, to get a valid fileid use FREEFILE.

But, your way will fail, as you can only open ~16 or so files, as specified in the config.sys file (iirc)

Mine is set to 32 for example..


The way I posted does the same thing, using less memory and less resources overall, it's always a good idea to keep as few files open as possible..

Posted: Thu Sep 29, 2005 2:14 pm
by {Nathan}
Z!re wrote:You should use another array and store the file id in it, to get a valid fileid use FREEFILE.

But, your way will fail, as you can only open ~16 or so files, as specified in the config.sys file (iirc)

Mine is set to 32 for example..


The way I posted does the same thing, using less memory and less resources overall, it's always a good idea to keep as few files open as possible..
you took the words straight out of my mouth.

Posted: Thu Sep 29, 2005 6:57 pm
by moneo
Right, Z!re's method is much simpler, easy to understand, and certainly will get the job done for ant number of files with no complications.

Nice work, Z!re! :D

BTW, Zerol, in your version, you don't need the FOR loop at the end to close the files. Just a simple CLOSE with no parameters will close all the files. As a matter of fact, unless the program is going to open any other files afterwards, you can just do an END or SYSTEM which will also close all the files, or code nothing at the end of the code, which does this by default.
*****

Posted: Thu Sep 29, 2005 7:04 pm
by Z!re
Thank you moneo =)