我正在尝试获取WMIC命令的输出,以便为每一行分配变量。
我正试图让出局看起来像这样:
安装了第一个程序
安装了2秒程序
安装了3个第三个程序
安装了第4个程序
5 ......等
@Echo off
wmic /output:file.txt product get name /format:csv
rem wmic product get name
@Echo Off
SetLocal EnableDelayedExpansion
Set n=
Set _InputFile=c:\users\jorge\desktop\file.txt
For /F "tokens=*" %%I IN (%_InputFile%) DO (
Set /a n+=1
Set _var!n!=%%I
)
:: This line will display the variables just assigned
:: For testing only, delete when not needed
Set _
EndLocal
pause
答案 0 :(得分:0)
这不会起作用,因为wmic会为你提供/ f无法读取的Unicode文件(参见here)。解决方法是使用('输入文件')代替:
@Echo off
:: not really need a csv here...
wmic /output:file.txt product get name /format:table
Set n=0
:: Suggest to use internal call :label rather than delayed expansion
For /F "tokens=* skip=1 delims=" %%I IN ('type file.txt') DO @call :Assign %%I
Set _
pause
goto :EOF
:Assign
Set /a n+=1
Set _var%n% = %*
goto :EOF