Results 1 to 1 of 1

Thread: Classic VB/API - How Do I Run DOS Commands From VB?

  1. #1

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

    Arrow Classic VB/API - How Do I Run DOS Commands From VB?

    You can run DOS commands from VB via the command window (CMD.exe). There are a few easy ways to do this as shown below. The CMD.exe file is not supported on 95/98 but those OS' do have Command.com

    As long as you're only needing to generate an output this is sufficient, but if you need to read the data back in then you may want to use other methods to detect the completion of the process or a more advanced method to redirect the output from the console window (as can be seen in this FAQ thread).


    You can Shell a DOS Batch file (.bat)

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4.     Shell "C:\MyFile.bat", vbNormalFocus
    5. End Sub
    You can use a more powerful version of the Shell function called the ShellExecute API.
    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _
    4. ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
    5.  
    6. Private Declare Function GetSystemDirectory Lib "kernel32.dll" Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, _
    7. ByVal nSize As Long) As Long
    8.  
    9. Private Const SW_HIDE As Long = 0
    10. Private Const SW_SHOWNORMAL As Long = 1
    11.  
    12.  
    13. Private Sub Command2_Click()
    14.     ShellExecute Me.hwnd, "Open", "C:\MyFile.bat", vbNullString, "C:\", SW_SHOWNORMAL
    15. End Sub
    16.  
    17. 'Or directly pass the parameters to the DOS console...
    18. Private Sub Command3_Click()
    19.  
    20. 'Output: >
    21. 'Append: >>
    22. 'Close DOS Console: /C
    23. 'Keep DOS Console open: /K
    24.     ShellExecute Me.hwnd, "Open", "C:\Windows\System32\CMD.exe", " /C Dir C:\ > C:\Dir.txt", "C:\", SW_SHOWNORMAL
    25. End Sub
    26.  
    27. 'And finally you can add dynamic location of the CMD.exe file.
    28. Private Sub Command4_Click()
    29.     Dim strBuff As String * 255
    30.     Dim strPath As String
    31.     Dim x As Integer
    32.     x = GetSystemDirectory(strBuff, Len(strBuff))
    33.     If x > 0 Then
    34.         x = InStr(strBuff, vbNullChar)
    35.         If x > 0 Then
    36.             strPath = Left$(strBuff, x - 1)
    37.         End If
    38.         ShellExecute Me.hwnd, "Open", strPath & "\CMD.exe", " /C Dir C:\  > C:\Dir.txt", "C:\", SW_SHOWNORMAL
    39.     End If
    40. End Sub
    Last edited by si_the_geek; Oct 19th, 2005 at 07:39 PM.

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