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:
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
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").
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.
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.
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.
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.