You are on page 1of 5

HOW TO PRINT A DIRECTORY TREE FROM WINDOWS EXPLORER

Do you ever have a need to print the contents of a folder or entire directory tree in
Windows XP or Vista?
If you do, Microsoft has a knowledge base article that will add "Print Directory" to
Windows Explorer right click menu which will print the folder contents (or directory tree)
that you are currently viewing.
But the problem with Microsoft's solution in Step 2 is that File Types \ Advanced option in
Folder Options does not exist in Vista, and for XP you need to hack the registry to prevent
the Search Companion window from opening when you try to open the folder using
Windows Explorer.
FORGET THAT CRAP!
Fortunantly there's an easier way to use this feature without messing around in the
registry or searching all day for the Advanced button that doesn't exist.
To do this, we'll use Step 1 as described in the knowledge base article, and create the
batch file which will include the commands needed for printing.
Creating The Printdir Batch File

 Open Notepad by clicking on Start \ Run and enter notepad in the Run dialog box
and click OK.
 Copy and paste the following text into Notepad:
@echo off
dir %1 /-p /o:gn > "%temp%\Listing"
start /w notepad /p "%temp%\Listing"
del "%temp%\Listing"
exit
 Close Notepad and click Yes to save the changes.
 In the Save As dialog box, copy and paste the following text in the File name: field
(shown in below screen shot):
%windir%\Printdir.bat
 Select All Files (*.*) in the Save as type: drop down field. Then Click Save.

The Printdir.bat file has now been saved in the \Windows folder.
Next, we'll need create a shortcut to the Printdir.bat file in the Send To
folder.
Create Printdir Shortcut

 In Vista, click on Start \ Run and enter the following in the Run dialog box and click
OK
%APPDATA%\Microsoft\Windows\SendTo
(NOTE: %APPDATA% is an environment variable that points to your user name
AppData\Roaming folder).
 In XP, click on Start \ Run and enter the following in the Run dialog box and click
OK (%USERPROFILE% is an environment variable that points to your use name
Documents and Settings folder).
%USERPROFILE%\SendTo
 In the SendTo folder, right click and select New \ Shortcut.
 Type %windir%\Printdir.bat in the "Type the location of the item:" field and click
Next.

 Type Print Directory Listing in the "Type a name for this shortcut" and click Finish.

Now when you have the need to print a directory tree of folder contents from Windows
Explorer, just right click and select Send To \ Print Directory Listing.
Much better than mucking around in the registry!
Hey What About Printing From The Command Prompt?
Yea, if you like to work at the command prompt, you can just type printdir.bat (if you
created the batch file) at the prompt or enter the following command:
dir /-p /o:gn > %temp%\Listing | start /w notepad /p %temp%\Listing
The command is similar to what we used in the Printdir.bat file, except were "piping" the
command together with the "|" character (found above the Enter key and holding Shift
down).
By using "|" (SHIFT+\ above Enter key), it allows you to execute two commands at once.
Every time you execute, it will over write the file "Listing" so you don't need to worry
about deleting it.
Cool!

POPULATE A TREEVIEW WITH DRIVES FOLDER


folder structure in treeview

 Populate a treeview with drives folder, you can try to refer the below sample code:

    private const string  VirtualImageRoot = @"D:\public";


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            PopulateTree(); 
        }
    }

    private void PopulateTree()


    {
        //Populate the tree based on the subfolders of the specified VirtualImageRoot
        //Dim rootFolder As New DirectoryInfo(Server.MapPath(VirtualImageRoot))

        DirectoryInfo rootFolder = new DirectoryInfo(VirtualImageRoot);


        TreeNode root = AddNodeAndDescendents(rootFolder, null);
        //Add the root to the TreeView
        DirectoryTree.Nodes.Add(root);
    }

    private TreeNode AddNodeAndDescendents(DirectoryInfo folder, TreeNode parentNode)


    {
        //Add the TreeNode, displaying the folder's name and storing the full path to the folder as
the value...
        string virtualFolderPath;

        if (parentNode == null)


        {
            virtualFolderPath = VirtualImageRoot;
        }
        else
        {
            virtualFolderPath = parentNode.Value + folder.Name + "/";
        }

        TreeNode node = new TreeNode(folder.Name, virtualFolderPath);

        //Recurse through this folder's subfolders


        DirectoryInfo[] subFolders = folder.GetDirectories();

        foreach (DirectoryInfo subFolder in subFolders)


        {
            TreeNode child = AddNodeAndDescendents(subFolder, node);

            foreach (FileInfo file in subFolder.GetFiles())


            {
              
                int index = file.FullName.LastIndexOf("\\");
                string strname = file.FullName.Substring(index + 1);
                string[] name = strname.Split('.');

                TreeNode tn = new TreeNode();


                if (name.Length >1&& name[1].ToString().ToUpper() == "MP3")
                {
                    tn = new TreeNode(name[0], file.FullName, @"~/Image/button-search.gif");
                }
                else
                {
                    tn = new TreeNode(name[0], file.FullName);
                }
                child.ChildNodes.Add(tn);
            }
            node.ChildNodes.Add(child);
          
        }
        //Return the new TreeNode
        return node;
    }

Hope it helps.

 SureSoft

You might also like