473,536 Members | 2,993 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Split a text file into several based on output file size.

TJ
I am very new to C# (this is my first real project), therefore please be
patient if my question is considered being to newbie.

I am modifying a program which takes a text file, does some formatting then
outputs the resulting file. What I am trying to do is have the program make
as many files as is needed, each limited to a given file size (in this case
240KB).

As way of example say I have a text file which is 723KB (I get this by using
Fileinfo.length) named Test.txt. Program runs and does it's formatting and
is ready to save the output file. In this case the program should make the
following files:

Test1.txt (240KB)

Test2.txt (240KB)

Test3.txt (204KB)

Test4.txt (3KB)

Can anyone offer either in simple steps a solution or place to start to
perform this operation. Remember I am not trying to determine the size of
an existing file, but trying to output X number of files based on a given
maximum file size for each.

Thank you,

tj
Nov 16 '05 #1
1 10787
To clarify what you're trying to do:
- you want your application to take in the name of a text file
- another parameter you want is the max size of each "chunk"
- then you want your application to return how many "chunks" the text file
will be split into?
- presumably you want your application to perform this split?

The easiest (perhaps not the most efficient) way to do this would be to open
the file and read out the contents into a StreamReader.

int maxChunkSizeKB = 240; // size in kilobytes
int maxChunkSizeB = maxChunkSizeKB * 1024; // convert to bytes (==
characters)
string textFileName = @"C:\test.text";
StreamReader rdr = File.OpenText ( textFileName );
then take this file and place it into a string buffer

string fileBuffer = rdr.ReadToEnd();
rdr.Close();
now cut up the file into sizeable files

int fileCounter = 0;
while ( fileBuffer.Length > 0 )
{
//
// get this chunk contents

fileCounter++;
string fileChunkBuffer = "";
if ( fileBuffer.Length > maxChunkSizeB )
{
fileChunkBuffer = fileBuffer.SubString( 0, maxChunkSizeB );
fileBuffer = fileBuffer.SubString ( maxChunkSizeB );
}
else
{
fileChunkBuffer = fileBuffer;
fileBuffer = "";
}

//
// write this chunk file

string chunkFileName = textFileName + fileCounter.ToString();
StreamWriter wtr = File.CreateText ( chunkFileName );
wtr.Write( fileChunkBuffer );
wtr.Close();
}
The number of files that were created is therefore equal to the value of
fileCounter.
I didn't compile this so you may need to tweak it.

Hope that helps.

Daniel.

"TJ" <TJ@nospam.me> wrote in message
news:Oz**************@TK2MSFTNGP10.phx.gbl...
I am very new to C# (this is my first real project), therefore please be
patient if my question is considered being to newbie.

I am modifying a program which takes a text file, does some formatting
then outputs the resulting file. What I am trying to do is have the
program make as many files as is needed, each limited to a given file size
(in this case 240KB).

As way of example say I have a text file which is 723KB (I get this by
using Fileinfo.length) named Test.txt. Program runs and does it's
formatting and is ready to save the output file. In this case the program
should make the following files:

Test1.txt (240KB)

Test2.txt (240KB)

Test3.txt (204KB)

Test4.txt (3KB)

Can anyone offer either in simple steps a solution or place to start to
perform this operation. Remember I am not trying to determine the size of
an existing file, but trying to output X number of files based on a given
maximum file size for each.

Thank you,

tj

Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
3671
by: ross | last post by:
I want to do some tricky text file manipulation on many files, but have only a little programming knowledge. What are the ideal languages for the following examples? 1. Starting from a certain folder, look in the subfolders for all filenames matching *FOOD*.txt Any files matching in each folder should be copied to a new subfolder within...
2
6655
by: Jen | last post by:
Trying to take one table in access and split it into multiple excel files(using an excel template); and then email based on email addresses in Table2; Of course, I would like to do all of this with minimum user-interface...If there is anyone out there that can help... please feel free to share!:) PS... Using MSOffice 2000 Example:...
3
8550
by: Krish | last post by:
I have requirement, that i get one big chunk of text file. This text file will have has information, that on finding "****End of Information****", i have to split them individual text file with our naming standard (unique id) and create them designated folder. This requirement should be created as a batch job and preferrably this job should...
3
9647
by: Ben | last post by:
Hi I am creating a dynamic function to return a two dimensional array from a delimeted string. The delimited string is like: field1...field2...field3... field1...field2...field3... field1...field2...field3...
5
8690
by: Sen Haerens | last post by:
I'm using string.split(/^$/m, 2) on a curl output to separate header and body. There’s an empty line between them. ^$ doesn’t seem to work... Example curl output: HTTP/1.1 404 Not Found Date: Wed, 22 Feb 2006 00:01:45 GMT Server: Apache/1.3.33 (Darwin) PHP/5.1.2 mod_perl/1.29 Transfer-Encoding: chunked Content-Type: text/html;...
6
3422
by: py_genetic | last post by:
Hi, I'm looking to generate x alphabetic strings in a list size x. This is exactly the same output that the unix command "split" generates as default file name output when splitting large files. Example: produce x original, but not random strings from english alphabet, all lowercase. The length of each string and possible...
2
1964
by: hash4sp | last post by:
I have just started my career in programming. My Problem is reading a text file using VB.NET. The text contains the state names followed by respective city names. My task is to read the file and generate an html page for each state with ciites. File format is as follows: Andhra Pradesh Hyderabad Warangal
1
4184
by: littlealex | last post by:
IE6 not displaying text correctly - IE 7 & Firefox 3 are fine! Need some help with this as fairly new to CSS! In IE6 the text for the following page doesn't display properly - rather than being aligned to the top, along with the slideshow and link buttons, you have to scroll down to see the text - how can I make IE6 display correctly? ...
7
4372
by: John Smith | last post by:
Hi, I am very new to C# and NET framework. I am trying to hash (using MD5CryptoServiceProvider) a source that is split into several files. Now when the source is in one file I can produce the correct md5 hash. My issue is how can I reproduce the correct hash when the file is split into different files.
0
7359
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7289
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7680
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7273
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5821
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5211
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4841
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1752
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
570
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.