Results 1 to 18 of 18

Thread: launch batch file

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    182

    launch batch file

    how would i launch a batch file from within visual basic?
    the batch file is called kill.bat i have used the following code

    Code:
     shell "/kill.bat"
    but does not work. the bat file is stored in the root where the application would be launched from.

  2. #2
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: launch batch file

    Code:
    Dim x As String
    
    x = App.Path 
    
    If Right$(x, 1) <> "\" Then x = x & "\"
    
    x = x & "kill.bat"
    
    Shell x, vbNormalFocus
    Not tested - from the top of my head

  3. #3
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 2006
    Location
    *Stack Trace*
    Posts
    1,511

    Re: launch batch file

    Quote Originally Posted by gavio
    Code:
    Dim x As String
    
    x = App.Path 
    
    If Right$(x, 1) <> "\" Then x = x & "\"
    
    x = x & "kill.bat"
    
    Shell x, vbNormalFocus
    Not tested - from the top of my head
    Wouldn't the following work fine?
    Code:
    Shell App.Path & "\kill.bat"
    I don't disagree with your method, because it should work fine, but is all the code around necessary?
    I mean I've never used it that way
    Delete it. They just clutter threads anyway.

  4. #4
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: launch batch file

    @TheBigB: what if EXE is in drive root? "c:\"? Then you have:
    Code:
    Shell "c:\\kill.bat"
    ... which of course, fails. You must think ahead

    My code is still not good enough. He should also check if "kill.bat" actually exists

  5. #5
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 2006
    Location
    *Stack Trace*
    Posts
    1,511

    Re: launch batch file

    Quote Originally Posted by gavio
    @TheBigB: what if EXE is in drive root? "c:\"? Then you have:
    Code:
    Shell "c:\\kill.bat"
    ... which of course, fails. You must think ahead

    My code is still not good enough. He should also check if "kill.bat" actually exists
    Never thought of that
    Thanks for pointing that out.
    Delete it. They just clutter threads anyway.

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: launch batch file

    Quote Originally Posted by gavio
    @TheBigB: what if EXE is in drive root? "c:\"? Then you have:
    Code:
    Shell "c:\\kill.bat"
    ... which of course, fails. You must think ahead

    My code is still not good enough. He should also check if "kill.bat" actually exists
    an alternative to that problem would be to use ShellExecute and specify a working directory of the App.Path it wont matter the root path or not (note the double backslash just as a working example).
    Code:
    Option Explicit
    
    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
    Private Const SW_SHOWMAXIMIZED As Long = 3
    Private Const SW_SHOWMINIMIZED As Long = 2
    
    Private Sub Command1_Click()
        ShellExecute Me.hwnd, "open", "\deleteme.txt", vbNullString, "C:\", SW_SHOWNORMAL
    End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7
    Hyperactive Member
    Join Date
    Jan 2006
    Location
    Pakistan
    Posts
    388

    Re: launch batch file

    I would rather recommend this code. Its much more simple and will do all that is required. And without APIs too.

    Code:
    Dim path As String
    path = App.Path
    If Right(path,1) <> "\" Then path = path & "\"
    path = path & "kill.bat"
    If LCase(Dir(path)) <> "kill.bat" Then
       'file does not exist
       MsgBox "The required file does not exist"
    Else
       'file exists
       Shell path
    End If

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: launch batch file

    No need to shy away from APIs as they are on your system from the OS anyways. Its just up to you to decide to call it or not and yes your code is good too. .

    This API is just as good too as it adds the backslash if needed.
    PathAddBackslash
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  9. #9
    Hyperactive Member
    Join Date
    Jan 2006
    Location
    Pakistan
    Posts
    388

    Re: launch batch file

    You flatter me saying my code is good, LOL.
    I don't shy from APIs, its just that they always tend to loosen the programmer's grip over the way things work. They also add the factor of uncertainty to the program and the chances of getting a program crash rise (well for me at least, maybe others don't agree with me). Thats why I always try to do without them.

  10. #10
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: launch batch file

    There is less chance of them failing but if they did it would be because of ones coded usage of them but with the non-api approach you are taking more chances and uncertanity as your code didnt go through the rigors of unit testing that MS put the APIs through.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  11. #11
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: launch batch file

    I'm not bringing in anything really new with the following code, just bringing some stuff to think about:
    Code:
    Dim strPath As String
    
    ' use string and byte version of Right
    If RightB$(App.Path, 2) <> "\" Then
        strPath = App.Path & "\kill.bat"
    Else
        strPath = App.Path & "kill.bat"
    End If
    
    ' another point: because we are finding out where a file (or folder) exists
    ' and Dir returns a null string if it was not found, we only need to check
    ' length of the returned string
    If LenB(Dir$(strPath)) Then
        Shell strPath
    Else
        MsgBox "The file """ & strPath & """ was not found!"
    End If
    As for ShellExecute, you don't need to do checking beforehand, you can just go ahead and make the function call. However instead you can get an error message out of it as a return value: http://msdn.microsoft.com/en-us/library/bb762153.aspx - although it is of the weirder ones, the return value is an error if it is from 0 to 32, otherwise the return value means success. That is even weirder than most other API functions, where 0 can mean success and any other value is an error; but there are also API functions where 0 means error and you have to use GetLastError API to get the error number, because any other value is a wanted result. But there are also APIs where -1 (or &HFFFFFFFF) is the failing condition. And then we have these functions like ShellExecute that are truly unique with their return values...
    Last edited by Merri; Aug 2nd, 2008 at 10:30 PM.

  12. #12
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: launch batch file

    My full example I usually post but I usually I try to keep it simple
    Code:
    Option Explicit
    
    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_SHOWNORMAL As Long = 1
    Private Const SW_SHOWMINIMIZED As Long = 2
    Private Const SW_SHOWMAXIMIZED As Long = 3
    
    Private Const ERROR_FILE_NOT_FOUND As Long = 2&
    Private Const ERROR_PATH_NOT_FOUND As Long = 3&
    Private Const ERROR_BAD_FORMAT As Long = 11&
    Private Const SE_ERR_ACCESSDENIED As Long = 5
    Private Const SE_ERR_ASSOCINCOMPLETE As Long = 27
    Private Const SE_ERR_DDEBUSY As Long = 30
    Private Const SE_ERR_DDEFAIL As Long = 29
    Private Const SE_ERR_DDETIMEOUT As Long = 28
    Private Const SE_ERR_DLLNOTFOUND As Long = 32
    Private Const SE_ERR_FNF As Long = 2
    Private Const SE_ERR_NOASSOC As Long = 31
    Private Const SE_ERR_OOM As Long = 8
    Private Const SE_ERR_PNF As Long = 3
    Private Const SE_ERR_SHARE As Long = 26
    
    Private Sub Command1_Click()
        On Error GoTo MyError
        Dim lRet As Long
        lRet = ShellExecute(Me.hwnd, "Open", "CMD.exe", " /C ipconfig.exe /all > C:\ip.txt", "C:\", SW_SHOWNORMAL)
        If lRet <= 32 Then
            Select Case lRet
                Case 0
                    MsgBox "The operating system is out of memory or resources."
                Case ERROR_FILE_NOT_FOUND
                    MsgBox "The specified file was not found."
                Case ERROR_PATH_NOT_FOUND
                    MsgBox "The specified path was not found."
                Case ERROR_BAD_FORMAT
                    MsgBox "The .EXE file is invalid (non-Win32 .EXE or error in .EXE image)."
                Case SE_ERR_ACCESSDENIED
                    MsgBox "The operating system denied access to the specified file."
                Case SE_ERR_ASSOCINCOMPLETE
                    MsgBox "The filename association is incomplete or invalid."
                Case SE_ERR_DDEBUSY
                    MsgBox "The DDE transaction could not be completed because other DDE transactions were being processed."
                Case SE_ERR_DDEFAIL
                    MsgBox "The DDE transaction failed."
                Case SE_ERR_DDETIMEOUT
                    MsgBox "The DDE transaction could not be completed because the request timed out."
                Case SE_ERR_DLLNOTFOUND
                    MsgBox "The specified dynamic-link library was not found."
                Case SE_ERR_FNF
                    MsgBox "The specified file was not found."
                Case SE_ERR_NOASSOC
                    MsgBox "There is no application associated with the given filename extension."
                Case SE_ERR_OOM
                    MsgBox "There was not enough memory to complete the operation."
                Case SE_ERR_PNF
                    MsgBox "The specified path was not found."
                Case SE_ERR_SHARE
                    MsgBox "A sharing violation occurred."
            End Select
        End If
        Exit Sub
    MyError:
        MsgBox Err.Number & " - " & Err.Description
    End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  13. #13
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 2006
    Location
    *Stack Trace*
    Posts
    1,511

    Re: launch batch file

    Nice
    That's some fool-proof code over there

    Quote Originally Posted by lone_REBEL
    You flatter me saying my code is good, LOL.
    I don't shy from APIs, its just that they always tend to loosen the programmer's grip over the way things work. They also add the factor of uncertainty to the program and the chances of getting a program crash rise (well for me at least, maybe others don't agree with me). Thats why I always try to do without them.
    You're right in a way, but you have to face that ShellExecute provides more functionality than Shell. In addition, API calls speed up your code, because it's a direct link from the code, rather than a redirection from the VB runtime.
    Delete it. They just clutter threads anyway.

  14. #14
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: launch batch file

    Perhaps ShellExecute is better way nowadays as some systems may block Shell command.

    This is a revived version of lone_REBEL's code in post#7, it also looks similar to Merri's code in post#11.
    Code:
    Sub KillIt()
       Dim sPath As String
       
       sPath = Replace(App.Path & "\kill.bat", "\\", "\") '-- cheating an If (I am lazy)  
       If Len(Dir(sPath)) Then '-- the file exists
          Shell sPath
       Else
          MsgBox "File Not Found: " & sPath
       End If
    End Sub
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  15. #15
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 2006
    Location
    *Stack Trace*
    Posts
    1,511

    Re: launch batch file

    Quote Originally Posted by anhn
    Perhaps ShellExecute is better way nowadays as some systems may block Shell command.

    This is a revived version of lone_REBEL's code in post#7, it also looks similar to Merri's code in post#11.
    Code:
    Sub KillIt()
       Dim sPath As String
       
       sPath = Replace(App.Path & "\kill.bat", "\\", "\") '-- cheating an If (I am lazy)  
       If Len(Dir(sPath)) Then '-- the file exists
          Shell sPath
       Else
          MsgBox "File Not Found: " & sPath
       End If
    End Sub
    What if the application is ran on a network location
    \\server\
    Delete it. They just clutter threads anyway.

  16. #16
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: launch batch file

    Quote Originally Posted by TheBigB
    What if the application is ran on a network location
    \\server\
    You know how to fix it.
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  17. #17
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: launch batch file

    You don't really need to clean up the \ character though:
    Code:
    Option Explicit
    
    Private Sub Form_Load()
        Dim strPath As String
        strPath = "C:\Windows\\Win.ini"
        If LenB(Dir$(strPath)) Then Debug.Print "File exists"
    End Sub
    You can even put \\\\\\\\\\\\\\\\\\\\\ in and it still says the file exists...
    Code:
    Option Explicit
    
    Private Declare Function ShellExecuteW Lib "shell32.dll" (ByVal hWnd As Long, ByVal lpOperation As Long, ByVal lpFile As Long, ByVal lpParameters As Long, ByVal lpDirectory As Long, ByVal nShowCmd As Long) As Long
    
    Private Sub Form_Load()
        Dim strPath As String
        strPath = "C:\\\\\\\\\\\\\\\\\\Windows\\\\\\\\\\\\\\\\Win.ini"
        If LenB(Dir$(strPath)) Then
            If ShellExecuteW(Me.hWnd, StrPtr("Open"), StrPtr(strPath), 0, 0, 1) > 32 Then
                Debug.Print "Success!"
            End If
        End If
    End Sub
    Success! (Also showing how to make Unicode call since all the others prefer ANSI... using Dir$ spoils this one though)
    Last edited by Merri; Aug 3rd, 2008 at 10:02 AM.

  18. #18
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: launch batch file

    Quote Originally Posted by Merri
    You don't really need to clean up the \ character though
    Good info, Merri. That's modern technology!

    The bat file may exist but if it cannot be run then the Shell() function will return 0. How about this:
    Code:
    Sub RunBatFile(sBatFileName As String, Optional ByVal sPath As String)
       Dim ID As Double
       
       If sPath = "" Then sPath = App.Path
       On Error Resume Next
       ID = Shell(sPath & "\" & sBatFileName)
       If ID = 0 Then MsgBox "Cannot run bat file: " & sBatFileName
    End Sub
    (Don't hate "On Error Resume Next". It's a good statement if we use it properly.)
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width