Hi, Amy:
As you probably know, the site has been down intermittently in the
past day or so, and I've been unable to get this out to you as fast as
I would have liked.
I've compiled and tested my modifications to the earlier code, adding
the two arguments to the subroutine ListItems for the name of the
logfile and the delimiter to use between extracted items. The
complete revised program is below.
You may not be familiar with the @"..." literal string construction I
used to pass in the logfile name in the outer function, but this C#
feature allows one to use backslashes simply as backslashes (without
quoting any characters except for double quotes). This is especially
convenient for expressing file paths in the DOS/Windows environment,
which can involve lots of backslashes.
Writing to the logfile itself is a matter of invoking the StreamWriter
constructor with a Boolean argument that says "append" to an existing
file if one exists (otherwise create the file).
Good luck with your project, and let me know if you have any questions
about how the revised code does the new tasks you asked about.
regards, mathtalk
using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
namespace textextract
{
/// <summary>
/// Main Class TxtXtrct for text extract console app.
/// </summary>
class TxtXtrct
{
public static void Main(string[] args)
{
if (args.Length == 0)
{
Console.WriteLine("You must specify a filename.");
}
else
{
TxtXtrct.ListItems(args[0],@"C:\txtxtrct.log","/");
}
return;
}
/// <summary>
/// Method for extracting text items from file
/// </summary>
/// <param name="filename">root/path/filename.ext</param>
/// <param name="logfile">root/path/logfile.ext</param>
/// <param name="delimit">delimiter string</param>
static void ListItems(string filename, string logfile, string delimit)
{
if (!File.Exists(filename))
{
Console.WriteLine(
"Error: Specified file {0} does not exist.",
filename);
return;
}
StreamReader sr = File.OpenText(filename);
while(sr.Peek() > -1)
{
if (Regex.IsMatch(sr.ReadLine(), "Order Session"))
break;
}
if (sr.Peek() == -1)
{
Console.WriteLine(
"Error: Order Session header was not found.");
sr.Close();
return;
}
while (sr.Peek() > -1)
{
string extractLine = sr.ReadLine();
if (Regex.IsMatch(extractLine, ","))
{
// found the line with the items of interest
// the last name is the trimmed part in front of the comma
string lastname = extractLine.Substring(
0, extractLine.IndexOf(",")).Trim();
Console.WriteLine("Last name: " + lastname);
// the first name is trimmed next part up to triple space
string extract1 = extractLine.Substring(
extractLine.IndexOf(",") + 1);
string firstname = extract1.Substring(
0, extract1.IndexOf(" ")).Trim();
Console.WriteLine("First name: " + firstname);
// the location is next after first name
string extract2 = extract1.Substring(
extract1.IndexOf(firstname + " ")
+ firstname.Length ).Trim();
string location = extract2.Substring(
0, extract2.IndexOf(" ")).Trim();
Console.WriteLine("Location: " + location);
// the last item room number is next, up to a dash
string extract3 = extract2.Substring(
extract2.IndexOf(location + " ")
+ location.Length ).Trim();
string roomnumber = extract3.Substring(
0, extract3.IndexOf("-")).Trim();
Console.WriteLine("Room number: " + roomnumber);
string textToWrite = firstname + delimit
+ lastname + delimit
+ location + delimit
+ roomnumber + delimit
+ "\r\n";
StreamWriter swLogFileAppend = new StreamWriter(logfile,true);
swLogFileAppend.Write(textToWrite);
swLogFileAppend.Flush();
swLogFileAppend.Close();
break;
}
}
sr.Close();
return;
}
}
} |