Mar 31 2008

List only Files and Folders in a Specific Directory and Sub-Directory

Dir displays a list of files and subdirectories in a directory.

DIR is a command used to display a list of files and subdirectories in a directory..

Windows 2000 and Windows XP syntax

DIR [drive:][path][filename] [/A[[:]attributes]] [/B] [/C] [/D] [/L] [/N] [/O[[:]sortorder]] [/P] [/Q] [/S] [/T[[:]timefield]] [/W] [/X] [/4]


Specifies drive, directory, and/or files to list.


D  Directories
 R  Read-only files
 H  Hidden files
 A  Files ready for archiving
 S  System files
 -  Prefix meaning not<?xml:namespace prefix = o /><?XML:NAMESPACE PREFIX = O />


Uses bare format (no heading information or summary).


Display the thousand separator in file sizes. This is the default. Use /-C to disable display of separator.


Same as wide but files are list sorted by column.


Uses lowercase.


New long list format where filenames are on the far right.


List by files in sorted order.


N  By name (alphabetic)
 S  By size (smallest first)
 E  By extension (alphabetic)
 D  By date/time (oldest first)
 G  Group directories first
 -  Prefix to reverse order<?xml:namespace prefix = o /><?XML:NAMESPACE PREFIX = O />


Pauses after each screenful of information.


Display the owner of the file.


Displays files in specified directory and all subdirectories.


Controls which time field displayed or used for sorting


C Creation

A Last Access

W Last Written


Uses wide list format.


This displays the short names generated for non-8dot3 file names. The format is that of /N with the short name inserted before the long name. If no short name is present, blanks are displayed in its place.


Displays four-digit years

the example list all folders under QTP installation folder. optionally you can load the output to an array, id you need to iterate the list.

Dim outMsg, listArr, dirName
dirname = Chr( 34 ) & Environment( "ProductDir" ) & Chr( 34 )
If Not RunDosCommand( "DIR /S " & dirname, outMsg ) Then
    Reporter.ReportEvent micFail, "RunDosCommand", outMsg
    ExitTest( "ERROR:RunDosCommand" )
End If
Print outMsg
' ** loading the list in an array
listArr = Split( outMsg, vbCrLf, -1 )

Note: When using Windows file names, you need to quote the path.

RunDosCommand Function

Public Function RunDosCommand( ByVal cmd, ByRef stdout )
   Dim sb, wShell, exec
   Dim input

   RunDosCommand = False
   Set wShell = CreateObject( "WScript.Shell" )
   ' ** Executing the command
   Set exec = wShell.Exec( "%comspec% /C " & cmd )
   ' ** Checking for errors...
   If Not exec.StdErr.AtEndOfStream Then
      stdout = "STDERR: " & exec.StdErr.ReadAll
      Set exec = Nothing : Set wShell = Nothing
      Exit Function
   End If
   ' ** The stringBuilder is faster than concatenation ( & )
   Set sb = DotNetFactory.CreateInstance( "System.Text.StringBuilder" )
   Do While Not exec.StdOut.AtEndOfStream
      input = Trim( exec.StdOut.ReadLine )
      ' ** Appending only not empty lines 
      If Len( input ) > 0 Then
         sb.AppendLine input
      End If
   ' ** retieve the output
   stdout = sb.ToString
   RunDosCommand = True
   Set sb = Nothing : Set exec = Nothing : Set wShell = Nothing
End Function