Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

DOS Dir command in VB

5 views
Skip to first unread message

Julie and Arnold Kay

unread,
Aug 1, 1999, 3:00:00 AM8/1/99
to
Hi,

At a command prompt, the command "dir c:/s>filename.out" can be run to
get a list of all the directories, filenames and the file sizes on drive
C: and redirected to a text file. Is there an equivalent way to do this
in Visual Basic?

AK

Geert Marttin

unread,
Aug 2, 1999, 3:00:00 AM8/2/99
to
Julie and Arnold Kay <J-A...@worldnet.att.net> wrote in message
news:37A541...@worldnet.att.net...
Call Shell("dir c:/s>filename.out", vbHide)?

HTH. Later...

Geert Marttin

-----
"Discontent is the first step in the progress of a man or a nation."
Oscar Wilde

Rick Rothstein

unread,
Aug 2, 1999, 3:00:00 AM8/2/99
to

Shell Environ("comspec") & " /c dir c: /s > filename.out", vbHide

If you are going to supply the filename.out file through a variable and
include the path with it, be aware that DOS is sensitive to blank spaces in
paths or filenames. Enclose the filename variable with extra quotes, like so
(using line continuation to protect against a "funny" break by your news
reader):

Drv = "c:"
FileOut = "filename.out"
Shell Environ("comspec") & " /c dir " & Drv & _
" /s > """ & FileOut &
"""", vbHide

If you will need to use this file inside the same program, then you will
need to wait for the Shell'ed out program to finish. Use the following:

Paste these lines in the (General)(Declarations) section of the form where
the Shell is being called (or remove the Private keywords and put them in a
BAS module if more than one form will use them):

Private Declare Function OpenProcess _
Lib "kernel32" _
(ByVal dwDesiredAccess As Long, _
ByVal bInheritHandle As Long, _
ByVal dwProcessId As Long) As Long
Private Declare Function CloseHandle _
Lib "kernel32" _
(ByVal hObject As Long) As Long
Private Declare Function WaitForSingleObject _
Lib "kernel32" _
(ByVal hHandle As Long, _
ByVal dwMilliseconds As Long) As Long

Call your Shell command in this form with the appropriate Shell arguments
placed in the parentheses:

PID = Shell( <<Put Shell Arguments Here>> )

And finally, paste the following IMMEDIATELY after the PID=Shell statement
above (making sure to handle the possible error where indicated; i.e. stop
the code from falling through to your other commands if the Shell failed):

If PID = 0 Then
'
'Handle Error, Shell Didn't Work
'
Else
hProcess = OpenProcess(&H100000, True, PID)
WaitForSingleObject hProcess, -1
CloseHandle hProcess
End If

One note -- there are some NT systems (those with VERY tight security
measures in place) where this call won't work. No problem at all on 95/98
though.

Rick


Julie and Arnold Kay wrote in message <37A541...@worldnet.att.net>...
>Hi,


>
>At a command prompt, the command "dir c:/s>filename.out" can be run to
>get a list of all the directories, filenames and the file sizes on drive
>C: and redirected to a text file. Is there an equivalent way to do this
>in Visual Basic?
>

>AK

Rick Rothstein

unread,
Aug 2, 1999, 3:00:00 AM8/2/99
to
That won't work because dir and the redirection operation are internal DOS
commands. You have to use the command processor with a /c option. See my
message/answer to this same post you responded to.

Rick

Geert Marttin wrote in message <#bATiXL3#GA.205@cppssbbsa04>...


>Julie and Arnold Kay <J-A...@worldnet.att.net> wrote in message
>news:37A541...@worldnet.att.net...

>> At a command prompt, the command "dir c:/s>filename.out"
>> can be run to get a list of all the directories, filenames and the
>> file sizes on drive C: and redirected to a text file. Is there an
>> equivalent way to do this in Visual Basic?

Rick Rothstein

unread,
Aug 2, 1999, 3:00:00 AM8/2/99
to
Damn, those three lines still got broken in a "funny" place. They should
have read

Drv = "c:"
FileOut = "filename.out"
Shell Environ("comspec") & " /c dir " & Drv & _
" /s > """ & FileOut & """", vbHide

Rick

Rick Rothstein wrote in message ...

>>At a command prompt, the command "dir c:/s>filename.out" can be run to
>>get a list of all the directories, filenames and the file sizes on drive
>>C: and redirected to a text file. Is there an equivalent way to do this
>>in Visual Basic?
>>

>>AK
>
>

Geert Marttin

unread,
Aug 2, 1999, 3:00:00 AM8/2/99
to
Rick Rothstein <fnr...@aosi.com> wrote in message
news:O3RCJaL3#GA....@cppssbbsa02.microsoft.com...

> Geert Marttin wrote in message <#bATiXL3#GA.205@cppssbbsa04>...
> >Call Shell("dir c:/s>filename.out", vbHide)?

> That won't work because dir and the redirection operation are
> internal DOS commands. You have to use the command
> processor with a /c option. See my message/answer to this
>same post you responded to.
You're right. Thanks for correcting me.
0 new messages