在批处理脚本中创建逗号分隔值

时间:2014-03-27 10:36:22

标签: windows batch-file csv

我在Batch脚本中编写了一段代码,有点像:

( 
    wmic.exe /node:x.x.x.x computersystem get name
    wmic.exe /node:x.x.x.x computersystem get domain 
    wmic.exe /node:x.x.x.x computersystem get manufacturer 
    wmic.exe /node:x.x.x.x computersystem get model 
    wmic.exe /node:x.x.x.x computersystem get username 
    wmic.exe /node:x.x.x.x computersystem get systemtype
) >> file.txt

file.txt的内容是:

ABC123  
xyz.com  
Hewlett-Packard  
HP xw4400 Workstation  
ABC123\Administrator  
x64-based PC 

我希望以上信息以CSV格式存储,如下所示:

ABC123 , xyz.com , Hewlett-Packard , HP xw4400 Workstation , ABC123\Administrator , x64-based PC 

如何实现这一目标?

2 个答案:

答案 0 :(得分:1)

@echo off
for /f "skip=2 delims=" %%a in (
    'wmic /node:localhost computersystem get name^,domain^,manufacturer^,model^,username^,systemtype /format:csv'
) do (
    for /f "tokens=2-7 delims=," %%b in ("%%a") do (
        echo(%%e , %%b , %%c , %%d , %%g , %%f
    )
) >> file.txt

如果你对两个for命令的原因加倍,WMIC的输出包含一个必须删除的aditional回车符。

答案 1 :(得分:0)

这样的事情:

FOR /F %%i IN ('wmic computersystem get name') DO SET N=%%i
FOR /F %%i IN ('wmic computersystem get domain') DO SET D=%%i
echo %N%;%D% > output.csv