Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Execute a command line exe file and get result 1

Status
Not open for further replies.

djburnheim

Technical User
Jan 22, 2003
71
0
0
AU
I'm fairly new to VB and don't know if this can be done. We have a command line exe file that generates passwords for us and I'm trying to write a VB app so that we can run it without starting a DOS session. I've created the forms in VB with a text box where the user would put in values that need to be passed to the DOS command which then generates a result that I want to be displayed in the form.

-VB form with txtUserEntry field.
-Click command button which calls an exe file that can only run in DOS.
Syntax needs to be c:\>exefile txtUserEntry
-Result is displayed in VB form

Hope it makes sense and I realise that it would probably be quite easy for an experience VB person to write a password generator but I certainly couldn't at this stage and this generator has already been used to generate a couple of hundred passwords. (passwords are not random...eg if txtUserEntry was the same everytime the result would be the same)

Thanks
Dave
 
Hmm... You could try something like:

Code:
dim strTemp as String
dim intFile as integer
strTemp = "passgen.exe " + chr(34) + " > c:\pw.txt"
call shell(strtemp,0)
intFile = FreeFile()
Open "c:\pw.txt" For Input as #intFile
Line Input #intFile,strTemp
close #intFile
Kill "c:\pw.txt"

Rem StrTemp now holds value

This approach might work, but it's a bit cheap and cheerful...

mmilan
 
Slight amendment.

dim strTemp as String
dim intFile as integer
strTemp = "passgen.exe > c:\pw.txt"
call shell(strtemp,0)
intFile = FreeFile()
Open "c:\pw.txt" For Input as #intFile
Line Input #intFile,strTemp
close #intFile
Kill "c:\pw.txt"

Rem StrTemp now holds value

 
Thanks mmilan...I think I understand what most of your code is about and I've tried to use it but get an error "Input Past End of File" when I run it. Also I had to create the pw.txt file first, should OPEN create it automatically? I'm not sure what the FreeFile bit is about...I stepped through the code and found that it gives a value of 1 but what does it mean? Also I think I need to get some info from the form in the strTemp. This is what I have


Private Sub cmdGenerate_Click()

Dim strTemp As String
Dim intFile As Integer

If frmMain.txtMachineName = "" Then
MsgBox "Please Enter Machine Name First!", vbExclamation, "No Machine Name"
frmMain.txtMachineName.SetFocus
GoTo Error:
End If

strTemp = "d:\Pgen\generator.exe" & Chr(32) & frmMain.txtMachineName & "> d:\Pgen\pw.txt"
Call Shell(strTemp, 0)
intFile = FreeFile()
Open "d:\Pgen\pw.txt" For Input As #intFile
Line Input #intFile, strTemp
Close #intFile
Kill "d:\Pgen\pw.txt"

frmMain.cmdAnother.Visible = True
frmMain.cmdAnother.Enabled = True
frmMain.cmdGenerate.Visible = False
frmMain.cmdGenerate.Enabled = False
frmMain.lblAdmin.Visible = True
frmMain.lblPassword.Caption = strTemp
frmMain.lblPassword.Visible = True
frmMain.lblRemember.Visible = True
frmMain.lblExample.Visible = True
frmMain.cmdClose.SetFocus

Error:
End Sub
 
Sorry my mistake....code does work I just needed to remove the .exe extension in the string that is passed to the shell. Still have a problem if the txt file doesn't exsist and becuase it is killed each time (which is preferable) I can only use it once...any ideas??
 
After more testing...

It actually isn't working to well. Originally I was testing the string that was being passed to DOS in DOS and it works like a charm..if pw.txt doesn't exsist it creates it, if it does it overwrites it. But when I run it through VB if the file doesn't exsist it is not created and if it does exsist there must be something in it otherwise I get "Input Past End of File". Even then the file is not updated with the new strTemp...I don't get any errors, it just always displays the contents of the txt file.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top