«

»

Mar 31 2008

List only directories in a specific 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]

 

[drive:][path][filename]

 

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

 

attributes

 

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

/B

 

Uses bare format (no heading information or summary).

 

/C

 

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

 

/D

 

Same as wide but files are list sorted by column.

 

/L

 

Uses lowercase.

 

/N

 

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

 

/O

 

List by files in sorted order.

 

sortorder

 

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 />

/P

 

Pauses after each screenful of information.

 

/Q

 

Display the owner of the file.

 

/S

 

Displays files in specified directory and all subdirectories.

 

/T

 

Controls which time field displayed or used for sorting

 

timefield

 

C Creation

A Last Access

W Last Written

 

/W

 

Uses wide list format.

 

/X

 

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.

 

/4

 

Displays four-digit years

 

the example list all folders under C:\Automation. optionally you can load the output to an array, id you need to iterate the list.

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
   Loop
   ' ** retieve the output
   stdout = sb.ToString
   RunDosCommand = True
   Set sb = Nothing : Set exec = Nothing : Set wShell = Nothing
End Function