March 5, 2012

I have been asked by quite a few people on listing documents from document libraries that include sub-folders; so I’d like to share with you a poor-man method of listing all files and folders in a document library in SharePoint with hundreds of files and folders, and displaying the contents of this in NotePad.

Now, I know there are probably lots of nice little apps out there, and I must admit writing one myself called GEGETDOCCONFIG (which does a whole lot more). There’s also information on how to use Powershell to enumerate document libraries here – but you can very quickly list all files and folders from a DIRectory assuming you can get its UNC (Universal Naming Convention) path and you can access the entire document library. All you will need to do is to use the famous DIR command from the command prompt, and you can do this without having to log onto any of the servers in your SharePoint farm.

If you’ve spent much time working in a DOS-based environment, chances are that you’re very familiar with the DIR command. When used in its most basic form, the DIR command simply displays a list of all the files and subdirectories contained within a particular directory. However, the DIR command has access to a fleet of special command line parameters that allow it to perform a host of very specific file listing and sorting operations. While you can perform many of these operations in Windows Explorer, the speed and accuracy with which you can perform them with the DIR command is astounding. Furthermore, you can easily combine these command line parameters to create unique directory listings on the fly.
You will need access to the COMMAND PROMPT, so you can use it against the UNC. A UNC is a naming convention used primarily to specify and map network drives in Microsoft Windows. Support for UNC also appears in other operating systems via technologies. UNC names are most commonly used to reach file servers or printers on a LAN. UNC names identify network resources using a specific notation. UNC names consist of three parts – a server name, a share name, and an optional file path. We will need the UNC of the relevant SharePoint location and then use DIR against it.

Follow these steps:

Step 1: Get the UNC

Take for example, a document library whose contents you would like to list:

http://documents/Products/Forms/AllItems.aspx

Its UNC from Windows Explorer would be:

\\documents\products

Make a note of the UNC path you will need it in the next step.

Step 2: Generate the List

So, to list all the files and the folders, off to DOS we go:

Click Start -> Accessories -> Command Prompt

Then, enter this command:

DIR /d /s (name of the above UNC path) > (name of the file you want to pipe the list to)

For example:

DIR /d /s \\documents\products > c:\productfilelist.txt

This will list all the files and folders from the UNC path into productfilelist.txt. Then, all you need to do is open this in notepad or clean it up in Excel.

That’s all there is to it.

Note that I used various switches, /d and /s in my DIR command – check out some other switches below.

/A:attributes

Only the files whose attributes match the ones you specify will be displayed. You can enter a sequence of attributes after the colon. It is not necessary to enter spaces between entries.

The possible attributes are:

  • H|-H – Hidden (or not hidden) files.
  • S|-S – System (or non system) files.
  • D|-D – DIRectories (or files only).
  • R|-R – Read-only (or read/write) files.
  • A|-A – Archivable (or already archived) files.

/O:order

Allows you to specify the order in which the entries will be displayed.

The possible options are:

  • N|-N – By name (alphabetical or reverse alphabetical).
  • E|-E – By extension ( alphabetical or reverse alphabetical).
  • D|-D – By date and time (chronologically or reverse).
  • S|-S – By size (increasing or decreasing).
  • C|-C – Sorts by DoubleSpace compression ratio lowest to highest or highest to lowest.
  • G|-G – Group directory (before, or after) other files.

Other Switches

  • /B – (Bare format) – Displays only file names.
  • /L Information is displayed in lowercase letters.
  • /S Displays file entries in the specified directory and all subdirectories located below it hierarchically.
  • /P Pauses when the screen is full. Press any key to display another screen full of data.
  • /W Displays only filenames and directory names (without the added information about each file) in a five-wide display format.

Also, note that you can use speech-marks to surround the UNC path if there are spaces in it. For example:

http://myportal/mysubsite/shared documents

would read as:

“\\myportal\mysubsite\shared documents”

Two final TechNet references for you if you need to get further information on DIR and UNC are below:

DIR COMMAND: http://technet.microsoft.com/en-us/library/cc755121(v=ws.10).aspx

UNC: http://technet.microsoft.com/en-us/library/cc939978.aspx

You May Also Like…