Hi experts
I'm looking for help to split a large text file into multiple text files based on size of the File.
Can anybody help me in this
Hi experts
I'm looking for help to split a large text file into multiple text files based on size of the File.
Can anybody help me in this
How to Split text file into multiple text files?
AronW
But what size should each file be Whatever that size is, set the maxFileSize to that, and run my code, and it will automatically split the file for you into the directory specified in destFileLocation.
hanjg82
It would have been useful to know that you needed intact lines upfront.
Well, basically you'd rewrite it to use StreamReader and ReadLine, then for each line you'd have to decide if the line would take your file beyond it's max size, and if it didn't write it to the file, and if it did, start a new file.
SP534
Thankq Got
But I'm reading lines from the text file
This method , splitting the line and place the truncated one in other file
Nick Sheng
Some more information about your requirements would help. What exactly do you mean when you say "based on size of the File" If you wanted each file to be a maximum of a certain size you could do something like:
string sourceFileName = @"C:\VS2005 SP1.exe";
string destFileLocation = @"C:\";
int index = 0;
long maxFileSize = 52428800;
byte[] buffer = new byte[65536];
using (Stream source = File.OpenRead(sourceFileName))
{
while (source.Position < source.Length)
{
index++;
// Create a new sub File, and read into t
string newFileName = Path.Combine(destFileLocation, Path.GetFileNameWithoutExtension(sourceFileName));
newFileName += index.ToString() + Path.GetExtension(sourceFileName);
using (Stream destination = File.OpenWrite(newFileName))
{
while (destination.Position < maxFileSize)
{
// Work out how many bytes to read
int bytes = source.Read(buffer, 0, (int) Math.Min(maxFileSize, buffer.Length));
destination.Write(buffer, 0, bytes);
// Are we at the end of the file
if (bytes < Math.Min(maxFileSize, buffer.Length))
{
break;
}
}
}
}
}
Karl Erickson
tdtdtd
Thanks for the reply Sean Hederman
Suppose if I've text(.txt) file more than 500KB , I want to split it into muliple files.
Ex :
temp.txt is file with 1209KB size
Now the result should be
temp1.txt
temp2.txt
temp3.txt
swathi_challa
can u tell the maxSize field here, what it is,
I want to split at 500KB each file.
If the main file exceeds 500KB then I want to split it.
Rahul Garg
Well, 500KB is 500x1024 bytes which means the maximum file size should be 512000.