Saturday, February 12, 2011

Listing files in a folder to a text file on Windows

One endless source of annoyance in my work is copying and a list of file names from some folder to paste in another document.

In the Windows UI, your only option is really right-clicking on individual file names, clicking Rename and then selecting the name to copy. This must be done for each file.

The fastest way I have found to achieve this uses a DOS Command Prompt and flags for the dir command.

To open a Command Prompt window, in pretty much all versions of Windows. Go to Start > Run > type "cmd" (without quotes).

To get a list of files and directories into a text file use the "dir" command with "/B" option

C:\java>dir /B
Apache Software Foundation
apache-ant-1.8.2-bin
apache-ant-1.8.2-bin.zip
apache-ant-1.8.2-manual
apache-ant-1.8.2-manual.zip
chrome.dmp
databene-benerator-0.6.3-dist
databene-benerator-0.6.3-dist.zip
eclipse-jee-ganymede-SR1-win32
eclipse-SDK-3.6.1-win32
eclipse-SDK-3.6.1-win32.zip
javadb-10_5_3_0
javadb-10_5_3_0.zip
MiniConnectionPoolManager.zip
opencsv-2.2
opencsv-2.2-src-with-libs.tar.gz
swt-3.6.1-cocoa-macosx
swt-3.6.1-cocoa-macosx.zip
swt-3.6.1-gtk-linux-x86
swt-3.6.1-gtk-linux-x86.zip
swt-3.6.1-gtk-linux-x86_64
swt-3.6.1-gtk-linux-x86_64.zip
swt-3.6.1-win32-win32-x86
swt-3.6.1-win32-win32-x86.zip
TimeTrack_win32


To have the output of this command piped to a file "C:\output.txt" use the ">" symbol:
dir /B > C:\output.txt

To only show files, no directories you can use the /A:-D flag ("/A:" is for attribute matching filters, "D" is directories, "-" is for not):
dir /A:-D/B > C:\output.txt

Results in the file contents being:

apache-ant-1.8.2-bin.zip
apache-ant-1.8.2-manual.zip
chrome.dmp
databene-benerator-0.6.3-dist.zip
eclipse-SDK-3.6.1-win32.zip
javadb-10_5_3_0.zip
MiniConnectionPoolManager.zip
opencsv-2.2-src-with-libs.tar.gz
output.txt
swt-3.6.1-cocoa-macosx.zip
swt-3.6.1-gtk-linux-x86.zip
swt-3.6.1-gtk-linux-x86_64.zip
swt-3.6.1-win32-win32-x86.zip


Note: on some versions of Windows you can't save a file to C:\ without running the cmd.exe as an Administrator.

For more information on the other flags for more control over your output:
C:\java>dir /?
Displays a list of files and subdirectories in a directory.

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

[drive:][path][filename]
Specifies drive, directory, and/or files to list.

/A Displays files with specified attributes.
attributes D Directories R Read-only files
H Hidden files A Files ready for archiving
S System files I Not content indexed files
L Reparse Points - Prefix meaning not
/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
/P Pauses after each screenful of information.
/Q Display the owner of the file.
/R Display alternate data streams 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

Switches may be preset in the DIRCMD environment variable. Override
preset switches by prefixing any switch with - (hyphen)--for example, /-W.

No comments:

Post a Comment