Google Answers Logo
View Question
Q: Save to file/log using delimiters as separator -- GA ( Answered 5 out of 5 stars,   0 Comments )
Subject: Save to file/log using delimiters as separator -- GA
Category: Computers > Programming
Asked by: amy123456-ga
List Price: $50.00
Posted: 20 Jan 2004 06:19 PST
Expires: 19 Feb 2004 06:19 PST
Question ID: 298346
The extraction that we just did on the orders, i need it to go to a
file so i can archive for further use. I need it to be saved as

F_Name/L_Name/Loc/Room #/

Save to file/log using delimiters as separator, all the events to go to one file.

Thank you

Request for Question Clarification by mathtalk-ga on 20 Jan 2004 16:13 PST
Hi, Amy:

Does it make sense to do this in the same subroutine as before, or
would a separate subroutine be better for your purposes?

Would you like to be able to pass in the logfile name and the
delimiter character, or would you prefer that I choose a name and
hard-coded "/" as the delimiter for the time being?

regards, mathtalk-ga

Clarification of Question by amy123456-ga on 21 Jan 2004 09:35 PST
1-right now we are writing to console, can we make it go to the save logfile.

2- Would you like to be able to pass in the logfile name and the
delimiter character,?  Yes  Please, i think that might work better. We
need to be able to pull the info later when we need to turn off the

Thank you very much
Subject: Re: Save to file/log using delimiters as separator -- GA
Answered By: mathtalk-ga on 22 Jan 2004 21:04 PST
Rated:5 out of 5 stars
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.");

    /// <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))
             "Error: Specified file {0} does not exist.",


      StreamReader sr = File.OpenText(filename);
      while(sr.Peek() > -1)
        if (Regex.IsMatch(sr.ReadLine(), "Order Session"))
      if (sr.Peek() == -1)
             "Error: Order Session header was not found.");

      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);

amy123456-ga rated this answer:5 out of 5 stars
Excellent work, and on a timely fashion.

There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  

Google Home - Answers FAQ - Terms of Service - Privacy Policy