1
votes

I am trying to concatenate several files together using a Windows batch file to make a single image of the files. Now, I have found this can be done using copy "copy file1+file2+file3+file4 FileConcat" but what I want to do is to start file1 @ byte address 00, then start file2 @ byte address 999424, start file3 @ byte address 3997696 and file4 @ byte address 4001536. I can do this in Linux using the following but is there a way to do this in windows without writing a specific application? Any help, much appreciated.

set COMBINEDFILE=IMAGE/combined.srec

dd conv=notrunc bs=1 if=INPUT/FILE1.srec of=$COMBINEDFILE seek=0

dd conv=notrunc bs=1 if=INPUT/FILE2.xap of=$COMBINEDFILE seek=999424

dd conv=notrunc bs=1 if=INPUT/FILE3.html of=$COMBINEDFILE seek=3997696

dd conv=notrunc bs=1 if=INPUT/FILE4.js of=$COMBINEDFILE seek=4001536

Thank you for any help

3
Are files larger or smaller then specified size? If smaller, you can keep padding target file until offset reached. - LS_ᴅᴇᴠ
Hi, yes the files will always be smaller than specified size allocated to them. This padding idea sounds like a good idea but how would I go about doing this?? Many thanks for the reply. - Ad0m
Do you take care for the 'end of line' character(s) if you calculate the offset? - Endoro
Hi Endoro, I do not calculate the offset in Linux, the offset of each file is fixed. File1 starts @ 00, File2 starts @ byte 999424. There will always be space between the end of file1 and the start of file2 and what goes in the space is not important. What is important is the starting byte position of each file. I hope this makes sense. - Ad0m
okay, I have and idea!!! How about I do this using the following steps?? step 1 - copy File1.srec to combined.srec Step 2 - get the size of combined.srec file using filesize.bat (@echo off; echo %~z1) step 3 - subtract the filesize from next file start point (999424 - filesize) and generate a file (temp.txt) using fsutil for the difference. step 4 - Concat the temp.txt onto combined.srec step 5 - Concat file2 onto the end of combined.srec step 6 - repeat steps 2 to 5 changing the next start location and the file in step 5. Does this sound feasible? I have no idea how to do it though! - Ad0m

3 Answers

0
votes

I only know methods for appending files in standard batch, none for truncation.

If input files don't need to be truncated, they can be added and them padded to fill empty spaces.

For this to work, you will need 1 byte size padding file (1.pad) as well as - for performance improvement - 10, 100, 1000, 10000 and 100000 bytes size padding files.

PADFILE.BAT - utility batch to pad your files:

@ECHO OFF
IF %2.==. GOTO :Syntax
IF NOT %3.==. GOTO :Syntax
SET /A _size=1*%2
:CheckSize
SET /A _Left=%_Size%-%~z1
IF %_Left% LSS 0 1>&2 ECHO File already bigger than size!&TYPE 2>NUL&GOTO :EOF
IF %_Left% == 0 GOTO :EOF
IF %_Left% GEQ 100000 TYPE 100000.PAD>>%1&&GOTO:CheckSize
IF %_Left% GEQ 10000 TYPE 10000.PAD>>%1&&GOTO:CheckSize
IF %_Left% GEQ 1000 TYPE 1000.PAD>>%1&&GOTO:CheckSize
IF %_Left% GEQ 100 TYPE 100.PAD>>%1&&GOTO:CheckSize
IF %_Left% GEQ 10 TYPE 10.PAD>>%1&&GOTO:CheckSize
TYPE 1.PAD>>%1
GOTO :CheckSize
:Syntax
ECHO Pads file until desired size.
ECHO Syntax: PADFILE filepath size

Now your commands would look like:

SET COMBINEDFILE=IMAGE/combined.srec
COPY INPUT/FILE1.srec %COMBINEDFILE%
PADFILE.BAT %COMBINEDFILE% 999424
COPY %COMBINEDFILE%+INPUT/FILE2.xap
PADFILE.BAT %COMBINEDFILE% 3997696
COPY %COMBINEDFILE%+INPUT/FILE3.html
PADFILE.BAT %COMBINEDFILE% 4001536
COPY %COMBINEDFILE%+INPUT/FILE4.js

Remember that at least 1.PAD should be placed in same folder as PADFILE.BAT.

For fast creation of other .PAD files from 1.PAD you can use:

(FOR /L %I IN (1,1,10) DO @TYPE 1.PAD)>10.PAD
(FOR /L %I IN (1,1,10) DO @TYPE 10.PAD)>100.PAD
(FOR /L %I IN (1,1,10) DO @TYPE 100.PAD)>1000.PAD
(FOR /L %I IN (1,1,10) DO @TYPE 1000.PAD)>10000.PAD
(FOR /L %I IN (1,1,10) DO @TYPE 10000.PAD)>100000.PAD
1
votes

I have an alternative answer to LS_dev's answer but it is based on his answer so I can't take any credit for it. Instead of generating padding using multiple padding files in a loop I generate an empty file based on the padding needed and append this to the file. I am not a full time programmer so maybe there are "issues" with doing it this way. If you are using a win7 machine, you need admin privileges to run this script. It's fsutil that needs admin rights to run. Thanks again LS_dev

PADOUT.BAT

@ECHO OFF
IF %2.==. GOTO :Syntax
IF NOT %3.==. GOTO :Syntax
SET /A _size=1*%2
del padding.txt
SET /A _Left=%_Size%-%~z1

fsutil file createnew %cd%\padding.txt %_Left%&&goto:eof

:Syntax
ECHO generates padding file to make file specific size
ECHO Syntax: PADOUT filepath size

GENERATE.BAT

del IMAGE\combined.srec
SET COMBINEDFILE= IMAGE\combined.srec
COPY INPUT\lwip_140_echo_server.elf.srec %COMBINEDFILE%
CALL PADOUT.BAT %COMBINEDFILE% 999424-1
COPY %COMBINEDFILE%+padding.txt+INPUT\MPDU1_Read.xap /b %COMBINEDFILE%
CALL PADOUT.BAT %COMBINEDFILE% 3997696-1
COPY %COMBINEDFILE%+padding.txt+INPUT\MPDU1_ReadTestPage.html %COMBINEDFILE%
CALL PADOUT.BAT %COMBINEDFILE% 4001536-1
COPY %COMBINEDFILE%+padding.txt+INPUT\Silverlight.js %COMBINEDFILE%
0
votes

When you add the following code at the beginning of the Batch file script that LS_DE provided. Then you don't need to create anything but this batch files. If the files already are created.

This code created the 'empty' PAD files with the length. I have validated this to be working without any issues.

fsutil file createnew %~dp01.PAD 1
fsutil file createnew %~dp010.PAD 10
fsutil file createnew %~dp0100.PAD 100
fsutil file createnew %~dp01000.PAD 1000
fsutil file createnew %~dp010000.PAD 10000
fsutil file createnew %~dp0100000.PAD 100000