2012-07-14 28 views
0

我試圖獲取WMIC命令的輸出以將變量分配給每一行。來自txt文件的wmic變量

我試圖讓出來放到這個樣子:

1第一程序安裝的

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

這將不起作用,因爲wmic將爲您提供/ f不能讀取的Unicode文件(請參閱here)。解決方法是使用('type file')代替:

@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