I need help copying only certain bytes to a new file.
Moderator: DosItHelp
I need help copying only certain bytes to a new file.
I'm trying to write a batch program to copy the first 2400 bytes from a file, skip the next 32 bytes and repeat over and over until the end of the file. Is this possible without using another language? The data that I am basically trying to crop the side off of, is a raw image, that has a stride at the far right side of it. The stride is 16 pixels at 2 bytes per pixel.
Re: I need help copying only certain bytes to a new file.
ImageMagik is a command line set of tools to manipulate images. And is free.
Re: I need help copying only certain bytes to a new file.
You can use a patch tool for binaries, ex. http://stackoverflow.com/questions/1945 ... ry-patches
Re: I need help copying only certain bytes to a new file.
You may also use the newly created pure batch solution (first linked post) of this topic (this is slower than the above alternatives):
http://www.dostips.com/forum/viewtopic.php?f=3&t=5326
This creates the files characters\00.char to characters\FF.char containing the labeled hex value.
Then you can run this example that can easily adjusted:
Note this example should only work with file sizes less or equal than 2147483647 bytes.
penpen
Edit: Added "skip=1 ": Thanks to einstein1969.
http://www.dostips.com/forum/viewtopic.php?f=3&t=5326
This creates the files characters\00.char to characters\FF.char containing the labeled hex value.
Then you can run this example that can easily adjusted:
Code: Select all
@echo off
setlocal enableDelayedExpansion
:: create example to copy (256 bytes long: 0x00 to 0xFF)
(for %%h in (0 1 2 3 4 5 6 7 8 9 A B C D E F) do for %%l in (0 1 2 3 4 5 6 7 8 9 A B C D E F) do type "characters\%%~h%%~l.chr") > "source.dat"
:: needed a file of the same size to read (in this case 256 NUL characters)
set "comparand=00"
(for %%h in (0 1 2 3 4 5 6 7 8 9 A B C D E F) do for %%l in (0 1 2 3 4 5 6 7 8 9 A B C D E F) do type "characters\%comparand%.chr") > "compare.dat"
:: copy blocks of 16 byte, with holes of 8 bytes, just replace these with other numbers if needed
set "block=16"
set "hole=8"
:: copy: i is the file index, j is the file index modulo (block + hole)
set /A "i=j=0"
(
for /f "skip=1 tokens=1,2 delims=: " %%a in ('fc /b "source.dat" "compare.dat"') do (
for /L %%B in (!i!, 1, 0x%%~a) do (
if !j! lss %block% (
if %%~B equ 0x%%~a ( type "characters\%%~b.chr"
) else type "characters\00.chr"
)
set /a "i+=1", "j=(j+1)%%(block+hole)"
)
)
) > target.dat
goto :eof
penpen
Edit: Added "skip=1 ": Thanks to einstein1969.
Last edited by penpen on 02 Feb 2014 17:19, edited 1 time in total.
-
- Expert
- Posts: 961
- Joined: 15 Jun 2012 13:16
- Location: Italy, Rome
Re: I need help copying only certain bytes to a new file.
@penpen
missing skip = 1 in for?
@josephf
that has the file size?
einstein1969
missing skip = 1 in for?
@josephf
that has the file size?
einstein1969