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

SHELL doesn't run batch file from VB

Status
Not open for further replies.

mjjks

Programmer
Jun 22, 2005
138
US
I searched board and Shell is covered extensively, however it didn't work for me, so I post in a hope that someone will shed a light on my problem.

I have a batch file on a app server that runs some commands. It works fine if I run it directly.

When I run it like this in VB: result = Shell(myFile), it returns process id, but code within batch file is not executed. VB dll I'm working on is currently on my PC but will reside on IIS server.

I'm using UNC path and thought that was a problem, mapped drive and used that but code in batch file is still is not executed.

I also tried to use ShellExecute API. That didn't work either.

Another thing I tried from the sample on this board is to use WSript but it didn't work too.

Dim oWsh As Variant
Set oWsh = CreateObject("WScript.Shell")
oWsh.CurrentDirectory = "\\MyServer\FolderName\"
oWsh.Run "My_Batch_File.cmd", 0, True

At this point I'm ready to pull my hair out. Simple operation but just won't work.
Any ideas on how to force code in batch file to execute?

Thanks much

 
If you place the bat file on the server and double click it, does it execute properly?
 
Yep, batch file is on the server and when I execute it by double-clicking, then code inside of batch file gets executed properly.
 
Here's a cut and paste of what I use

Code:
Option Explicit
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Private cmdRunBatch_Click()

strPath = App.Path
If Right(App.Path, 1) <> "\" Then strPath = strPath & "\"
'bat file should be on the same machine as this exe
strShell = strPath & "RunMASJob.bat"
Success = ShellAndWait(strShell, vbNormalFocus)

If Success Then
'''Do your stuff
Else
'''Handle Failure
End If

End Sub

Private Function ShellAndWait(ByVal program_name As String, ByVal window_style As VbAppWinStyle) As Boolean

Dim process_id As Long
Dim process_handle As Long
' Start the program.
On Error GoTo ShellError
process_id = Shell(program_name, window_style)
On Error GoTo 0

' Hide.
Me.Visible = False
DoEvents

' Wait for the program to finish.
' Get the process handle.
process_handle = OpenProcess(SYNCHRONIZE, 0, process_id)
If process_handle <> 0 Then
WaitForSingleObject process_handle, INFINITE
CloseHandle process_handle
End If

'Reappear.
Me.Visible = True
ShellAndWait = True
Exit Function

ShellError:
MsgBox "Error starting MAS task" & vbCrLf & _
              Err.Description, vbOKOnly Or vbExclamation, _
              "Error"
ShellAndWait = False

End Function
This works fine for me, I did have to set security up on the folders/programs my batchfile was accessing which was on another machine. Good Luck
 
Thanks, but I couldn't test it as SYNCHRONIZE and INFINITE declarationa are missing.

And then you have a comment saying that batch file should be on the same machine as executable. That won't work for me as I have an ASP page and DLL on IIS server, and batch file is on another App server.
 
Executing batch files from other machines works fine for me.
While I don't think this will make a difference since your file associations seem to be fine.

Maybe try -
result = Shell("cmd.exe /k c:\MyFile.bat")



Tom
 
sysinternals (and wininternals) have a great set of tools and utilities. And I'm pleased to see that, contrary to original fears, in the wake of the acquisition by Microsoft the number one priority is to keep the tools freely available
 
Code:
 Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _
    ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
  Private Const SW_HIDE As Long = 0
  Private Const SW_SHOWNORMAL As Long = 1


ShellExecute 0&, "OPEN", "M:\RCVR\SERVER4\UPLOAD.BAT", vbNullString, "M:\RCVR\SERVER4", SW_SHOWNORMAL

Here's how I've done it.

I tried to have patience but it took to long! :) -DW
 
I didn't know that they had been picked up by Microsoft, and I'm also glad to see that they've gone that route. We shall see if Microsoft starts adding "improvements" based on, say, the CLR, and "deprecating" the existing utilities.
 
Thank you guys for sharing your ideas.
I found what's wrong however don't know how to fix it yet.
It seems that CMD/Shell can't resolve path issue. I put "sleep 300" after my commad lines in a batch file and cmd window opens and sits there, so I see an error.

I use this:
Code:
ShellExecute 0&, "OPEN", "\\MyRemoteServer\TestFolder\BatchFile.cmd", vbNullString, "\\MyRemoteServer\TestFolder\", SW_SHOWNORMAL

This is what I get in cmd window:
Code:
'\\MyRemoteServer\TestFolder\
CMD.EXE was started with the above path as the current directory.
UNC paths are not supported.  Defaulting to Windows directory.

C:\WINDOWS>[highlight]G:\scan\srv\essclient\essclient -V -R 285006 -S MyRemoteServer -U scan
sys -P y6Tr$23 -F G:\scan\srv\essclient\285006.ttx -D G:\scan\srv\essclient\log[/highlight]

The system cannot find the path specified.

C:\WINDOWS>sleep 300

Highlighted is an entry in the batch file which points to a local drive.

Any idea how to change CMD directory so it thinks it executes on remote server and not my local "C:\Windows"?

I aslo tried mapped drive and this is an error in the cmd window:
Code:
X:\>[highlight]G:\scan\srv\essclient\essclient -V -R 285006 -S MyRemoteServer -U scan
sys -P y6Tr$23 -F G:\scan\srv\essclient\285006.ttx -D G:\scan\srv\essclient\log[/highlight]

The system cannot find the path specified.

X:\>sleep 300

Thanks

 
This may or may not help. In the batch file try this to set the mapped drive path.

Code:
cd\
cd driveletter\folder


I tried to have patience but it took to long! :) -DW
 
Well, I did spend a good deal of time doing something similar, being unable to get it to work, and using the solution I posted here. Feel free to peruse thread222-1220505. Note that jadams' solution has a mapped drive. If you can map a drive, then well and good, but I was not in a position to do that, and I found that the syntax that mijks uses didn't work for me either.

However, psexec definitely worked for me to do exactly what you're trying to do, so if you're in a hurry, you might want to check it out.

Bob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top