Easy--creating and writing to a file

I want to create and write to a batch file.  I know about opening, reading, and closing files with a particular extension, but I don't know about writing.  Please help.
lmmAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RuchiCommented:
Open "C:\yourfile.txt" For Ouput As #1
Print #1, "YourMessages"
Close #1
0
st_steveCommented:
if you want to append to the already existing file, use this code:

Open App.Path & "\filename.txt" For Append As #1
Print#1, a, b, c, d
Close #1

Or instead of using Print, you can also use "Write #1"...the difference is that Write #1 will write to the file using  inverted commas as delimiters. eg:

if you use Print #1, "abc", "def"

you get:

abc, def

in the file you specified.

But if you use Write #1, "abc", "def"

then you get:

"abc", "def"

in the file.

the difference is not important...unless you want to pass the file (character by character)...then you have to trim out the inverted commas....and that would add additional coding..so..if you're planning to parse the file character by character, use Print...otherwise..there's no difference.

now...the way you read the file is simple.

you have the followin coding:

Open App.Path & "\filename.txt" for Input As #1
input #1, a, b, c, d
Close #1

one important thing to note here....if you have four fields in a file (in this example, a, b, c and d, you MUST read all fields at once....otherwise...your next line of input will be from all over the place..

So if you wrote the file using this code:

Open App.Path & "\filename.txt" for Output as #1
Write #1, a, b, c, d
Write #1, e, f, g, h
Close #1

and you try to read using the following code:

Open App.Path & "\filename.txt" for Input As #1
Input #1, a, b, c
Input #1, d, e, f
Close #1

You'll have a problem when you want to read the third line....coz the data will run out as soon as you read the third item on the third line...got it??

if not..msg me back...I'll explain again....

but the point I was making....read the same number of fields that you have written...

so if you use

Write #1, a, b, c, d

do a

Input #1, a, b, c, d

ok??
0
mhdhallakCommented:
I think st steve pretty well explained to you the basics of reading and writing to a file.
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

lmmAuthor Commented:
Ruchi,

I used your code and it works fine, however I want to put the contents from a text box into the file.  How do I do that?

lmm
0
RuchiCommented:
'write the contents of the text box text1 into the file yourfile.txt
Open "C:\YourFile.txt" For Output As #1
'Text1.Text = "YourMessages"
Print #1, Text1.Text
Close #1
0
AdamB_OpenCommented:
Warning.

Use:

dim f as long
f=freefile()

Open .... as #f
....
print #f
....
close #f

freefile gives a unique file identifier, so that if you have multiple files open you can identify each one, they will not all be #1.
0
lmmAuthor Commented:
Ruchi,

I appreciate your response, however I have tried this method and it doesn't work.

lmm
0
RuchiCommented:
Imm: Can you explain what is not working?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
lmmAuthor Commented:
Ruchi-It creates the file, no problem, but nothing is in the file.
0
lmmAuthor Commented:
Ruchi,

I found the problem, I created the file too soon.  Thanks for all of your help!!

lmm
0
lmmAuthor Commented:
Thank you for all of your help!

lmm
0
RuchiCommented:
Glad that you got it, Imm.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.