p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   VB How-To (http://p2p.wrox.com/forumdisplay.php?f=78)
-   -   Writing to a .BAT file using VB6 (http://p2p.wrox.com/showthread.php?t=23509)

rhysduk January 5th, 2005 06:46 PM

Writing to a .BAT file using VB6
 
Hi all,

How do i write to a .BAT file using visual basic 6?

I need it for a program im making, due to the fact that public variables (Public strRegistration As Integer) lose their value when my application is lost.. so i need a permantly set string, or somethingt hat my program can read and interpret. The user willl be setting the value of the strings in the BAT file as it contains username and a serial number.

Regards
Rhys


Vadivel January 6th, 2005 12:05 AM

You can use something like this or File system object to write a bat file.

Code:

Open App.Path & "\Test.bat" For Output As #1
   Print #1, "@echo off"
   Print #1, "cls"
   Print #1, "@echo Test file"
Close #1

Best Regards
Vadivel

MVP ASP/ASP.NET
http://vadivel.thinkingms.com

rhysduk January 6th, 2005 10:21 AM

Thanks,

This works perfectly.. my next problem is though, how do i get visual basic to read the BAT file? i want to set a value to a string EG:

strUsername = Me

I want visual basic to be able to return the value of "strUsername" from the BAT file.. and then set this value to a public variable in visual basic!

Anyone have any ideas please?
Rhys


SerranoG January 6th, 2005 10:27 AM

What do you mean by "read the BAT file"? Do you mean execute it (i.e. use the SHELL VBA command) or do you mean actually read its contents?

Greg Serrano
Michigan Dept. of Environmental Quality, Air Quality Division

rhysduk January 6th, 2005 10:29 AM

Sry i meant read the contents that i have writen to it.

Rhys


Vadivel January 6th, 2005 01:13 PM

http://www.cpcug.org/user/clemenzi/t...ges/FileIO.htm -- this might give you a decent start...

Best Regards
Vadivel

MVP ASP/ASP.NET
http://vadivel.thinkingms.com

rhysduk January 6th, 2005 03:37 PM

Thank You a big help!

I can now write to any type of file that i want (.txt or .bat)

My next problem is reading the contents of the file !?!?

I have written the following contents to the file using :

Code:

Private Sub Form_Click()
Open App.Path & "\Test.Txt" For Output As #1
Print #1, "strUsername = rhys"
Print #1, ""
Print #1, ""
Close #1
End Sub

But my problem lies in the reading of the value of strUsername ... HOW!?

All i want to do is return the value of rhys (and any other value i assign to other strings, variables) but i cant, i have read through the link that Vadivel gave to me ... but no success..

I have tried :

Code:

Dim TextLine$, Filename$
Dim FileHandle as Integer

Filename$ = "test.txt"

    ' Test if the file exists
If Dir(Filename$) = "" Then Exit Sub

FileHandle = FreeFile ' This is safer than assigning a number

Open Filename$ For Input As #FileHandle

Do While Not EOF(FileHandle)        ' Loop until end of file
Line Input #FileHandle, TextLine$ ' Read line into variable
    ' Your code here
Loop

Close #FileHandle

but i just dont understand how i do it.

I have also tried :

Code:

Private Sub Command2_Click()
Open App.Path & "\Test.txt" For Input As #1
Input #1, Val(strUsername)
Close #1
End Sub

But returns a variable error or something along those lines

Regards
Rhys

SerranoG January 6th, 2005 03:49 PM

First, include

Dim strUserName as String at the top.

Then after "Your Code Here" enter

   Print #1, "strUsername = rhys"

strUsername = Mid(TextLine$, Instr(TextLine$,"=") + 2)

This will isolate the name rhys from the string.


Greg Serrano
Michigan Dept. of Environmental Quality, Air Quality Division

SerranoG January 6th, 2005 03:51 PM

Oops... take out the

   Print #1, "strUsername = rhys"

from my code. I pasted it there by accident. All you need is

strUsername = Mid(TextLine$, Instr(TextLine$,"=") + 2)




Greg Serrano
Michigan Dept. of Environmental Quality, Air Quality Division

rhysduk January 6th, 2005 03:58 PM

Right, ive put this in the code, run the application, it writes to the file, appears to opena nd read it..

But how do i know say obtain the value of strUsername in the BAT file to display to the user (im gonna set it as a labels caption)


Also why do i need to put Dim strUserName As String in there as this is not supposed to be a string in the actual application, its a thing (dont know what to call it in the bat file of which its value i need to get)
Thanks
Rhys


All times are GMT -4. The time now is 05:27 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.