Parsing 从批处理脚本读取文本文件时出现问题
从批处理脚本读取文本文件时,我无法获取已经存在的值 doxygen.txt 版本 8.0.56336(版本和值之间没有空格) 测试蝙蝠 此处我无法获取第1行、第2行等的值 谁能告诉我哪里出了错Parsing 从批处理脚本读取文本文件时出现问题,parsing,batch-file,Parsing,Batch File,从批处理脚本读取文本文件时,我无法获取已经存在的值 doxygen.txt 版本 8.0.56336(版本和值之间没有空格) 测试蝙蝠 此处我无法获取第1行、第2行等的值 谁能告诉我哪里出了错 提前感谢。如果您只是想从输出中获取版本,这里有一个更简单的方法: 1: @echo off setlocal EnableDelayedExpansion set num=0 if %errorlevel%==0 ( FOR /F "skip=1 tokens=* delims=" %%v IN
提前感谢。如果您只是想从输出中获取版本,这里有一个更简单的方法: 1:
@echo off
setlocal EnableDelayedExpansion
set num=0
if %errorlevel%==0 (
FOR /F "skip=1 tokens=* delims=" %%v IN ('wmic product where "Name like 'Microsoft Visual C++ 2005 Redistributable'" get version ^| findstr "."') do (
echo Version: %%v
set mc_vcpp_05=%%v
)
)
endlocal
2:
如果出于某种原因需要读取该文件,请执行以下操作:
@echo off
if %errorlevel%==0 (
FOR /F "skip=1 tokens=1 delims= " %%a IN ('wmic product where "Name like 'Microsoft Visual C++ 2005 Redistributable'" get version ^| findstr "."') do (
echo %%a >> doxygen.txt
)
for /f "tokens=1* delims=" %%I in ('type doxygen.txt') do (
echo Version: %%I
)
del doxygen.txt
)
我还不知道为什么在你最初的剧本中它会对你起作用。当我发现行或括号块在执行前被解析时,我将编辑或注释我的答案。批处理扩展百分比表达式。
因此,set line1=%v[1]%将简单地扩展为零,因为它甚至在FOR循环开始之前扩展 但是您可以使用延迟扩展语法,因为它们在运行时被扩展
你应该在第一个循环中有
%%%a
吗?即使没有使用Alex。问题仍然存在。我们可以直接获得版本很好,但问题是我需要从文本文件中获得它。无论如何,非常感谢你的回复。
@echo off
if %errorlevel%==0 (
FOR /F "skip=1 tokens=1 delims= " %%a IN ('wmic product where "Name like 'Microsoft Visual C++ 2005 Redistributable'" get version ^| findstr "."') do (
echo %%a >> doxygen.txt
)
for /f "tokens=1* delims=" %%I in ('type doxygen.txt') do (
echo Version: %%I
)
del doxygen.txt
)
@echo off
setlocal EnableDelayedExpansion
set num=0
if %errorlevel%==0 (
FOR /F "tokens=* delims=" %%a IN ('"wmic product where "Name like 'Microsoft Visual C++ 2005 Redistributable'" get version"') do (
echo %%a>> doxygen.txt
)
rem FOR /F "tokens=* delims=" %%x in (doxygen.txt) DO echo %%x
for /f "tokens=* delims=" %%i in (doxygen.txt) do (
set /a num+=1
set "v[!num!]=%%i"
)
del doxygen.txt
set "line1=!v[1]!"
set "line2=!v[2]!"
set "line3=!v[3]!"
set "line4=!v[4]!"
echo line1: !line1!
echo line2: !line2!
echo line3: !line3!
echo line4: !line4!
)
endlocal