Loops 如何使用findstr命令从批处理文件中提取二进制数据?
我使用下面的批处理脚本,脚本底部附加了二进制部分(.exe程序)。我使用此技巧将二进制部分附加到批处理脚本:Loops 如何使用findstr命令从批处理文件中提取二进制数据?,loops,batch-file,binary,findstr,Loops,Batch File,Binary,Findstr,我使用下面的批处理脚本,脚本底部附加了二进制部分(.exe程序)。我使用此技巧将二进制部分附加到批处理脚本: ::Append binary part to your batch file with COPY copy /y /a "batchscript.bat" + /b program.exe /b combined.bat 为了从批处理脚本(combined.bat)中提取二进制部分,我使用以下方法和“findstr”命令: 因此,这段代码将从脚本中提取二进制部分到program.ex
::Append binary part to your batch file with COPY
copy /y /a "batchscript.bat" + /b program.exe /b combined.bat
为了从批处理脚本(combined.bat)中提取二进制部分,我使用以下方法和“findstr”命令:
因此,这段代码将从脚本中提取二进制部分到program.exe:
findstr /v "^;;;===,,," %~f0 > program.exe
但作为缺点,脚本部分的每一行都必须以以下前缀开头
;;;===,,,
我想做的是只在最后一行代码中使用前缀“;;;===,,”,并提取这一行之后的所有二进制数据。是否有可能通过findstr+for循环+if命令的疯狂组合来实现这一点?例如:
@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
@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
setlocal
rem获取分界线的线号
对于/F“delims=:”('findstr/N“^;;;;===,,“%~F0””)中的%%a,请设置“行=%a”
rem从该文件中提取二进制部分
<“%~F0”(
rem通过第一行
对于(1,1,%line%)中的/L%%i,请设置/P“=”
雷姆,把剩下的抄下来
findstr“^”
)>program.exe
后藤:EOF
;;;===,,, ::下面是program.exe的二进制数据
这个程序在功能上等同于您的代码,也就是说,当您的程序失败时,这个程序将失败。您应该知道,findstr
命令不能在所有情况下成功复制二进制数据
此解决方案充分利用了这样一个事实,即
findstr
命令不会移动重定向输入文件的文件指针,该文件指针从上一个set/p
命令保留在正确的位置。感谢您的回答,不幸的是,您的解决方案不起作用。脚本被卡住并显示错误消息(FINDSTR:Line XXXX太长),我需要以ctrl+break结束它。文件program.exe已部分创建,但其数据与批处理文件中的二进制数据不匹配。因此,您的代码确实跳过了第一个脚本行,但二进制数据没有正确复制。奇怪的是,这两个代码在功能上是等价的,因为我的“findstr”代码在相同的批处理脚本和相同的二进制数据上工作得很好。Ops!我刚刚意识到,当findstr
命令从重定向输入读取行时,最大长度被限制为8KB,因此这两种方法并不等效!有关更多详细信息,请参阅。
@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