如何使用findstr命令从批处理文件中提取二进制数据? [英] How to extract binary data from batch file using findstr command?

查看:181
本文介绍了如何使用findstr命令从批处理文件中提取二进制数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的批处理脚本,该脚本在脚本底部附加了二进制部分(.exe程序).我使用此技巧将二进制部分附加到批处理脚本中:

I use the below batch script which has binary part (.exe program) append to the bottom of the script. I used this trick to attach binary part to my batch script:

::Append binary part to your batch file with COPY
copy /y /a "batchscript.bat" + /b program.exe /b combined.bat

为了从批处理脚本(combined.bat)中提取二进制部分,我将以下方法与"findstr"命令一起使用:

In order to extract the binary part from the batch script (combined.bat), I use the following method with "findstr" command:

;;;===,,, @ECHO OFF
;;;===,,, SETLOCAL ENABLEEXTENSIONS
;;;===,,, 
;;;===,,, echo test line1
;;;===,,, echo test line2
;;;===,,, 
;;;===,,, findstr /v "^;;;===,,," %~f0 > program.exe
;;;===,,, 
;;;===,,, echo test line3
;;;===,,, echo test line4
;;;===,,, exit /b
;;;===,,, ::Below are binary data for program.exe
binarybinarybinarybinarybinarybinarybinarybinarybinarybinarybinarybinary
binarybinarybinarybinarybinarybinarybinarybinarybinarybinarybinarybinary
...

因此,这段代码将脚本中的二进制部分提取到program.exe:

So this piece of code extracts the binary part from the script to program.exe:

findstr /v "^;;;===,,," %~f0 > program.exe

但是,不利的一面是,脚本部分的每一行都必须以以下前缀开头

But as a down-side, each line of the script part must begin with following prefix

;;;===,,,

我想做的是仅在代码的最后一行使用前缀";;; === ,,",并提取此行之后的所有二进制数据.是否有可能通过findstr + for loop + if命令的某种疯狂组合来实现这一目标?示例:

What I want to do is to use the prefix ";;;===,,," only at the last line of code and extract all binary data after this line. Is it possible to achieve this via some crazy combination of findstr + for loop + if command? Example:

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS

echo test line1
echo test line2

HERE IS THE CODE TO EXTRACT BINARY PART AFTER LAST SCRIPT LINE

echo test line3
echo test line4
exit /b
;;;===,,, ::Below are binary data for program.exe
binarybinarybinarybinarybinarybinarybinarybinarybinarybinarybinarybinary
binarybinarybinarybinarybinarybinarybinarybinarybinarybinarybinarybinary
...

非常感谢.

推荐答案

@echo off
setlocal

rem Get the line number of the dividing line
for /F "delims=:" %%a in ('findstr /N "^;;;===,,," "%~F0"') do set "lines=%%a"

rem Extract the binary part from this file
< "%~F0" (

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

   rem Copy the rest
   findstr "^"

) > program.exe
goto :EOF

;;;===,,, ::Below are binary data for program.exe

该程序与您的代码在功能上是等效的,也就是说,该程序在您的程序失败时将失败.您应该知道findstr命令在所有情况下都不能 成功复制二进制数据.

This program is functionally equivalent to your code, that is, this program will fail when your program fails. You should know that findstr command can not successfully copy binary data in all cases.

此解决方案充分利用了以下事实:findstr命令不会移动重定向的输入文件的文件指针,该文件输入指针与先前的set /P命令放在正确的位置.

This solution makes good use of the fact that findstr command does not move the file pointer of the redirected input file that is leaved at the right place from the previous set /P commands.

这篇关于如何使用findstr命令从批处理文件中提取二进制数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