locked
Custom merge text files RRS feed

  • Question

  • User-1231525838 posted

    Dear All,

    i am new to entering this sotware coding and i will expect guidelines to get solutions for our concern request. Please find below is request and can any one please help me to get soultion.

    We need to copy text file informations based on the given "Y".

    In below example, 00001.txt need to copy 00001 to 00005 i.e. start "Y" flag to untill next "Y" flag.

    Input text file:

    X:\ED0942_Export for LDD\IMAGES\001\JPMBSCI00001.txt,Y
    X:\ED0942_Export for LDD\IMAGES\001\JPMBSCI00002.txt,,
    X:\ED0942_Export for LDD\IMAGES\001\JPMBSCI00003.txt,,
    X:\ED0942_Export for LDD\IMAGES\001\JPMBSCI00004.txt,,
    X:\ED0942_Export for LDD\IMAGES\001\JPMBSCI00005.txt,,
    X:\ED0942_Export for LDD\IMAGES\001\JPMBSCI00006.txt,Y
    X:\ED0942_Export for LDD\IMAGES\001\JPMBSCI00007.txt,Y
    X:\ED0942_Export for LDD\IMAGES\001\JPMBSCI00008.txt,,
    X:\ED0942_Export for LDD\IMAGES\001\JPMBSCI00009.txt,,
    X:\ED0942_Export for LDD\IMAGES\001\JPMBSCI00010.txt,,
    X:\ED0942_Export for LDD\IMAGES\001\JPMBSCI00011.txt,Y
    X:\ED0942_Export for LDD\IMAGES\001\JPMBSCI00012.txt,,
    X:\ED0942_Export for LDD\IMAGES\001\JPMBSCI00013.txt,,
    X:\ED0942_Export for LDD\IMAGES\001\JPMBSCI00014.txt,,
    X:\ED0942_Export for LDD\IMAGES\001\JPMBSCI00015.txt,Y
    X:\ED0942_Export for LDD\IMAGES\001\JPMBSCI00016.txt,,
    X:\ED0942_Export for LDD\IMAGES\001\JPMBSCI00017.txt,,

    Please help me ..... I will expect your examples from ASP.NET 2010.

    Have a great day !!!

    Thursday, January 31, 2013 8:04 AM

Answers

  • User281315223 posted

    The following should do exactly what you need and will yield a collection of grouped file names as you have mentioned. I'm sure there are much more efficient methods of handling it, but I wanted to use a very basic example that explained what was going on (so that if you wanted to use a different approach, you could use some of the same ideas.) 

    string fileContents;
    
    //Your file stream to read your file
    using (FileStream stream = new FileStream("yourInputFile.txt", FileMode.Open))
    {
           fileContents = new StreamReader(stream).ReadToEnd();
    }
    
    //Stores each of the lines
    string[] split = fileContents.Split(new string[]{Environment.NewLine},StringSplitOptions.RemoveEmptyEntries);
    
    //This will group each of your Lists of file names
    List<List<string>> mappings = new List<List<string>>();
    //This will store a temporary list of the current grouping of files
    List<string> currentLines = new List<string>();
    //Toggles when a new set as began
    bool started = false;
    
    //Iterates through each of the lines
    foreach (string line in split)
    {
           //If this is the first to start a set
           if(line.EndsWith("Y") && !started)
           {
                   //Start the set (toggles)
                   started = !started;
    
                   //Adds the current line to the current set
                   currentLines.Add(line.Replace(",", "").TrimEnd('Y'));
           }
           //If this set should end
           else if (line.EndsWith("Y") && started)
           {
                   //Clear the current lines and add them to the mapping
                   mappings.Add(currentLines);
                   currentLines = new List<string>();
    
                   //Adds the current line to the current set
                   currentLines.Add(line.Replace(",", "").TrimEnd('Y'));
           }
           else
           {
                   //Adds the current line to the current set
                   currentLines.Add(line.Replace(",", "").TrimEnd('Y'));
           }
    }
    
    //Adds any remaining items
    mappings.Add(currentLines);
    
    //Now you can just iterate through your groupings, which will contain all of your files
    foreach(List<string> filesInGroup in mappings)
    {
          //Logic to merge text files here
    }
    


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 31, 2013 9:06 AM

