首页 > 解决方案 > 在批处理脚本中获取输出范围

问题描述

我正在制作一个批处理脚本,并在谷歌上搜索了如何将命令的输出放入变量中,但是这段代码获取了整个输出,因为我只需要前 4 位数字到冒号。(就我而言)

这是 debug.exe 的示例输出:

0ADE:0AC0

因此,在这种情况下,myvar 的值应该是 0ADE。这是代码:

for /f "tokens=* usebackq" %%a in (`echo s 100 8000 74 00 5c 00  4f`) do (set myvar=%%a)

我想我可能需要 delims 参数,但无法弄清楚如何将它与批处理一起使用。

标签: batch-file

解决方案


编辑:如果我理解得很好,你只需要剪断一个字符串。如果是这种情况,请在之前的变量集中使用“:~0,0”,其中每个 0 代表一个索引位置。EG:以下每一行都将输入“Hello world!”

set random=Hello world!
echo %random%
echo %random:~0,5% world!
echo Hello %random:~6%

关于“debug.exe”的结果,也许您可​​以将其结果通过管道传输到一个临时文件中,在批处理中调用它,然后剪切结果。

由于您没有共享足够的代码,我无法为您提供适当的帮助,但我会建议这样的事情:

@echo off

rem This will start debug.exe, and let's hope it allows to pipe the result to a file, as it does not always works.
start "" do.exe > "%temp%\log_file_debug.txt"

rem Wait until the output file is written.
echo Press any key once the application is finished.
pause > nul

rem Now lets retrieve the results from the previous file, and hope it have this format: 0ADE:0AC0
for /f "tokens=*" %%c in ('type "%temp%\log_file_debug.txt"') do set results=%%~c

rem As we have retrieved the results into a variable, let's snip it only for the 4 firts characters
set snip_result=%results:~0,4%

rem Your snipped code is now set in the variable snip_result. Check it out:
echo Check the result: %snip_result%

pause

希望能帮助到你。问候。厘米。-


推荐阅读