Results 1 to 9 of 9

Thread: How to run a .bat file with VBScript ?

  1. #1
    Join Date
    Feb 2006
    Posts
    185

    How to run a .bat file with VBScript ?

    I want to run a .bat file from VB, can anyone help me out? I have looked for a long time for it and found nothing. Thanks in advance

  2. #2
    Join Date
    Dec 2007
    Posts
    1,599
    All I do in my VB program is this to run a batch file, might work or not:
    Code:
     'send e-mail
        Shell strEmailPath & strEmailBAT
    Below are lines from the ini file that I use during initialization to set strEmailPath & strEmailBAT.
    ########## email batch file location
    EMAILPATH=C:\jazz\copydata\email\
    EMAILBAT=emailerr.bat

    Hope this helps

  3. #3
    Join Date
    Dec 2007
    Posts
    1,736
    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/art...f=ShellExecute

  4. #4
    Join Date
    Jun 2006
    Posts
    623
    Code:
    <%@ LANGUAGE="VBSCRIPT" %>
    <% 
    Set WSHShell = CreateObject("Wscript.Shell")
    WSHShell.Run ("c:\myBatfile.bat") 
    %>
    
    <html>
    <title>Sample</title>
    <body>
    Blah Blah
    </body>
    </html>
    I hope you find this post helpful.

    Regards

  5. #5
    Join Date
    Dec 2007
    Posts
    1,547
    Just run it like any other executable.

    Sample:

    ----------------
    echo this is a batch file
    c:
    cd\temp
    something.vbs
    REM note the .vbs above is optional
    Echo Batch file is over
    -----------------

  6. #6
    Join Date
    Nov 2010
    Posts
    3

    Re: How to run a .bat file with VBScript ?

    dim shell
    set shell=createobject("wscript.shell")
    shell.run "batchfile.bat"
    set shell=nothing

    Results in Cannot Find File...

    (I've checked the path)

  7. #7
    Join Date
    Nov 2010
    Posts
    3

    Re: How to run a .bat file with VBScript ?

    This is my code...... (trying to send an "Enter" key to another application .... but not successful.. could anybody pls help?)


    Set Shell = CreateObject("WScript.Shell")


    Shell.run "C:\Documents and Settings\Tom\Desktop\Auto AntiMalwarebytes.bat"

    set shell = nothing

    msgbox(Activewindow.name)
    'this is the bit I need help as well ( how do I get it to recognize a particular window by its name .... also not sure if that would be the real name of the window as its an external application ... hence going by active window..)

    Wscript.Sleep 10000

    ws.SendKeys "{ENTER}"
    WScript.Quit

  8. #8
    Join Date
    Jan 2010
    Posts
    2

    Re: How to run a .bat file with VBScript ?

    You have a space in your filename for the batch file. A space cannot be part of the filename.

  9. #9
    Join Date
    Dec 2007
    Posts
    2,291

    Re: How to run a .bat file with VBScript ?

    Quote Originally Posted by Boomer View Post
    You have a space in your filename for the batch file. A space cannot be part of the filename.
    Whom are you addressing to the querie that you have asked? You should always quote the message above for the person to let him know you want to either querie or answer him. In any ways, if you find any problem in the code then can you perhaps post the proper code that you are referring to?

Similar Threads

  1. Replies: 4
    Last Post: 21-03-2012, 05:01 AM
  2. VBscript to run batch file carry on running
    By hokaday in forum Software Development
    Replies: 1
    Last Post: 12-02-2010, 01:34 AM
  3. Vbscript to find latest file
    By BoanHed in forum Software Development
    Replies: 3
    Last Post: 23-11-2009, 08:38 AM
  4. What happens when you drag and drop a file on top of a vbscript
    By Dharmavira in forum Software Development
    Replies: 2
    Last Post: 01-06-2009, 10:00 PM
  5. How can i drag and drop another file on top of the VBScript file
    By Dharmesh Arora in forum Software Development
    Replies: 2
    Last Post: 01-06-2009, 09:53 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,703,940.21174 seconds with 17 queries