Page 1 of 1
I need help copying only certain bytes to a new file.
Posted: 01 Feb 2014 23:37
by josephf
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.
Posted: 02 Feb 2014 02:57
by foxidrive
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.
Posted: 02 Feb 2014 03:43
by Endoro
Re: I need help copying only certain bytes to a new file.
Posted: 02 Feb 2014 04:13
by penpen
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=5326This 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
Note this example should only work with file sizes less or equal than 2147483647 bytes.
penpen
Edit: Added "skip=1 ": Thanks to einstein1969.
Re: I need help copying only certain bytes to a new file.
Posted: 02 Feb 2014 11:17
by einstein1969
@penpen
missing skip = 1 in for?
@josephf
that has the file size?
einstein1969