Split file at offSet

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Split file at offSet

#1 Post by Ed Dyreen » 11 Jan 2015 13:43

hey guys,

I know how to easily join files with batch using

Code: Select all

copy /args fileA + fileB fileC
But I can't find an easy DOS way to split a file at certain HEX offset.
For now I've been doing this manually using Hackman Hex Editor which even comes with a script editor to automate this proces. Unfortunately I haven't been able to find hackman's script editor manual. Can anyone provide me a link to this manual or an easy DOS way ?

I could convert the file to HEX, start counting chars and then split at a certain offset, but with native batch, that would be much coding and much processing afterwards, certainly when dealing with huge gigabyte-size files this seems too impractical to be of use.

Any suggestions ?

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Split file at offSet

#2 Post by Squashman » 11 Jan 2015 14:38

Is the offset at a different position on each line?

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Split file at offSet

#3 Post by dbenham » 11 Jan 2015 15:21

@Squashman - it looks to me as if Eddie is attempting to split a binary file into 2 parts at a particular offset.

@Ed - Yes. One "pure" batch way would be do use FC and a temp dummy file to create a list of hex codes for each byte, count, and reconstruct the two parts into the binary form. Very ugly. You could probably so something with CERTUTIL --encodehex , but that seems ugly as well.

As long as your source is <= 2 GB, then it is not hard with JREPL.BAT:

Code: Select all

:: splitAtOffset.bat  source  offset
@echo off
setlocal
set /a "offset=%~2"
call jrepl "^((?:\w|\W){%offset%})((?:\w|\W)*)" "stdout.Write($2);$1" /m /j /f "%1" /o "%~n1_1%~x1" >"%~n1_2%~x1"

Usage: The following will split yourFile.ext into yourFile_1.ext with 1000 bytes, and yourFile_2.ext with the remainder

Code: Select all

splitAtOffset yourFile.ext 0x3E8

But the JREPL call may be nearly as complicated as just writing your own VBS or JScript or Powershell to do the task directly.


Dave Benham

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Split file at offSet

#4 Post by Aacini » 11 Jan 2015 16:46

What exactly do you mean with "split a file"? Take one input file and generate two output files? So the first part have a given size and the second one have the rest?

Well, you may generate the first part (of a given size) using my FilePointer.exe and TruncateFile.exe auxiliary programs in a very simple way:

Code: Select all

rem Create a duplicate of input file for the first part:
copy input.ext firstPart.ext

rem Get the offset in decimal:
set /A offset=0x%HEX_offset%

(
   rem Set FilePointer position at end of first part
   FilePointer 1 %offset%
   rem Truncate the file at this point
   TruncateFile.exe 1
) >> firstPart.ext


The second part imply to read the file from the truncation point to the EOF. Again, FilePointer.exe program may set the file pointer at the right position, so just the reading of the file from this point on would be needed. Accordingly to the original jeb's post, FINDSTR command read the redirected file from current FP position until EOF, so the second part may be generated this way:

Code: Select all

< input.ext (
   rem Advance FilePointer to start of second part
   FilePointer 0 %offset%
   rem Copy from this point on
   findstr "^"
) > secondPart.ext


Antonio

miskox
Posts: 553
Joined: 28 Jun 2010 03:46

Re: Split file at offSet

#5 Post by miskox » 12 Jan 2015 06:07

I use Total Commander every day (started with Norton Commander back in the DOS days).

Total Commander has an option to split/combine files (it also generates a checksum file). This is what you want (ghisler.com).
Resulting files can be combined with Total Commander and/or

Code: Select all

copy/b input_file.* output_file


Saso

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Split file at offSet

#6 Post by Ed Dyreen » 12 Jan 2015 23:03

3 very creative approaches, I'll look into it and report my findings when I find time.

great thanks !

Post Reply