All replies

  • User281315223 posted

    The following should do exactly what you need and will yield a collection of grouped file names as you have mentioned. I'm sure there are much more efficient methods of handling it, but I wanted to use a very basic example that explained what was going on (so that if you wanted to use a different approach, you could use some of the same ideas.) 

    string fileContents;
    
    //Your file stream to read your file
    using (FileStream stream = new FileStream("yourInputFile.txt", FileMode.Open))
    {
           fileContents = new StreamReader(stream).ReadToEnd();
    }
    
    //Stores each of the lines
    string[] split = fileContents.Split(new string[]{Environment.NewLine},StringSplitOptions.RemoveEmptyEntries);
    
    //This will group each of your Lists of file names
    List<List<string>> mappings = new List<List<string>>();
    //This will store a temporary list of the current grouping of files
    List<string> currentLines = new List<string>();
    //Toggles when a new set as began
    bool started = false;
    
    //Iterates through each of the lines
    foreach (string line in split)
    {
           //If this is the first to start a set
           if(line.EndsWith("Y") && !started)
           {
                   //Start the set (toggles)
                   started = !started;
    
                   //Adds the current line to the current set
                   currentLines.Add(line.Replace(",", "").TrimEnd('Y'));
           }
           //If this set should end
           else if (line.EndsWith("Y") && started)
           {
                   //Clear the current lines and add them to the mapping
                   mappings.Add(currentLines);
                   currentLines = new List<string>();
    
                   //Adds the current line to the current set
                   currentLines.Add(line.Replace(",", "").TrimEnd('Y'));
           }
           else
           {
                   //Adds the current line to the current set
                   currentLines.Add(line.Replace(",", "").TrimEnd('Y'));
           }
    }
    
    //Adds any remaining items
    mappings.Add(currentLines);
    
    //Now you can just iterate through your groupings, which will contain all of your files
    foreach(List<string> filesInGroup in mappings)
    {
          //Logic to merge text files here
    }
    


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 31, 2013 9:06 AM
  • User-1231525838 posted

    Dear  Rion,

    Thanks you very much.

    I really tried in past one month and i was not identify to reach my solution. I am new to software coding. Please give me any further idea or provide sample application example with source code AEAP.

    I am awaiting your reply.

    Once again thanks for your great support to us....:)

    Regards,

    Ganesh

    Wednesday, March 20, 2013 11:32 AM
  • User281315223 posted

    Could you PM me a copy of what your text file might look like?

    I remember recreating this same example using some of the test input that you provided along with the post I posted above and it seemed to work as intended (at least to group the files themselves).

    Is the issue that you are having actually merging the documents themselves? Because that would be a different bit of logic entirely.

    Wednesday, March 20, 2013 11:37 AM
  • User-1231525838 posted

    Hi Rion,

    Thansk for your reply...!!!

    Please find below is used in my Visual Studio forms. I was not get meged text file. Please give me the step by step action to do...

    =====Merger.aspx.cs===============
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.IO;
    using System.Text;
    
    
    public partial class merger : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            string fileContents;
    
            //Your file stream to read your file
            using (FileStream stream = new FileStream(@"C:\TEST PROJECT\Opticon.txt", FileMode.Open))
            {
                fileContents = new StreamReader(stream).ReadToEnd();
            }
    
            //Stores each of the lines
            string[] split = fileContents.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
    
            //This will group each of your Lists of file names
            List<List<string>> mappings = new List<List<string>>();
            //This will store a temporary list of the current grouping of files
            List<string> currentLines = new List<string>();
            //Toggles when a new set as began
            bool started = false;
    
            //Iterates through each of the lines
            foreach (string line in split)
            {
                //If this is the first to start a set
                if (line.EndsWith("Y") && !started)
                {
                    //Start the set (toggles)
                    started = !started;
    
                    //Adds the current line to the current set
                    currentLines.Add(line.Replace(",", "").TrimEnd('Y'));
                }
                //If this set should end
                else if (line.EndsWith("Y") && started)
                {
                    //Clear the current lines and add them to the mapping
                    mappings.Add(currentLines);
                    currentLines = new List<string>();
    
                    //Adds the current line to the current set
                    currentLines.Add(line.Replace(",", "").TrimEnd('Y'));
                }
                else
                {
                    //Adds the current line to the current set
                    currentLines.Add(line.Replace(",", "").TrimEnd('Y'));
                }
            }
    
            //Adds any remaining items
            mappings.Add(currentLines);
    
            //Now you can just iterate through your groupings, which will contain all of your files
            foreach (List<string> filesInGroup in mappings)
            {
                //Logic to merge text files here
                
            }
        }
    }

    ==============merger.aspx=================

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="merger.aspx.cs" Inherits="merger" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <p>
            <br />
        </p>
        <p>
            TEXT FILE MERGER</p>
        <form id="form1" runat="server">
        <div>
        
            
            <asp:Button ID="ReadText" runat="server" onclick="Button1_Click"
                Text="ReadText" />
            
        
        </div>
        </form>
    </body>
    </html>

    Regards,

    Ganesh



    Thursday, March 21, 2013 7:13 AM