I need help copying only certain bytes to a new file.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
josephf
Posts: 11
Joined: 01 Feb 2014 23:22

I need help copying only certain bytes to a new file.

#1 Post by josephf » 01 Feb 2014 23:37

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.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: I need help copying only certain bytes to a new file.

#2 Post by foxidrive » 02 Feb 2014 02:57

ImageMagik is a command line set of tools to manipulate images. And is free.

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: I need help copying only certain bytes to a new file.

#3 Post by Endoro » 02 Feb 2014 03:43

You can use a patch tool for binaries, ex. http://stackoverflow.com/questions/1945 ... ry-patches

penpen
Expert
Posts: 1992
Joined: 23 Jun 2013 06:15
Location: Germany

Re: I need help copying only certain bytes to a new file.

#4 Post by penpen » 02 Feb 2014 04:13

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:

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.
Last edited by penpen on 02 Feb 2014 17:19, edited 1 time in total.

einstein1969
Expert
Posts: 941
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: I need help copying only certain bytes to a new file.

#5 Post by einstein1969 » 02 Feb 2014 11:17

@penpen
missing skip = 1 in for?

@josephf
that has the file size?

einstein1969

Post Reply