How to Print a Windows Directory

Need to print a windows directory?

Here’s how:
1. Open up notepad
2. paste this text in

Windows Registry Editor Version 5.00

@="Print Directory"


3. Save file as printdir.reg
4. Open up a new notepad window
5. paste this in:

@echo off
dir %1 /-p /o:gn /S > "%temp%\Listing"
start /w notepad "%temp%\Listing"
del "%temp%\Listing"

If you prefer to sort by file size use this instead:

@echo off
dir %1 /-p /o:-s /S > "%temp%\Listing"
start /w notepad "%temp%\Listing"
del "%temp%\Listing"

6. save file as printdir.bat
7. copy printdir.bat to /windows dir
8. double click the .reg file and add to registry

Now when you right click on a directory you have an option to print it to a text file. That’s how you print windows directory

Additional Info

Although not as full featured as the Linux ls command, the humble dir command offers us a bunch of options, allowing us to mimic pretty much everything Hal has done above. The main options we’ll use here are:
/o followed by a one-character option that lets us specify a sort order (we’ll use /os to sort by size and /od by date… with a – sign in front of the one character to reverse order)
/t, also followed by one character which lets us specify a time field we’re interested in (the field options we have and their definitions, according to the dir command’s help, are /tc for Creation time, /ta for Last Access time, and /tw for Last Written time).

So, to get a directory listing sorted by size (smallest to largest), we’d run:

C:\> dir /os

Want them reversed? We would use:

C:\> dir /o-s

Want those sizes in human readable form? Install Cygwin and use the ls command, for goodness sakes. This is the dir command we’re talking about here. We don’t need no stinkin’ human readable format. Actually, the default output for dir does show commas in its size numbers, making things a little more readable than the stock Linux output.

To see directory contents listed by Last Written (which is what dir calls them… roughly the same as last modified times in Linux parlance), in reverse order (with the most recently modified near the top), you could execute:

C:\> dir /o-d /tw

But, like we see with the ls command, Last Written is the default, so you can leave off the /tw to get the same results.

Wanna sort by creation time, again in reverse? Use:

C:\> dir /o-d /tc

And, how about last access? You could go with:

C:\> dir /o-d /ta

It’s a good thing that the /od and /o-d sort options pick up the proper timestamp specified by the /t option, or else we’d be forced to do some waaaaay ugly sort command nonsense. Whew!

Tim responds too:

To get a directory listing we use Get-ChildItem. The name is a bit odd, but it is a generic command and can be used to get the child items from any container such as the registry, file system, or the certificate store. Today we are just looking at the file system.

First, let’s take a look at the aliases for this useful cmdlet.

PS C:\> Get-Alias -Definition Get-ChildItem

CommandType Name Definition
———– —- ———-
Alias dir Get-ChildItem
Alias gci Get-ChildItem
Alias ls Get-ChildItem

I typically use ls since it is 33% more efficient to type than dir. But I digress…

Let’s sort by file size:

PS C:\> gci | sort length

Directory: C:\

Mode LastWriteTime Length Name
—- ————- —— —-
-a— 6/10/2009 4:42 PM 10 file1.txt
-a— 6/10/2009 4:42 PM 24 file2.txt
-a— 11/24/2009 3:56 PM 1442522

The Get-ChildItem cmdlet does not have sorting capability built in, none of the cmdlets do. But that is what the pipeline and the Sort-Object cmdlet are for.

Want to sort by file size in reverse order? Use the Descending parameter.

PS C:\> gci | sort length -descending

Directory: C:\

Mode LastWriteTime Length Name
—- ————- —— —-
-a— 11/24/2009 3:56 PM 1442522
-a— 6/10/2009 4:42 PM 24 file2.txt
-a— 6/10/2009 4:42 PM 10 file1.txt

We can sort by any property, including LastAccessTime, LastWriteTime, or CreationTime.

PS C:\> gci | sort LastWriteTime

We can even sort on two properties.

PS C:\> gci | sort LastWriteTime, Length

Directory: C:\

Mode LastWriteTime Length Name
—- ————- —— —-
-a— 6/10/2009 4:42 PM 24 file2.txt
-a— 6/10/2009 4:42 PM 10 file1.txt
-a— 11/24/2009 3:56 PM 1442522

The files will first be sorted by write time. If two files have the same write time, they will then be sorted by length.

Finally, we come to displaying the size in a human readable format, and it isn’t pretty. We have to write a custom expression to display the size in KB or MB.

PS C:\> gci | format-table -auto Mode, LastWriteTime, Length,
@{Name=”KB”; Expression={“{0:N2}” -f ($_.Length/1KB) + “KB” }},
@{Name=”MB”; Expression={“{0:N2}” -f ($_.Length/1MB) + “MB” }},

Mode LastWriteTime Length KB MB Name
—- ————- —— — — —-
-a— 6/10/2009 4:42 PM 10 0.01KB 0.00MB file1.txt
-a— 6/10/2009 4:42 PM 24 0.02KB 0.00MB file2.txt
-a— 11/24/2009 3:56 PM 1442522 1,408.71KB 1.38MB

We can specify custom properties to display. This format works with any of the format cmdlets (Get-Command -Verb Format) or select-object. The custom columns are created by using a hashtable. A hashtable is specified by using @{ key1=value1, key2=value2 }. In our case we specify a name and an expression. Here is a simple example.

…, @{Name=”Foo”; Expression={ $_.Length + 1 }}, …

In this case we would add a column with the heading Foo and with a value of the Length plus 1. The expression can include all sorts of math or other crazy PowerShell fu.

Ironically, getting a human readable output comes from a non-human readable command.

Leave a Reply




You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>