11 March 2016 - Forum Rules

Main Menu

Easy program to copy only part of a file

Started by Guadozoku, March 21, 2013, 11:45:47 PM

Previous topic - Next topic


I'm looking for an easy way to copy only part of a file (c004 to 2c004) to another file without doing it manually in a hex editor. Anyone know a program to do that? I tried Google, but couldn't find one.


dd if=in.bin of=out.bin bs=1 count=$((0x20000)) skip=$((0xc004))

... or at least I think that's right. You do use a Unix, right? Or is that just my memory playing tricks again?
we are in a horrible and deadly danger


Does it have to be an arbitrary part of another file or basically slice this section out and stick in a new file?
BRPXQZME took the unix a like method and I will assume no powershell (not that I can think how to do it there at this point, primarily as I do not know it that well) or cygwin in the case of windows. If you want windows I would suggest using a file trimmer instead. If you do need an arbitrary insertion you will probably have to do something like trim the first part of the file, the file after what you are replacing and do something like "copy /b firstpart.file+newfragment.file+restoffile.file newshinyfile.file".
My command line file trimmer of choice these days is filecutter has it. Syntax is filecutter length file.out <-s start>
0x is the indicator for hex and it otherwise accepts decimal.


Or you might use Python or PHP or some other scripting language to do it.
For example, a complete PHP script to do it:

$fp = fopen('inputfile.bin', 'r');
fseek($fp, 16); // Source file position
file_put_contents('outputfile.bin', // Output file name
  fread($fp, 0x20000)); // Number of bytes to copy


(For the record)
Or you could use Swiss Free Knife

With a command line like this (on windows, just remove .exe in *nux):
sfk169.exe partcopy myrom.gba 1E83E3 1000 extractedfile.bin

where 1E83E3 = starting offset and 1000=size in decimal


you do you mean when you say doing it manually?
go r2, use debug. .... White hand was fainted


Python would be along the lines of:
with open("filename.ext", 'rb') as f:
data =
with open("output.ext", 'wb') as f:

Still think it's faster to just open a hex editor, but if you need to do it programmicaly then it's all cool.


what's wrong wtih using hex-editors for this?
go r2, use debug. .... White hand was fainted


I'd say it depends on how many times and exactly the way you have to do it, really. If it's going into a makefile, for instance, having the command would be better. Not that you can't Sikuli up a hex editor, but surely that's not the way to go!
we are in a horrible and deadly danger


I use it when I am making extraction files that slice up archive files for various people and myself for that matter. If you are staring at an archive containing several hundred files it considerably less tedious and error prone than referencing a file location/size list, possibly doing some maths on it and then selecting it or telling my hex editor to select a block before doing the rest, even if not it is still easier than holding someone's hand through using a hex editor (for some reason a lot of people treat them as magic tools with the power to end the universe or something).

Granted for not a lot more effort I could probably have a python script or something do the same, indeed I probably should get around to making some program fragments aimed at such a thing, and probably even get myself halfway to a repacking/rebuilding tool but eh.

In short, we see an awful lot of requests of dubious merit around here but this is not one of them.


you might have noticed, that I really like r2. Here's how I would do it:

open src-file with r2
s [src-offset]
wt foo [number of bytes to copy]
quit r2
open r2 with -w
s [dst-offset]
wf foo
quit r2

go r2, use debug. .... White hand was fainted


r2= ?

I had never heard of it, I will look further though as it could be useful to have such a thing to point people at and it has a nicer license than ROMulan which was my previous "not quite a full language" tool of choice.


yup, if you install radare2, r2 is set as an alias for radare2
go r2, use debug. .... White hand was fainted


dd for DOS:

John Fine's partcopy:
NOTE: this program can also write to raw sectors on the drive and such, so using the wrong parameters could be lethal!  (under DOS anyway, Windows NT+ will probably prevent the raw writes.)

both of these are dated, pure DOS programs, so they won't understand long filenames.