Click to See Complete Forum and Search --> : DIR In Dos Window?


softweng
July 10th, 2001, 01:08 PM
How can I open a DOS window, pipe a directory listing to a text file, and close the window from VB?

The DOS command is like this:
Dir C:\ >C:\Test\DirList.txt

This Needs to work on NT4 and Win2k.

I am sure I have to shell it but I can't get it to work.

Kris
Software Engineer
Phoenix,AZ

softweng
July 10th, 2001, 03:19 PM
I figured it out. This is how I did it and it works.

In a Module:

option Explicit

'//public Constants
public Const NORMAL_PRIORITY_CLASS = &H20&
public Const INFINITE = -1&

'//public Types
public Type STARTUPINFO
cb as Long
lpReserved as string
lpDesktop as string
lpTitle as string
dwX as Long
dwY as Long
dwXSize as Long
dwYSize as Long
dwXCountChars as Long
dwYCountChars as Long
dwFillAttribute as Long
dwFlags as Long
wShowWindow as Integer
cbReserved2 as Integer
lpReserved2 as Long
hStdInput as Long
hStdOutput as Long
hStdError as Long
End Type

public Type PROCESS_INFORMATION
hProcess as Long
hThread as Long
dwProcessID as Long
dwThreadID as Long
End Type

'//API Declarations
public Declare Function WaitForSingleObject Lib "kernel32" (byval _
hHandle as Long, byval dwMilliseconds as Long) as Long

public Declare Function CreateProcessA Lib "kernel32" (byval _
lpApplicationName as Long, byval lpCommandLine as string, byval _
lpProcessAttributes as Long, byval lpThreadAttributes as Long, _
byval bInheritHandles as Long, byval dwCreationFlags as Long, _
byval lpEnvironment as Long, byval lpCurrentDirectory as Long, _
lpStartupInfo as STARTUPINFO, lpProcessInformation as _
PROCESS_INFORMATION) as Long

public Declare Function CloseHandle Lib "kernel32" (byval _
hObject as Long) as Long




In A Form:

private Sub Command2_Click()

on error resume next

'//Create The Directory Listing
CreateDirList "C:\Test", 6969

End Sub

private Sub CreateDirList(sPath as string, ArchiveNum as Integer)
Dim retval as Double
Dim AppToRun as string
Dim ParamForApp as string
Dim CmdLine as string

on error GoTo ErrHandler

'//Create Directory Listing for Each CD Using File Piping
'//set Application to Run
AppToRun = "C:\WINNT\System32\CMD.exe"

'//set Command Line Parameters
ParamForApp = " /C Dir """ & sPath & """ >\\gotham\wi_sup$\archives\TestDir-" & ArchiveNum & ".txt"

'//Build Command string
CmdLine = AppToRun & ParamForApp

'//Shell App And Wait for It to Finish
ExecCmd CmdLine

Exit Sub

ErrHandler:
'//Display error
ProcessError ("frmArchive.GetDirList")
End Sub

public Sub ExecCmd(CmdLine as string)
Dim Proc as PROCESS_INFORMATION
Dim Start as STARTUPINFO
Dim ReturnValue as Integer

on error GoTo ErrHandler

'//Initialize The STARTUPINFO Structure
Start.cb = len(Start)

'//Start The Shelled Application
ReturnValue = CreateProcessA(0&, CmdLine, 0&, 0&, 1&, _
NORMAL_PRIORITY_CLASS, 0&, 0&, Start, Proc)

'//Wait for The Shelled Application to Finish
Do
ReturnValue = WaitForSingleObject(Proc.hProcess, 0)
DoEvents
Loop Until ReturnValue <> 258

'//Close Handle to Shelled Application
ReturnValue = CloseHandle(Proc.hProcess)

Exit Sub

ErrHandler:
'//Display error
ProcessError ("frmArchive.ExecCmd")
End Sub





It works great for me.


Kris
Software Engineer
Phoenix,AZ