( Index )
Month

Brief Information about the January '10 CSIG Meeting

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

by Bruce Arnold
SEE   ALSO
Ron Winters C6 Video Demo


Files.exe     Sample

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 HTML 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. For a comparison see the FILES program from December 2001.
http://acgnj.barnold.us/Csig0112.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: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { sender; e; UInt64 totalBytes = 0; folderBrowserDialog1->ShowNewFolderButton = false; folderBrowserDialog1->RootFolder = System::Environment::SpecialFolder::Desktop; folderBrowserDialog1->SelectedPath = System::Environment::GetFolderPath(System::Environment::SpecialFolder::MyDocuments); if ( folderBrowserDialog1->ShowDialog() == Windows::Forms::DialogResult::OK) { int i=0; // // The user selected a folder and pressed the OK button. // try { array<String^>^ fileEntries = IO::Directory::GetFiles(folderBrowserDialog1->SelectedPath); IEnumerator^ files = fileEntries->GetEnumerator(); DoOpenTempFile(BROWSER_SOURCE_FILE); DoHeader(folderBrowserDialog1->SelectedPath, fileEntries->Length.ToString()); while ( files->MoveNext() ) { // Note: the safe_cast creates Verifiable MSIL. See reference at end. // FileInfo ^fi = gcnew FileInfo(safe_cast<String ^>(files->Current)); DoLineItem( String::Format(" {0,-24} {1,15:0,0} {2}", (fi->LastWriteTime).ToString("G"), fi->Length, fi->Name)); totalBytes += fi->Length; ++i; if (!(i%3)) DoHorizontalLine(); } DoFooter(totalBytes); DoShowBrowser(tempPath); } catch(...) { ; // no special action. } } // end if result ok } // ******************************************************** // ****** DoOpenTempFile // ******************************************************** private: System::Void DoOpenTempFile(String ^arg) { tempPath = System::IO::Path::GetTempPath(); tempPath += arg; sw = gcnew StreamWriter(tempPath); // NEED TRY CATCH } // ******************************************************** // ****** DoHeader // ******************************************************** private: System::Void DoHeader(String ^arg, String ^count) { sw->WriteLine("<HTML>"); sw->WriteLine("<HEAD>"); 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("<H1><FONT COLOR=\"0000FF\">    Files </FONT></H1>"); sw->WriteLine(String::Format("<H2 ALIGN=center>{0}</H2>", arg)); sw->WriteLine(String::Format("<H4 ALIGN=center>{0} file{2} at {1}</H4>", 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