Brief Information about the September 16th CSIG Meeting
Fsearch.cs - [1.51] Console App for File Searching Whole Hard Drive by B. Arnold
(General Info) In this latest iteration of the program that we started before Summer break, the logic has been finetuned to make it more useful from both command box and Windows usage. Remember that the goal (or object) is to scan the entire C:\Users drive (or whatever) and locate any file whose name includes the given string. All targets are printed to the system STDOUT and may be redirected to a file. Because this is a console type program, the speed is quite spectacular. Error handling and Recursion topics are also covered. The program uses DOTNET library functions and may easily be converted to C++. See the flow chart below. At the meeting, we will be discussing the logic and the coding of the program.
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!)
This month's program includes the following.
This is a CLI DOT
NET program is coded using C-sharp (C#) and is very similar to C++. It
also still uses the
same DOT NET libraries.
The program uses a number of DOT NET Library functions including the following:
errorWriter | ReadLine | Try and Catch | Console I/O |
TextWriter | DirectoryInfo | Exceptions | Main |
WriteLine | EnumerateFiles | Recursive | ToString |
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 4.5 |
C++ 9.0, etc. |
.Net 4.5, etc. |
CLI |
Common Language Infrastructure |
Sample Code
// Fsearch.cs "FILE SEARCH" B.Arnold 20-May-2014
// Object: to scan the entire C:\Users drive and locate any file whose name includes
// the given string. All targets are printed to the system STDOUT and may be
// redirected to a file. Because this is a console type program, the speed is
// quite spectacular. Error handling and Recursion topics are also covered.
// The program uses DOTNET library functions and may easily be converted to C++.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Security;
namespace Fsearch
{
class Program
{
static string version =
" FSEARCH search_string --- Version 1.00 by B.Arnold May 2014 \n" +
" =============================================================== \n" +
" Wildcard specifiers \n" +
" * --- zero or more characters in that position. \n" +
" ? --- zero or one character in that position. \n" +
" anything else is considered a literal character. \n";
static string mQuery;
static int count, dircount;
static void Main(string[] args)
{
mQuery = "*";
count = dircount = 0;
if (args.Count() > 0) // Console.WriteLine(args[0]);
{
mQuery = args[0].ToString();
Process("C:\\Users"); // root
}
else
Console.WriteLine(version);
TextWriter errorWriter = Console.Error;
errorWriter.WriteLine("\n ==> " + dircount + " <
== Directories Scanned. \n");
errorWriter.WriteLine("\n ==> " + count + " <
== Filenames Listed. (Press Enter)");
Console.ReadLine(); //Wait for user
}
static void Process(string root)
{
if (!root.EndsWith("\\")) root += "\\";
DirectoryInfo mPath = new DirectoryInfo(root); ++dircount;
// Scan the files in this directory.
// --------------------------------
// MS: "EnumerateFiles can be more efficient than GetFiles for large tasks."
try
{
foreach (var mFile in mPath.EnumerateFiles(
mQuery, SearchOption.TopDirectoryOnly))
{
Console.WriteLine(mFile.FullName); ++count;
}
}
//// Land here if EnumerateFiles fails ...
catch (DirectoryNotFoundException dnf) { ; }
//Console.WriteLine("Not Found Exception"); }
catch (SecurityException se) { ; }
//Console.WriteLine("Security Exception"); }
catch (UnauthorizedAccessException uae) { ; }
//Console.WriteLine("Unauthorized Access Exception");
// Scan the Folders in this directory.
// --------------------------------
try
{
foreach (var mDirName in mPath.EnumerateDirectories(
"*.*", SearchOption.TopDirectoryOnly))
{
Process(mDirName.FullName); // RECURSIVE !!!
}
}
//// Land here if EnumerateFiles fails ...
catch (DirectoryNotFoundException dnf) { ; }
//Console.WriteLine("Dir Not Found Exception"); }
catch (SecurityException se) { ; }
//Console.WriteLine("Dir Security Exception"); }
catch (UnauthorizedAccessException uae) { ; }
//Console.WriteLine("Dir Unauthorized Access Exception"); }
}
}
}
SOURCE CODE
For help, email me at "b a r n o l d @ i e e e . o r g".Back to C++ Main Page