【问题标题】:wmic variables from txt filetxt 文件中的 wmic 变量
【发布时间】:2012-07-14 03:26:08
【问题描述】:

我正在尝试获取 WMIC 命令的输出以将变量分配给每一行。

我试图让输出看起来像这样:

第一个安装的程序

安装了 2 秒的程序

安装了第三个程序

已安装 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

【问题讨论】:

    标签: variables assign wmic


    【解决方案1】:

    这行不通,因为 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
    

    【讨论】:

      猜你喜欢
      • 2023-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多