Results 1 to 9 of 9

Thread: sending dos commands to a dos prompt

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2004
    Posts
    15

    sending dos commands to a dos prompt

    hi guys,

    is there anyway of sending dos commands to a dos prompt using vb? i don't want to execue a batch file using the shell function...this is not what i want. All i want is to send dos commands to the same dos session. plz help.

    thanks,
    zaf

  2. #2
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961
    Once a Dos Shell is up and running Im not sure how you would pull that off, but I know you can call the command shell 1 time with another DOS command as an argument and it will run it for you once.

  3. #3
    Addicted Member
    Join Date
    Aug 2002
    Location
    Luton, UK
    Posts
    178
    Easy enough. This works to do a CD and DIR and waits for an Enter to exit:-

    Code:
    Sub test()
        Enter = "~"
        retval = Shell("c:\winnt\system32\cmd.exe", vbNormalFocus)
        SendKeys "CD C:\TEMP" & Enter, True ' change folder
        SendKeys "DIR" & Enter, True        ' dir
        SendKeys "EXIT". True
    End Sub
    Regards
    BrianB
    -------------------------------

  4. #4

    Thread Starter
    New Member
    Join Date
    May 2004
    Posts
    15
    Thanks guys, "sendkey" seems to have done the trick.

    zaf

  5. #5
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961
    Originally posted by BrianB
    Easy enough. This works to do a CD and DIR and waits for an Enter to exit:-

    Code:
    Sub test()
        Enter = "~"
        retval = Shell("c:\winnt\system32\cmd.exe", vbNormalFocus)
        SendKeys "CD C:\TEMP" & Enter, True ' change folder
        SendKeys "DIR" & Enter, True        ' dir
        SendKeys "EXIT". True
    End Sub
    This is not DOS. Does this also work on a DOS prompt? DOS is run from Win9x like "Command.com", not "Cmd.exe".
    Last edited by Dave Sell; Jul 23rd, 2004 at 09:32 AM.

  6. #6

    Thread Starter
    New Member
    Join Date
    May 2004
    Posts
    15
    i'm using command.com.

  7. #7
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961
    Cool, thanks. Can this work with the DOS window minimized?

  8. #8

    Thread Starter
    New Member
    Join Date
    May 2004
    Posts
    15
    the command prompt must have focus, else the text will not get printed in the dos prompt:

    AppActivate "command.com" 'Command Prompt title bar

    zaf

  9. #9
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    SendKeys - Arrrg! SendKeys is so flakey that if the user
    accidentally changes the active application or presses a key, it
    will throw SendKeys out of sync.

    ShellExecute CDM.exe with parameters of what you want to pass or
    execute in the DOS window.
    Ex. this will create a text file of your c drive contents and close the
    command window. This is very fast, better than using vb commands
    to iterate through all the directories and out to a text file.

    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Long, _
    4. ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, _
    5. ByVal nShowCmd As Long) As Long
    6.  
    7. Private Const SW_SHOWNORMAL As Long = 1
    8. Private Const SW_SHOWMINIMIZED As Long = 2
    9. Private Const SW_SHOWMAXIMIZED As Long = 3
    10.  
    11. Private Sub Command1_Click()
    12.     Dim lRet As Long
    13.     lRet = ShellExecute(0, "OPEN", "c:\winnt\system32\cmd.exe", "/C Dir >> C:\Dir_C.txt", "C:\", SW_SHOWNORMAL)
    14. End Sub


    HTH
    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

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