0

I'm trying to run the following bit of code in a vb6 dll:

Dim objWSShell As Object
Set objWSShell = CreateObject("Wscript.Shell")
objWSShell.Run strPath & "test.bat", 0, True

The dll process gets hung up. The batch file will not run, no matter what its contents. I even tried an empty batch file and it still hung up. However, if I try this same piece of code, with this change:

Dim objWSShell As Object
Set objWSShell = CreateObject("Wscript.Shell")
objWSShell.Run "calc", 0, True

It works fine. I can't figure out why exe files work and bat files don't. Any ideas?

Joe M
  • 3,060
  • 3
  • 40
  • 63

3 Answers3

3

You don't need to use the shell scripting stuff, you can make things simpler & use the built in Shell() function:

shell environ$("COMSPEC") & " /C c:\xxx\yyy.bat", vbNormalFocus 

Ditto for:

shell "calc", vbNormalFocus 
Alex K.
  • 171,639
  • 30
  • 264
  • 288
  • I agree with this, and it can be enhanced using a "Shell and wait" technique if required. Google should turn up some examples readily. – Bob77 Aug 25 '10 at 17:48
2

You need to run cmd.exe and pass your BAT file to it.

objWSShell.Run "%COMSPEC% /c " & strPath & "test.bat", 0, True
Bob77
  • 13,167
  • 1
  • 29
  • 37
1

I had a similar issue where batch files couldn't be run directly from WScript.Shell, but I didn't have access to modify the VBScript. It turns out there was a registry override on the .bat extension.

While using COMSPEC worked for me, deleting the registry key actually fixed more than just the WScript problem.

TAbdiukov
  • 1,185
  • 3
  • 12
  • 25
cod3monk3y
  • 9,508
  • 6
  • 39
  • 54