Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > VB How-To
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old January 5th, 2005, 06:46 PM
Authorized User
 
Join Date: Sep 2004
Location: , , .
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

Reply With Quote
  #2 (permalink)  
Old January 6th, 2005, 12:05 AM
Friend of Wrox
 
Join Date: Dec 2004
Location: Chennai, Tamil nadu, India.
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
Reply With Quote
  #3 (permalink)  
Old January 6th, 2005, 10:21 AM
Authorized User
 
Join Date: Sep 2004
Location: , , .
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

Reply With Quote
  #4 (permalink)  
Old January 6th, 2005, 10:27 AM
Friend of Wrox
Points: 4,007, Level: 26
Points: 4,007, Level: 26 Points: 4,007, Level: 26 Points: 4,007, Level: 26
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Lansing, Michigan, USA.
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
Reply With Quote
  #5 (permalink)  
Old January 6th, 2005, 10:29 AM
Authorized User
 
Join Date: Sep 2004
Location: , , .
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

Reply With Quote
  #6 (permalink)  
Old January 6th, 2005, 01:13 PM
Friend of Wrox
 
Join Date: Dec 2004
Location: Chennai, Tamil nadu, India.
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
Reply With Quote
  #7 (permalink)  
Old January 6th, 2005, 03:37 PM
Authorized User
 
Join Date: Sep 2004
Location: , , .
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
Reply With Quote
  #8 (permalink)  
Old January 6th, 2005, 03:49 PM
Friend of Wrox
Points: 4,007, Level: 26
Points: 4,007, Level: 26 Points: 4,007, Level: 26 Points: 4,007, Level: 26
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Lansing, Michigan, USA.
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
Reply With Quote
  #9 (permalink)  
Old January 6th, 2005, 03:51 PM
Friend of Wrox
Points: 4,007, Level: 26
Points: 4,007, Level: 26 Points: 4,007, Level: 26 Points: 4,007, Level: 26
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Lansing, Michigan, USA.
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
Reply With Quote
  #10 (permalink)  
Old January 6th, 2005, 03:58 PM
Authorized User
 
Join Date: Sep 2004
Location: , , .
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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 09:38 AM.


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