10 Replies - 3345 Views - Last Post: 22 April 2010 - 10:23 AM Rate Topic: -----

#1 jhoughton   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 02-April 10

Copy block of text from file

Posted 16 April 2010 - 08:56 AM

I'm working on a new program, but I'm not even sure where to start.

I need to read an .ini file, and if a line contains [TEST] then copy every line after that up to [TESTEND] to another file.

Does this even seem possible or am I wasting my time?

P.S. I'm fairly new to C# still, and I know I have no code to post. I'm looking for proof of concept and a point in the right direction.
Is This A Good Question/Topic? 0
  • +

Replies To: Copy block of text from file

#2 SarumanTheWhite   User is offline

  • D.I.C Regular

Reputation: 74
  • View blog
  • Posts: 352
  • Joined: 04-November 08

Re: Copy block of text from file

Posted 16 April 2010 - 09:14 AM

I'm pretty sure it's possible. .INI files is just text. If I were to start, I would scan the file line by line, and if a particular line contains that phrase, start building a string from information after it, so keep on scanning and appending data to the string until you run across the TESTEND, then everything after TESTEND in the line is thrown away. Then do with that result string what you will.

I don't know if there's a library in C# that handles INI exclusively, but that's the way I would try first.
Was This Post Helpful? 0
  • +
  • -

#3 MentalFloss   User is offline

  • .
  • member icon

Reputation: 619
  • View blog
  • Posts: 1,590
  • Joined: 02-September 09

Re: Copy block of text from file

Posted 16 April 2010 - 09:50 AM

Piece of cake.

Create a System.IO.StreamReader and a System.Text.StringBuilder. Open the file with the StreamReader and read each line. If it contains your token "[TEST]", then you store each line after that to your StringBuilder until the line read in contains "[TESTEND]". At which point, you return that StringBuilder data via ToString()'s method.

Have fun.

EDIT: Oh yeah, the second part.

Once you've gotten the returned data, use a System.IO.StreamWriter object to output that text to a file.

This post has been edited by MentalFloss: 16 April 2010 - 09:51 AM

Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1663
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Copy block of text from file

Posted 16 April 2010 - 10:04 AM

I'm in the middle of a blog post on parsing an INI file with C# so stay tuned
Was This Post Helpful? 0
  • +
  • -

#5 jhoughton   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 02-April 10

Re: Copy block of text from file

Posted 16 April 2010 - 10:08 AM

View PostMentalFloss, on 16 April 2010 - 08:50 AM, said:

Piece of cake.

Create a System.IO.StreamReader and a System.Text.StringBuilder. Open the file with the StreamReader and read each line. If it contains your token "[TEST]", then you store each line after that to your StringBuilder until the line read in contains "[TESTEND]". At which point, you return that StringBuilder data via ToString()'s method.

Have fun.

EDIT: Oh yeah, the second part.

Once you've gotten the returned data, use a System.IO.StreamWriter object to output that text to a file.

Ok, I'm familiar with tokens from batch files and again I'm fairly new to c sharp. I can't find anything in my docs about tokens. On the net all I can find are people using String.Split. Is that what you're talking about?

View PostPsychoCoder, on 16 April 2010 - 09:04 AM, said:

I'm in the middle of a blog post on parsing an INI file with C# so stay tuned


That would be awesome. Will you post it today? This project isn't a time critical thing, I'm just the type to go nuts until I figure something out.

This post has been edited by jhoughton: 16 April 2010 - 10:09 AM

Was This Post Helpful? 0
  • +
  • -

#6 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1663
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Copy block of text from file

Posted 16 April 2010 - 11:08 AM

Check this out:

Parse an INI file with C# & P/Invoke
Was This Post Helpful? 0
  • +
  • -

#7 jhoughton   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 02-April 10

Re: Copy block of text from file

Posted 16 April 2010 - 11:29 AM

View PostPsychoCoder, on 16 April 2010 - 12:08 PM, said:



Interesting read, but way over my head I'm afraid :(
Was This Post Helpful? 0
  • +
  • -

#8 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1663
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Copy block of text from file

Posted 16 April 2010 - 11:32 AM

You get better by tackling things you think are over your head. Is this a homework assignment by the way where you have to follow a specific task?
Was This Post Helpful? 0
  • +
  • -

