Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > VB How-To
|
VB How-To Ask your "How do I do this with VB?" questions in this forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 5th, 2005, 06:46 PM
Authorized User
 
Join Date: Sep 2004
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to rhysduk Send a message via Yahoo to rhysduk
Default 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

 
Old January 6th, 2005, 12:05 AM
Friend of Wrox
 
Join Date: Dec 2004
Posts: 307
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Vadivel Send a message via Yahoo to Vadivel
Default

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
 
Old January 6th, 2005, 10:21 AM
Authorized User
 
Join Date: Sep 2004
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to rhysduk Send a message via Yahoo to rhysduk
Default

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

 
Old January 6th, 2005, 10:27 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,151
Thanks: 2
Thanked 14 Times in 14 Posts
Send a message via ICQ to SerranoG Send a message via AIM to SerranoG
Default

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
 
Old January 6th, 2005, 10:29 AM
Authorized User
 
Join Date: Sep 2004
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to rhysduk Send a message via Yahoo to rhysduk
Default

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

Rhys

 
Old January 6th, 2005, 01:13 PM
Friend of Wrox
 
Join Date: Dec 2004
Posts: 307
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Vadivel Send a message via Yahoo to Vadivel
Default

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
 
Old January 6th, 2005, 03:37 PM
Authorized User
 
Join Date: Sep 2004
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to rhysduk Send a message via Yahoo to rhysduk
Default

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
 
Old January 6th, 2005, 03:49 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,151
Thanks: 2
Thanked 14 Times in 14 Posts
Send a message via ICQ to SerranoG Send a message via AIM to SerranoG
Default

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
 
Old January 6th, 2005, 03:51 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,151
Thanks: 2
Thanked 14 Times in 14 Posts
Send a message via ICQ to SerranoG Send a message via AIM to SerranoG
Default

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
 
Old January 6th, 2005, 03:58 PM
Authorized User
 
Join Date: Sep 2004
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to rhysduk Send a message via Yahoo to rhysduk
Default

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





Similar Threads
Thread Thread Starter Forum Replies Last Post
confused- what to use .bat or .jar file - plz he ruchisingh Java Basics 1 May 18th, 2006 12:08 PM
bat file doesn't work maroun BOOK: Beginning Cryptography with Java 1 October 25th, 2005 04:59 PM
Run .bat file from Excel VBA Harold Hawken Excel VBA 2 June 23rd, 2005 03:03 PM
Problem with reading BAT file rhysduk VB How-To 0 January 12th, 2005 09:53 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.