在变量中获取findstr的输出

时间:2017-08-25 08:45:07

标签: batch-file

我是批处理脚本的新手,想知道如何将findstr的输出变为变量?

@echo off
set /p assetPath="Set asset path: "
setlocal enableDelayedExpansion
for /r %assetPath% %%p in (*.png) do (
    findstr "guid" "%%p".meta
    if not !errorlevel!==0 (
        echo Could not find guid of file: %%p!
    )
)
pause

findstr将输出 “guid:d46decd9d3bbf0d46b31a3d4ae0f18ff” 对于包含guid的每个文件,我如何对此进行操作并将guid保存到变量中?

1 个答案:

答案 0 :(得分:1)

@echo off
set /p assetPath="Set asset path: "
setlocal enableDelayedExpansion
SET /A COUNT=0
for /r %assetPath% %%p in (*.png) do (
    FOR /F "TOKENS=1*" %%f in (' findstr "guid" "%%p".meta ') DO (
    if not !errorlevel!==0 (
        echo Could not find guid of file: %%p!
    ) ELSE (
      SET /A COUNT+=1
      SET "FILE[!COUNT!]=%%p"
      SET "GUID[!COUNT!]=%%g"
    ))
)
FOR /L %%x in (1,1,%COUNT%) DO echo !GUID[%%x]! is guid for !FILE[%%x]!
pause

这应该为找到的文件设置FILE [*]和GUID [*]并报告结果。