|
I want to run a .bat file from VB, any ideas ?
|
|
|
I was wondering just that...I have looked for a long time for it and found nothing.
|
|
|
There's probably a better way to do what you're trying to do, but this will probably work:
dim shell as new wscript.shell
shell.run "batchfile.bat"
|
|
|
Well, I tried to use that code and it alone is not enough to open a batch file. Maybe you can explain more.
Thanks!
|
|
|
That's all I've got.
If you're using vbscript, you can do this:
dim shell
set shell=createobject("wscript.shell")
shell.run "batchfile.bat"
set shell=nothing
so you should be able to call the same automation object in VB.
for VB.NET this will work:
using System;
namespace Learn
{
class cmdShell
{
[STAThread]
static void Main(string[] args)
{
Declare New Process
System.Diagnostics.Process proc;
Run test.bat from command line.
proc = System.Diagnostics.Process.Start("C:\\test.bat");
Waits for the process to end.
proc.WaitForExit();
}
}
}
in VB you might be able to import the shell exec api:
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
you can then use shellexecute to run "mybat.bat"
see this page for more info:
http://custom.programming-in.net/articles/art9-1.asp?f=ShellExecute
|
|
|
In Javascript on WINDOWS you can use this to open aplications:
<object ID="AppCtl" WIDTH="0" HEIGHT="0"
CLASSID="CLSID:07340190-BCE0-11D1-BD7E-00207812DE95">
</object>
<a href="javascript:AppCtl.RunApplication('C:\\program%20files\\internet%20explorer\\iexplore.exe');">Internet Explorer</a>
<! use "\\" as a "\" in the address ==========>
|
|
|
The question was how to run a bat file from VB- that's why I included VB/VBS examples, and not j/html examples...
|
|
|
Would you also please add the lines of code how we can pass some arguments along with the batch file on the command line to execute
something like "\\Server\Job.bat" & "Successfully Executed"
|
|
|
If you want a message box to display if the execution of the batch file suceeded then you need to do a couple things.
The easiest way I believe is add a net send or echo command to the end of the .bat you are running.
http://www.robvanderwoude.com/usermessages.html
The above link will show you how to add message box type commands to the batch script.
If you use the net send command, make sure the messenger service is enabled and started else it will fail.
Good luck,
Smudge
|
|
|
This works with VB6:
Dim RetVal
RetVal = Shell("(path)\(file).bat", vbWindowState)
However, any code after the shell function call might run even if the batch file hasn't finished its task.
To get around this, you could do several different things.
You could put code in your batch file to create a "/forum/done.txt" file in some designated folder, and in the code following the shell call, enable a timer control. Within the timer event, put code to check for the existence of the "/forum/done.txt" file, and then delete the "/forum/done.txt" file from VB and resume normal execution of the VB code (I like using the scripting.filesystemobject to do this-- you need to have "Microsoft Scripting Runtime" in your VB project references).
|
|
|
To make the batch file run in a synchronous manner (i.e.-- VB6 code doesn't continue executing until the batch file is finished running), there's a much simpler method than I mentioned previously.
In project references, make sure "Windows Script Host Object Model" is selected, and use the following 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.
|
|
|
Whenever we are executing vbscripts using Altiris, it is a best practice to return Exit Codes/Return Codes to the Altiris. Following is the template that not only return an Exit code but also writes to the Event Log consolidate debt.
Note: Place the executable and script in one place
'--------------------------------------------------
'GET CURRENT FOLDER PATH
path = FileSystemObject.GetParentFolderName(WScript.ScriptFullName)
If Right(path, 1) <> "\" Then path = path & "\"
End If
errTrap = shell.Run(path, 1 ,True)
If (errTrap = 0) Or (errTrap = 3010) Then
'WRITE EXIT CODE [0-success/3010-success&requires reboot] TO EVENTLOG
shell.LogEvent vbLogSuccess, sLogHeader & "Installation completed successfully." & VbCrLf & "Exit code: " & errTrap
WScript.Quit (errTrap) payday loans
Else
'WRITE EXIT CODE TO EVENTLOG
shell.LogEvent vbLogError, sLogHeader & "Installation failed." & VbCrLf & "Exit code: " & errTrap
WScript.Quit (errTrap)
End If
Set FileSystemObject = Nothing
Set Shell = Nothing
'--------------------------------------------------
personal loans
One needs to define sLogHeader variable so that the hedder of the event log will written with this name, befor the following line in the above script
(shell.LogEvent vbLogSuccess, sLogHeader & "Installation completed successfully." & VbCrLf & "Exit code: " & errTrap).
Example
sLogHeader = "Install batch file"
Preferably sLogHeader name should be task name that you are running so that it will be essy identify in event log foreclosures.
I hope this may help.
Thanks
|
|
|