DPyro Posted October 7, 2013 Share Posted October 7, 2013 Need to loop through a directory and merge a series of binary files like this > File001, File002, File003 etc. I'm using the following code but need to count how many are in the folder then concat them: using (var fs = File.OpenWrite(output)) { var buffer = File.ReadAllBytes(file1); fs.Write(buffer, 0, buffer.Length); buffer = File.ReadAllBytes(file2); fs.Write(buffer, 0, buffer.Length); buffer = File.ReadAllBytes(file3); fs.Write(buffer, 0, buffer.Length); buffer = File.ReadAllBytes(file4); fs.Write(buffer, 0, buffer.Length); buffer = File.ReadAllBytes(file5); fs.Write(buffer, 0, buffer.Length); fs.Flush(); } Also need to split files in 1Mb or 3Mb chunks like this > File001, File002, File003. Link to comment Share on other sites More sharing options...
0 Andre S. Veteran Posted October 7, 2013 Veteran Share Posted October 7, 2013 Directory.GetFiles() gets you a string array containing all the file names in the specified directory. Its length is the number of files. To split in chunks of a specific size, well that's the third parameter to the FileStream.Write method you're using. Keep track of how many bytes you've written and open new files as needed. Link to comment Share on other sites More sharing options...
0 DPyro Posted October 7, 2013 Author Share Posted October 7, 2013 Directory.GetFiles() gets you a string array containing all the file names in the specified directory. Its length is the number of files. To split in chunks of a specific size, well that's the third parameter to the FileStream.Write method you're using. Keep track of how many bytes you've written and open new files as needed. How would the code for that look like? I don't want all files, just ones with filename00* foreach (var file in Directory.GetFiles(folderBrowserDialog1.SelectedPath, "*.*")) { var buffer = File.ReadAllBytes(file); fs.Write(buffer, 0, buffer.Length); } Link to comment Share on other sites More sharing options...
0 Andre S. Veteran Posted October 7, 2013 Veteran Share Posted October 7, 2013 I don't want all files, just ones with filename00* Directory.GetFiles().Where(path => Path.GetFileName(path).StartsWith("filename00")); Link to comment Share on other sites More sharing options...
0 DPyro Posted October 7, 2013 Author Share Posted October 7, 2013 What does the final code look like? Can't seem to get it working... EDIT: using (var fs = File.OpenWrite(output)) { foreach (var file in Directory.GetFiles(folderBrowserDialog1.SelectedPath).Where(path => Path.GetFileName(path).StartsWith("File00"))) { var buffer = File.ReadAllBytes(file); fs.Write(buffer, 0, buffer.Length); } fs.Flush(); } What should I be doing at var buffer? Doesn't seem to find/do anything. EDIT2: Never mind it's working, had some old code that was stopping it from working. Link to comment Share on other sites More sharing options...
0 DPyro Posted October 7, 2013 Author Share Posted October 7, 2013 Have no clue how to go about splitting... Link to comment Share on other sites More sharing options...
0 firey Posted October 7, 2013 Share Posted October 7, 2013 splitting is easy.Here is the pseudo codeset fileName to be test.exe set newFile to be test.exe.partset index to be 0set length to be 1024set offset to be 0 set byteBuffer to a new Byte array of length size. open fileName for binary input while ((readDataLen = fileName.read(byteBuffer,offset,length) > 0) //we got data set offset to be offset + readDataLenopen (newFile + index+=1) for binary outputwrite byteBuffer into the newFile starting at 0, going readDataLen longclose the newFilego back to beginning of while loop (assuming readDataLen > 0, otherwise go to next line)close the fileNameDone. Link to comment Share on other sites More sharing options...
0 DPyro Posted October 7, 2013 Author Share Posted October 7, 2013 I'm using this converted from vb: ChopItUp(openFileDialog1.FileName, 1048576); public int ChopItUp(string inputFile, int chunkSize) { FileStream reader = new FileStream(inputFile, FileMode.Open); int fileIndex = 0; byte[] buffer = new byte[chunkSize + 1]; FileStream writer = default(FileStream); int bytesRead = 0; do { bytesRead = reader.Read(buffer, 0, chunkSize); if (bytesRead > 0) { writer = new FileStream(Path.GetDirectoryName(openFileDialog1.FileName) + @"\File000" + fileIndex, FileMode.Create); writer.Write(buffer, 0, bytesRead); writer.Close(); fileIndex = fileIndex + 1; } } while (bytesRead > 0); reader.Close(); return fileIndex - 1; } Link to comment Share on other sites More sharing options...
0 firey Posted October 7, 2013 Share Posted October 7, 2013 that could be cleaned up quite a bit and restructured, but it should work. Link to comment Share on other sites More sharing options...
Question
DPyro
Link to comment
Share on other sites
8 answers to this question
Recommended Posts