( Index )
Month

 

Brief Information about the May '16 CSIG Meeting

FILES.EXE - The Utility That Microsoft Forgot!
Create a Hardcopy Printout of a Directory Listing

by Bruce Arnold
Files.exe     

Welcome to the CSIG, a Special Interest Group of the ACGNJ. This is an exciting time for the C Language programming since Microsoft now has 4 different language compilers: C++, C++ Express, C-Sharp, and C-Sharp Express. These are all capable of creating Windows (tm) programs. (Some are FREE!)

Here's a brief synopsis of the coming meeting:

The object of this program is display and optionally print a list of files in a folder or directory. Think about it. It seems like the simplest task in the world. You have Explorer open and you're looking at the files. Now you want a hard copy printout of the directory. Here are the Microsoft options: a) Bring up a command box and type a DOS command redirected to the printer. (Difficult if it's a network printer.) b) Do an Alt-PrintScreen and paste the partial directory information into Microsoft Paint. This month's program uses Microsoft DOT NET C# code (CLI) to create an HTML5 data file after the folder is selected. Since the viewer is a standard browser such as Internet Explorer or FireFox, the output can be searched and/or printed. Even large folders such as the Windows System32 directory can be printed. (Note: be sure to create a link in your "SendTo" folder.)

The program uses some of the most sophisticated Dot Net Library functions including the following:

ExtractAssociatedIcon()
FolderBrowserDialog()
FileInfo Class
GetTempPath()
GetFiles()
GetEnumerator()
GetCommandLineArgs()
ShowDialog()
StreamWriter()
String::Format()
try ... catch ... finally
WriteLine()

The application using the latest Microsoft Visual Studio C# code and is a rewrite of a MFC program presented years ago. For a comparison see the FILES program from December 2001 and January 2010.
http://acgnj.barnold.us/Csig0112.htm       http://acgnj.barnold.us/Csig1001.htm

Other features

Since the program creates a data file and then calls the system Internet Browser (Internet Explorer, FireFox, Chrome, Safari, etc.), you don't have to actually send it to the printer. Within the browser you may scale it, or search it, or copy parts of it to paste in another application. However, if your goal is to print the listing (even if it is 50 pages) you may use the "print preview" menu of the browser to scale it and customise the output. If you adjust the paper size, you can create a file listing to be included with a DVD or CD Rom.


There are a number of ways to refer to Microsoft's latest compilers and code. Here's what Wikipedia says: The Common Language Infrastructure (CLI) is an open specification developed by Microsoft that describes the executable code and runtime environment that form the core of the Microsoft .NET Framework. The specification defines an environment that allows multiple high-level languages to be used on different computer platforms without being rewritten for specific architectures.

Microsoft .Net Framework 3.5
C++ 9.0
.Net 3.5
CLI
Common Language Infrastructure
Managed

Sample Code

        //               ********************************************************
        //               ******  USER STARTS THE ACTION
        //
        private void button1_Click(object sender, EventArgs e)
            {
            UInt64 totalBytes = 0;
            folderBrowserDialog1.ShowNewFolderButton = false;
            folderBrowserDialog1.RootFolder = System.Environment.SpecialFolder.Desktop;
            String[] args = Environment.GetCommandLineArgs();   // handle SendTo function.
            if (args.Length > 1)                                // Be sure to setup SendTo folder.
                folderBrowserDialog1.SelectedPath = Path.GetDirectoryName(args[1]);
            else
                folderBrowserDialog1.SelectedPath =
                   System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);

            if (folderBrowserDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                //
                // The user selected a folder and pressed the OK button.
                //
                this.Cursor = Cursors.WaitCursor;
                int i = 0;
                try
                    {
                    String[] fileEntries = System.IO.Directory.GetFiles(folderBrowserDialog1.SelectedPath,
                        "*", System.IO.SearchOption.TopDirectoryOnly);
                    System.Array.Sort(fileEntries); // This is required since order is not guaranteed.

                    IEnumerator files = fileEntries.GetEnumerator();

                    DoOpenTempFile(BROWSER_SOURCE_FILE);
                    DoHeader(folderBrowserDialog1.SelectedPath, fileEntries.Length.ToString());

                    while (files.MoveNext())        // Process each filename found in the folder.
                        {
                        FileInfo fi = new FileInfo(Convert.ToString(files.Current));

                        // now continue with the data output.
                        DoLineItem(string.Format("    {0,-24} {1,15:0,0}  {2}",
                            (fi.LastWriteTime).ToString("G"), fi.Length, fi.Name));
                        totalBytes += (UInt64)fi.Length;    // Note: these are 64 bit items.
                        ++i;
                        if (0 == (i % 3)) DoHorizontalLine();
                        }
                    if (i ==0)
                        {
                        MessageBox.Show(String.Format("{0}\r\nThere were no files found.", 
                            folderBrowserDialog1.SelectedPath), "Information");
                        sw.Close();
                        }
                    else
                        {
                        DoTotals(totalBytes);
                        DoFooter();     // and close output file.
                        DoShowBrowser(htmFile); // LAUNCH INTERNET EXPLORER, FIREFOX, ETC.
                        }
                    }
                catch (Exception ee)
                    {
                    MessageBox.Show(String.Format("{0}", ee), "Exception caught.");
                    }
                this.Cursor = Cursors.Default;
                } // end if result ok
            }

        //               ********************************************************
        //               ******  DoOpenTempFile
        //
        private void DoOpenTempFile(string arg)
            {
            tempPath = System.IO.Path.GetTempPath();
            htmFile = tempPath + arg;
            try
                {
                sw = new StreamWriter(htmFile);
                }
            catch
                {
                MessageBox.Show("Unable to open output file.", "Error");
                Close();
                }
            }

        //               ********************************************************
        //               ******  DoHeader
        //
        private void DoHeader(string arg, string count)
            {
            sw.WriteLine("<!DOCTYPE HTML>");
            sw.WriteLine("<HTML>");

            sw.WriteLine("<head>");
            sw.WriteLine("<meta charset=\"UTF-8\"");
            sw.WriteLine(string.Format("<!-- \r\n        {0} \r\n -->", VERSION));
            sw.WriteLine(string.Format("<TITLE>{0}</TITLE>", arg));
            sw.WriteLine("</head>");

            sw.WriteLine("<body>");
            sw.WriteLine(string.Format("<span style=\"margin:10%; Color:blue\">{0}</span>", VERSION));
            sw.WriteLine("<H1 style=\"font-size:xx-large; text-align:center\">");

            sw.WriteLine(string.Format("{0} <br /><br />", arg));
            sw.WriteLine(string.Format("{0} file{2} at {1}</H1>",
                count, DateTime.Now.ToString("G"), count == "1" ? "" : "s"));
            sw.WriteLine("<pre><B>");
            }


SOURCE CODE

Source Code Files

For help, email me at b a r n o l d @ i e e e . o r g
Back to C++ Main Page