#9 jhoughton   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 02-April 10

Re: Copy block of text from file

Posted 16 April 2010 - 11:45 AM

No my company works with a lot of dbf and ini files for our databases. Currently when it comes to getting information from ini files we use batch files, I'm trying to move us away from this method of data retrieval. This tends to leave hundreds of batch files spread across our servers in the field. While limiting the amount of people that can use, write, understand them. If one of them breaks for whatever reason, it's a nightmare.

(I won't even get into how we were getting information from the dbf files before I started this)

By building a program, I'm attempting to lump everything into one program (using tab control or multiple forms) saving time but more importantly making it easy to hire a temp worker to come in, connect to the server, and click a button for our data retrievals.

I decided to learn c# since this is what our vendor's software is written in and our servers are built around that with .net frameworks. I'm still pretty new to it, but at least I have the general understanding of the basics right now

This post has been edited by jhoughton: 16 April 2010 - 11:50 AM

Was This Post Helpful? 0
  • +
  • -

#10 jhoughton   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 02-April 10

Re: Copy block of text from file

Posted 20 April 2010 - 12:18 PM

Alright, so this is where I'm at with this. Not even close to what you guys were trying to tell me but it's working so far. My only problem is getting it to stop.

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Tools
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click_1(object sender, EventArgs e)
        {

            string fileopen = @"C:\Users\Josh\Desktop\TEST.ini";
            string filewrite = @"C:\Users\Josh\Desktop\Test.txt";
            StreamReader readfile = new StreamReader(fileopen);
            StreamWriter writefile = new StreamWriter(filewrite);

            string fileread = "";
            try
            {

                using (readfile)
                {
                    fileread = readfile.ReadToEnd();
                }


                int i = fileread.IndexOf("[TEST]");

                string d = fileread.Substring(i);

                writefile.Write(d);
                writefile.Close();
                readfile.Close();

                MessageBox.Show("Complete!");
            }
            catch (Exception)
            {

            }
        }
    }
}


Here's what my Test.ini has in it:
NOCOPY
NOCOPY
NOCOPY
NOCOPY
NOCOPY
[TEST]
COPY1
COPY2
COPY3
COPY4
COPY5
[TESTEND]
NOCOPY
NOCOPY
NOCOPY
NOCOPY
NOCOPY

Now with the code I'm using, the output is like this:
[TEST]
COPY1
COPY2
COPY3
COPY4
COPY5
[TESTEND]
NOCOPY
NOCOPY
NOCOPY
NOCOPY
NOCOPY

What I would like to it to output is:

[TEST]
COPY1
COPY2
COPY3
COPY4
COPY5
[TESTEND]

What do I need to do to get it to stop at [TESTEND]? Any ideas?

PS. I know this looks rough and there are probably 100 better ways to accomplish what I'm doing, but this was all I could piece together with my limited knowledge.

This post has been edited by jhoughton: 20 April 2010 - 12:20 PM

Was This Post Helpful? 0
  • +
  • -

#11 MentalFloss   User is offline

  • .
  • member icon

Reputation: 619
  • View blog
  • Posts: 1,590
  • Joined: 02-September 09

Re: Copy block of text from file

Posted 22 April 2010 - 10:23 AM

public static class InitializationFileReader
{
    private static string startToken = "[TEST]";
    private static string stopToken = "[TESTEND]";

    public static IList<string> GetTextBlock(string path)
    {
        var text = new List<string>();

        if (String.IsNullOrEmpty(path) | !File.Exists(path))
        {
            throw new ArgumentException("Provided path is invalid");
        }
        else
        {
            try
            {
                using (StreamReader reader = new StreamReader(path))
                {
                    string line = String.Empty;
                    bool startTokenFound = false;
                    while ((line = reader.ReadLine()) != null)
                    {
                        if (line.Contains(startToken))
                            startTokenFound = true;

                        if (startTokenFound)
                            text.Add(line);

                        if (line.Contains(stopToken))
                            break;
                    }
                }
            }
            catch (IOException ioEx)
            {
                Console.WriteLine("Error processing file: {0}", ioEx.Message);
            }
        }
        return text;
    }
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1