banner



How To Split Large Text File In Windows

  • Remove From My Forums

 locked

How can i split large size text file to many small text files ?

  • Question

  • In this lawmaking sample im retriving text from a text file specific text and write it in to a new text file.

    Now i desire for instance after the new text file is finished to be written w.Shut();

    I desire to bank check that if the new text file size is over lets say 30kb then carve up the file to ane file of 30kb and the balance in another file.

    And if its 300kb the new text file then split it to x files each of 30kb. Allways the start file 30kb and rest as much every bit needded.

    I also want that it volition make tha for example splitted file 000001.txt will contain the text from the beginning file 000002.txt will kickoff from the end of the starting time file and file number 000003.txt will offset from where 000002.txt ended.

    Then i want in the stop im doing something with the files and i desire in the cease to merge them all dorsum to one big file in the order of 000001 then merge to information technology 000002 into the terminate of 000001 and and so on so in the cease sick take a new text file back with all the text as information technology was in the beginning.

    This is the retriving text code:

                        private                    void                    retrivingText()         {                    string                    startTag =                    "<Text>";                    string                    endTag =                    "</Text>";                    int                    startTagWidth = startTag.Length;                    int                    endTagWidth = endTag.Length;              index = 0;             westward =                    new                    StreamWriter(@"d:\retrivedText.txt");                    while                    (truthful)                {                  index = f.IndexOf(startTag, index);                    if                    (index == -one)                    {                    break;                    }                    // else more to practice - index now is positioned at offset character of startTag                                        int                    start = index + startTagWidth;                 index = f.IndexOf(endTag, start+1);                    if                    (alphabetize == -1)                  {                    interruption;                   }                    // institute the endTag                                        string                    thousand = f.Substring(starting time, alphabetize - start);               westward.WriteLine(g);               }             w.Close();         }                  

    Thanks.


    danieli

Answers

  • ... something to showtime with.

    Note: this is more or less air code, its written in a rush (for I must exercise some other things now out of the house...) and non really tested, so there may be ameliorate ways to do this and there surely will be a better error-treatment exist possible:

                          public fractional grade Form1 : Form     {         public Form1()         {             InitializeComponent();         }          private void button1_Click(object sender, EventArgs e)         {             cord largeFile = @"C:\Users\...\Desktop\a.txt";             string savePath = @"C:\Users\...\Desktop\SplittedFiles";             bool b = SplitFiles(largeFile, 50, savePath);         }                   private void button2_Click(object sender, EventArgs eastward)         {             string savePath = @"C:\Users\...\Desktop\SplittedFiles";             string newLargeFile = @"C:\Users\...\Desktop\SplittedFiles\AAAA.txt";             bool b = CombineFiles(newLargeFile, savePath);         }          private bool SplitFiles(cord largeFile, int numLines, string savePath)         {             bool bRet = fake;             if (File.Exists(largeFile))             {                 StreamReader sr = zilch;                 StreamWriter sw = null;                 try                 {                     Directory.CreateDirectory(savePath);                      sr = new StreamReader(largeFile);                      int i = 0;                      while (sr.Peek() != -1)                     {                         sw = new StreamWriter(Path.Combine(savePath, "File" + i.ToString("D4") + ".txt"));                         sw.AutoFlush = truthful;                          for (int j = 0; j < numLines; j++)                         {                             sw.WriteLine(sr.ReadLine());                              if (sr.Peek() == -ane)                                 suspension;                         }                          i++;                          sw.Close();                     }                      bRet = true;                 }                 catch                 {                     bRet = false;                 }                 finally                 {                     sr.Shut();                     sr = aught;                      sw.Close();                     sw = null;                 }             }              return bRet;         }          private bool CombineFiles(cord newLargeFile, string savePath)         {             bool bRet = faux;              if (Directory.Exists(savePath))             {                 StreamReader sr = null;                 StreamWriter sw = null;                  try                 {                     Listing<FileInfo> fList = new List<FileInfo>();                     fList.AddRange(new DirectoryInfo(savePath).GetFiles("*.txt").Where(a => a.FullName.ToLower().Equals(newLargeFile.ToLower()) == false).ToArray());                     fList = fList.OrderBy(a => a.Name).ToList();                      sw = new StreamWriter(newLargeFile);                     sw.AutoFlush = true;                      foreach (FileInfo fi in fList)                     {                         sr = new StreamReader(fi.FullName);                         sw.Write(sr.ReadToEnd());                         sr.Close();                     }                 }                 catch                 {                     bRet = faux;                 }                 finally                 {                     sr.Shut();                     sr = null;                      sw.Shut();                     sw = zip;                 }             }              return bRet;         }     }

    Regards,

      Thorsten

    • Edited by Sabbatum, October xv, 2011 10:28 AM
    • Marked as respond by chocolade Saturday, October 15, 2011 eleven:sixteen AM
  • ... and here's a size-based method that first reads the complete file:

                            individual                        void                        button1_Click(object                        sender, EventArgs due east)         {                        string                        largeFile =                        @"C:\Users\...\Desktop\a.txt";                        string                        savePath =                        @"C:\Users\...\Desktop\SplittedFiles";                        bool                        b = SplitFiles(largeFile, 1024 * 16, savePath);         }                        private                        bool                        SplitFiles(string                        largeFile,                        int                        preferredSize,                        string                        savePath)         {                        bool                        bRet =                        simulated;                        if                        (File.Exists(largeFile))             {                 StreamReader sr =                        null;                 StreamWriter sw =                        nil;                        effort                        {                     Directory.CreateDirectory(savePath);                      sr =                        new                        StreamReader(largeFile);                        char[] allchars = sr.ReadToEnd().ToCharArray();                     sr.Close();                        int                        i = 0;                        int                        position = 0;                        while                        (position < allchars.Length)                     {                        int                        fifty = Math.Min(preferredSize, allchars.Length - position);                         sw =                        new                        StreamWriter(Path.Combine(savePath,                        "File"                        + i.ToString("D4") +                        ".txt"));                         sw.AutoFlush =                        true;                        char[] buffer =                        new                        Char[50];                         Array.Copy(allchars, position, buffer, 0, l);                         sw.Write(buffer);                          position += l;                          i++;                          sw.Close();                          bRet =                        truthful;                     }                 }                        take hold of                        {                     bRet =                        faux;                 }                        finally                        {                     sr.Close();                     sr =                        nada;                      sw.Close();                     sw =                        null;                 }             }                        return                        bRet;         }                      

    Regards,

      Thorsten

    • Marked equally answer by chocolade Saturday, October xv, 2011 10:20 AM

Source: https://social.msdn.microsoft.com/Forums/windows/en-US/4d353558-6a95-472c-b142-33e44034cb7d/how-can-i-split-large-size-text-file-to-many-small-text-files-?forum=winforms

Posted by: voexill1984.blogspot.com

0 Response to "How To Split Large Text File In Windows"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel