Search code examples
.netvb.netvisual-studioiofilestream

Delete N length of bytes from a position in a large file


I read several files and merged them into one file, now I want to delete a specific file from it.


Let's say that there are 10 files with 1GB of size each, I merged them into one file making it 10GB of size, now I want to delete the 7th numbered file from it, is it possible? how do I achieve it?


  • I know length/size of the file to be deleted.

  • I know start and end position of the file to be deleted in that one large file.


    I figured that it is possible by...


  • Read and write first 6 and last 3 files and skip 7th file and write it to another file.

  • Or by reading from the end position of the file to be deleted and write it to start position of the file to be deleted until EOF in the same file then cut last unwanted N bytes.


    but I want to avoid it if there is another solution because it will take a lot of time to read and write the whole file and take a lot of disk space(in the first option) which may not be available to the user, so I am looking if there is another solution for that.


    I can also use any library or another method/solution if you guys recommend me, Thanks.


  • Solution

    1. You can use the batch Copy command to copy the file, it seems to be the easiest one copy file1.txt + file2.txt + ... + file10.txt newfile.txt here we skips the 7th file i.e. file7.txt. This is same as your 1st approach. It should be the efficient way as copy command interacts directly with kernel and memory device. Use may like to refer Robocopy too. You may pass construct the command from .Net and use shell execute.
    2. W.r.t. your 2nd approach, as suggested by you, its best to start from end of the file. You can use BinaryWriter to copy and overwrite using Seek method. SeekOrigin.Current will help you to overwrite the chunks.
    3. Low level programming i.e. you may directly play around with clusters of file and move sections of file. For this you may require to play around with small file and then use it on final consolidated large file. Refer the link. It is in C#. It seems to be the most efficient and interesting too. Of-course I haven't done it yet.