Results 1 to 30 of 30

Thread: Command Prompt

Hybrid View

  1. #1

    Thread Starter
    Banned ThaRubby's Avatar
    Join Date
    Apr 2005
    Location
    127.0.0.1
    Posts
    356

    Re: Command Prompt

    Awesome thats pretty good and useable but is it also posible to get the command issued...bacily everything that happened?

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

    Re: Command Prompt

    Instead of a batch file you can just pass the DOS commands directly. Then output the results to a file.
    Its not that hard.

    This example will pass the DIR command, enter C:\, and output the directory listing to the file C:\Dir.txt
    The /K switch is to keep the DOS window open but you can change it to the /C to close it. Also, SW_HIDE to make
    it totally transparent to the user.

    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 Const SW_HIDE As Long = 0
    7. Private Const SW_SHOWNORMAL As Long = 1
    8.  
    9. Private Sub Command1_Click()
    10.     ShellExecute Me.hwnd, "Open", "C:\Windows\System32\CMD.exe", " /K Dir C:\  > C:\Dir.txt", "C:\", SW_SHOWNORMAL
    11. End Sub
    Note: you would want to use the GetSystemDirectory API to dynamically get the directory where the CMD.EXE program is located.

    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

  3. #3
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Command Prompt

    Quote Originally Posted by RobDog888
    you would want to use the GetSystemDirectory API to dynamically get the directory where the CMD.EXE program is located.
    Well that would require that cmd.exe actually is in that directory, and on a Win9x/ME box cmd.exe doesn't even exist. What you can use instead is Environ("COMSPEC").

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

    Re: Command Prompt

    Quote Originally Posted by Joacim Andersson
    Well that would require that cmd.exe actually is in that directory, and on a Win9x/ME box cmd.exe doesn't even exist. What you can use instead is Environ("COMSPEC").
    At the end of my post I added a Note that you can get the CMD.exe location dynamically using the GetSystemDirectory API.

    I dont like to rely on the environ variable since it can be changed or deleted by the user.

    Quote Originally Posted by dgleinna
    How do you append command output to the same file, though?
    You simple use the append mode of file output instead of the output mode.

    Output: >
    Append: >>

    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

  5. #5

    Thread Starter
    Banned ThaRubby's Avatar
    Join Date
    Apr 2005
    Location
    127.0.0.1
    Posts
    356

    Re: Command Prompt

    Anyway I can send the output into a string and into a Textbox?

    I tried this it was pretty dumb heh..

    Private Sub Form_Load()
    Dim Test As String
    Test = Shell("C:\file.bat > " & Test)
    Text1.Text = Test
    End Sub


    The output of text1.text was "3628" lol

  6. #6
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Command Prompt

    You could read the textfile you have written into a textbox.

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

    Re: Command Prompt

    Read the outputted file into your program using basic file i/o and set the .Text property of your textbox to the
    contents (assuming mult-line textbox).
    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

  8. #8

    Thread Starter
    Banned ThaRubby's Avatar
    Join Date
    Apr 2005
    Location
    127.0.0.1
    Posts
    356

    Re: Command Prompt

    kk ill start googleing how to read files... thanks guys been a big help

  9. #9
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Command Prompt

    Quote Originally Posted by RobDog888
    At the end of my post I added a Note that you can get the CMD.exe location dynamically using the GetSystemDirectory API.

    I dont like to rely on the environ variable since it can be changed or deleted by the user.
    Yes I know, but that requires that cmd.exe is in the system folder. Besides the ComSpec environment variable is safe to use, Win9x doesn't even work properly if you change that to an invalid command interpretor. And on an NT based system the ComSpec environment variable is a system variable which you need admin rights to change.

    Anyhow... I've attached a module that uses a different approach (and a tiny bit more advanced) to this. It contains a function called GetCommandOutput that will shell any command and return the output written to StdOut and StdErr as a string. You can then do whatever you want with that string, such as write it to a file, display it in a text box or whatever...

    One of the (optional) arguments to this function is called fOEMConvert which would convert the OEM characters use by a command line to the Ansi characters used by Windows. This is especially importent if your command line program will display international characters (like Å or ü for example) since they are different in Ansi compared to OEM.
    Attached Files Attached Files

  10. #10
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Command Prompt

    How do you append command output to the same file, though?

  11. #11
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Command Prompt

    Quote Originally Posted by dglienna
    How do you append command output to the same file, though?
    You use >> instead of the > redirection character.

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