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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What path is used when Shell is called 2

Status
Not open for further replies.

djburnheim

Technical User
Jan 22, 2003
71
AU
I've been working on a small app that calls a command line exe file (see thread222-681160) and I almost have it working now but am running into two problems. Firstly the command line exe file needs to be run from the same location as a DAT file and I've tried copying this DAT file to all the locations I can think of (windows, system32, system, root) but it doesn't seem to work so I'm not sure what path is used when Shell is called? The other problem I have is with the txt output file, from what I understand OPEN should create a file if it doesn't exsist but it doesn't so I have to leave it when I'd prefer to delete it. Running the below command from a DOS prompt works everytime with no problems (assuming the DAT file exsists in the path I'm in) and creates or overwrites the txt file as is it should but I just can't get it working from VB...any suggestions???

C:\SomeDOSPath\>d:\pgen\generator txtmachinename > d:\pgen\pw.txt

[red]***code***[/red]
[blue]
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 NoMachineName:
End If

strTemp = "D:\PGen\Generator" & 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
[green] 'Kill "D:\PGen\pw.txt"[/green]

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

NoMachineName:
End Sub[/blue]
 
I have it working!! I changed the way shell was called using a way I found in another post and also added a pause (Don suggested this sometime ago)...it seems to of fixed all my problems! Thanks everybody for their help..working code is below. Now to work out how to package it!

[wavey3]

[red]***code***[/red][blue]
Private Sub cmdGenerate_Click()

Dim strTemp As String
Dim intFile As Integer
Dim lngRtn As Long

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

strTemp = "D:\PGen\Generator" & Chr(32) & frmMain.txtMachineName & Chr(32) & ">D:\PGen\pw.txt"
ChDir App.Path
Shell "cmd /C D:\PGen\Generator" & Chr(32) & frmMain.txtMachineName & Chr(32) & ">D:\PGen\pw.txt", vbHide

'Wait
Dim x
For x = 1 To 10000
DoEvents
Next x

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

NoMachineName:
End Sub
 
Dave

Your email server is stripping out the exe file! Can you change its extension and try again?


Andy
"Logic is invincible because in order to combat logic it is necessary to use logic." -- Pierre Boutroux
"Why does my program keep showing error messages every time something goes wrong?"
 
Dave

You've had more luck than me. I couldn't get the damn thing to work either. Glad to see you've finally got it going though. I shall now go and eat my dinner.

Andy
"Logic is invincible because in order to combat logic it is necessary to use logic." -- Pierre Boutroux
"Why does my program keep showing error messages every time something goes wrong?"
 
Dave

I can get it to work time after time in the IDE (at long £$%^ last!):
Code:
  Const cstrF As String = "C:\My Documents\TT"
  Const cstrN As String = "Test.txt"
  Dim dblR As Double
  Dim strX As String
  Dim intS As Integer
  
  ChDir cstrF
  dblR = Shell("C:\COMMAND.COM /C Generator.exe ab12345 > " & cstrN, vbMinimizedNoFocus)
  DoEvents
  intS = FreeFile
  Open cstrF & "\" & cstrN For Input As #intS
  Line Input #intS, strX
  Close intS
  MsgBox strX

Obviously you'll want to swap "CMD.EXE" for "COMMAND.COM", but the DoEvents seems to do the trick. Hope it works for you!

Andy
"Logic is invincible because in order to combat logic it is necessary to use logic." -- Pierre Boutroux
"Why does my program keep showing error messages every time something goes wrong?"
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top