Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Shelling to Command Prompt to run a DOS command???/

Status
Not open for further replies.

DougP

MIS
Dec 13, 1999
5,985
US
I am writing a program tha lists all of the files in a folder.<br>
you can do this by going to the &quot;Command Prompt&quot; and typing <br>
dir *.* &gt; docs.txt <br>
which will list all of the files and creates a text doc.<br>
<br>
I want to do this without having that ugly Black window pop up<br>
<p> DougP<br><a href=mailto: dposton@universal1.com> dposton@universal1.com</a><br><a href= > </a><br>
 
Doug, How about doing something like this?<br>
<br>
Private Sub Form_Load()<br>
Dim strFilename As String<br>
<br>
'List the first file in C:\<br>
strFilename = Dir(&quot;C:\&quot;, vbNormal)<br>
Debug.Print strFilename<br>
<br>
'While filename are being returned, keep getting the next filename.<br>
Do While strFilename &lt;&gt; &quot;&quot;<br>
<br>
'Get the next file in the directory.<br>
strFilename = Dir()<br>
Debug.Print strFilename<br>
Loop<br>
End Sub<br>
<br>
This will do the same thing, but from VB itself. If you want the contents in a file, add the filenames as you read them.<br>
<br>
Steve<br>
<A HREF="mailto:sdmeier@jcn1.com">sdmeier@jcn1.com</A>
 
Ok I just thought of that about 10 minutes after I posted this. Thanks<br>
<br>
Do you know how to CALL the Command Prompt window and send commands to it?<br>
and Close it too afterwords?<br>
<br>
<p> DougP<br><a href=mailto: dposton@universal1.com> dposton@universal1.com</a><br><a href= > </a><br>
 
&lt;Do you know how to CALL the Command Prompt window and send &lt;commands to it?<br>
&lt;and Close it too afterwords?<br>
<br>
Option Explicit<br>
<br>
Private Declare Function AllocConsole Lib &quot;kernel32&quot; () As Long<br>
Private Declare Function GetStdHandle Lib &quot;kernel32&quot; (ByVal nStdHandle As Long) As Long<br>
Private Declare Function WriteConsole Lib &quot;kernel32&quot; Alias &quot;WriteConsoleA&quot; (ByVal hConsoleOutput As Long, ByVal lpBuffer As String, ByVal nNumberOfCharsToWrite As Long, lpNumberOfCharsWritten As Long, lpReserved As Any) As Long<br>
Private Declare Function CloseHandle Lib &quot;kernel32&quot; (ByVal hObject As Long) As Long<br>
Private Declare Function FreeConsole Lib &quot;kernel32&quot; () As Long<br>
<br>
Private Const STD_OUTPUT_HANDLE = -11&<br>
<br>
Private hConsole As Long<br>
<br>
Private Sub Form_Load()<br>
Dim txt As String<br>
Dim num_written As Long<br>
<br>
If AllocConsole() Then<br>
hConsole = GetStdHandle(STD_OUTPUT_HANDLE)<br>
If hConsole = 0 Then MsgBox &quot;Couldn't allocate STDOUT&quot;<br>
<br>
' Present a warning.<br>
txt = &quot;******************************************&quot; & vbCrLf & _<br>
&quot;* Warning: Do not close this window! *&quot; & vbCrLf & _<br>
&quot;* Close the VB program's window instead. *&quot; & vbCrLf & _<br>
&quot;******************************************&quot; & vbCrLf<br>
WriteConsole hConsole, txt, Len(txt), num_written, vbNullString<br>
<br>
' Make this form visible and on top.<br>
Me.Show<br>
SetFocus<br>
Else<br>
MsgBox &quot;Couldn't allocate console&quot;<br>
End If<br>
End Sub<br>
<br>
a CommandButton with :<br>
<br>
Private Sub Command1_Click()<br>
Dim app_name As String<br>
Dim txt As String<br>
Dim num_written As Long<br>
<br>
app_name = App.Path<br>
If Right$(app_name, 1) &lt;&gt; &quot;\&quot; Then app_name = app_name & &quot;\&quot;<br>
app_name = app_name & &quot;test.bat&quot;<br>
<br>
txt = &quot;Ready to run&quot; & vbCrLf<br>
WriteConsole hConsole, txt, Len(txt), num_written, vbNullString<br>
Shell app_name<br>
End Sub<br>
<br>
Private Sub Form_Unload(Cancel As Integer)<br>
CloseHandle hConsole<br>
FreeConsole<br>
End Sub<br>
<br>
<br>
<p>Eric De Decker<br><a href=mailto:vbg.be@vbgroup.nl>vbg.be@vbgroup.nl</a><br><a href= Center : Visual Basic Center</a><br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top