如何使用findstr提取分隔符字符串后的二进制文件的一部分?

时间:2018-06-08 07:19:36

标签: loops batch-file binary findstr

两个文件(二进制或非二进制)可以使用控制台命令以特定方式连接到一个文件,并在它们之间使用特定的分隔符字符串。稍后,可以通过以下简单方法°提取第二个文件,同样仅使用控制台命令:

@echo off
setlocal enableextensions enabledelayedexpansion

REM === JOINING FILES ===

REM Divider.txt must consist of a line starting with "di-vi-der"
REM preceeded by at least one line and followed by a blank line.
echo This is a necessary preceeding line. > Divider.txt
echo di-vi-der This line must be followed by a blank line. >> Divider.txt

REM Join files to Joined.bin by one of these two methods:
REM copy /b FirstFile.exe + /a Divider.txt + /b File2BeExtracted.exe /b Joined.bin
type FirstFile.exe Divider.txt File2BeExtracted.exe > Joined.bin

REM === UNJOINING FILES ===

REM Get the line number of the dividing line in Joined.bin:
for /F "delims=:" %%a in ('findstr /N "^di-vi-der" "Joined.bin"') do set "lines=%%a"

REM Extract the part of the Joined.bin following the divider line:
< "Joined.bin" (

   REM Pass thru the first lines:
   for /L %%i in (1,1,%lines%) do set /P "="

   REM Copy the rest to Output.bin:
   findstr "^"

) > Output.bin

这几乎完美无缺。

我的第一个(和主要)问题是:findstr在写入Output.bin的最后一行末尾添加0D 0A。如何纠正这个问题?

第二个问题可能是:由于对findstr命令的一些限制(行的最大长度),这种方法可能会在某些(罕见?)情况下失败。要检测此类情况,可以确定File2BeExtracted.exe的文件大小并将其存储在分隔线中,以便在提取文件后进行最终的身份检查。如何做到这一点?

°这个棘手的方法是由jeb发明的。另见here

0 个答案:

没有答案