Running a batch file from VB with shell

Status
Not open for further replies.

QuaziBee

Daemon Poster
Messages
661
Location
Calgary, AB
Trying to use:
Shell("command.com /c G:\Whyland\PullFromBack1.bat")

It just flashes the cmd window for a second and does not run. Googling this, people just say to use Shell("[batch path and name]") but most of those posts are from like 2000 and it doesn't work.

What do?
 
what happens when you remove command.com /c ? just tell the shell to run the batch file

im also not sure why you tried command.com instead of cmd.exe
 
I have tried
Shell("G:\Whyland\PullFromBack1.bat")
Shell("cmd /c G:\Whyland\PullFromBack1.bat")
Shell("C:\Windows\system32\cmd.exe G:\Whyland\PullFromBack1.bat")
Shell("C:\Windows\system32\cmd.exe /c G:\Whyland\PullFromBack1.bat")
Shell("cmd /c" & " G:\Whyland\PullFromBack1.bat")


command.com was just the last in that line that I tried.
 
copy paste?

Run a .bat file from VB

Code:
Dim wshThisShell As WshShell
Dim lngRet as Long
Dim strShellCommand As String
Dim strBatchPath as String

Set wshThisShell = New WshShell
strBatchPath = "C:\Program Files\Batch Path\Batch.bat"
'the path for the batch file you're using 
strShellCommand = """" & strBatchPath & """"
'the ridiculous number of quotation marks is necessary
'when there is a space in one or more of the folder names
lngRet = wshThisShell.Run(strShellCommand, vbNormalFocus, vbTrue)
'set 3rd argument above to vbFalse for asynchronous
'execution of the batch file.



edit - from ms forums, more specific to your exact problem

http://social.msdn.microsoft.com/forums/en-us/vblanguage/thread/2423A712-AD05-4A52-9777-2D1A4E35F127

Code:
Dim p As New Process
        p.StartInfo.FileName = "test.bat"
        p.StartInfo.WorkingDirectory = "c:\"
        p.Start()
 
Shell probably doesn't hold the command window open so what you're seeing is the batch file running and then closing down straight away. To see whether this is the case (since you almost certainly won't be able to debug into the batch?) make the batch write some output (maybe the time) into a file. If the file is present and has the expected time after running your application then you know that the batch is being called.
 
I put a Pause at the end of the batch file and it stayed open after launching it from within the VB script. Turns out the command I was calling in the script was not registered with windows. Dropped the .exe into system32 and it now works like a charm.

Final code looks like

Shell("cmd /c G:\Whyland\PullFromBack1.bat", AppWinStyle.NormalFocus)


------

New question.
I'm trying to determine if a file exists on remote computers. I decided to use:

If Dir("\\OBS" & TextBox1.Text & "T1\C$\SC\File.ini") <> "" Then
MsgBox("Totally there!")
Else
MsgBox("More work for me!")
End If

The naming convention of the computers looks like OBSXXXXT1 so I just put in the XXXX number for each computer.
This works fine on any computer that I either map a network drive to or am currently browsing because it requires credentials.

My question is where and how to amend this command to include the admin username and pw?
 
Status
Not open for further replies.
Back
Top Bottom