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

Windows 在批处理脚本中创建逗号分隔的值,windows,batch-file,csv,Windows,Batch File,Csv,我用批处理脚本编写了一段代码,有点像: ( 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.

我用批处理脚本编写了一段代码,有点像:

( 
    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 
如何做到这一点?

类似这样的事情:

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

如果您怀疑这两个
for
命令的原因,WMIC的输出包含一个必须删除的传统回车字符。

尝试了此操作。它只是在输出文件中显示一系列逗号。如果您在控制台中键入它,则需要将%%更改为%。如果将其保存在批处理文件中并运行,则其编写方式是正确的,而不是手动键入。不,我在批处理文件中键入并执行了它。尝试复制并粘贴它,而不是键入它。结果相同。只显示逗号。太好了!如果我也想添加IP地址和MAC地址怎么办?@MandarShinde该数据可通过
wmic nicconfig
获得。但是,您需要首先确定要从哪个nic获取数据,是ip4还是ip6,还是两者兼而有之。获取数据的方法与发布的代码相同。检索一组字段并将其存储在变量中,检索第二组字段并存储在变量中。然后将变量内容输出到输出文件中。如果我必须执行另一个
wmic
命令(例如,
wmiccpu
),并将其输出附加到上述命令的输出旁边(
wmiccomputersystem
),以便它们显示在同一行中,该怎么办?@MandarShinde,同上。在
for
命令中,不要回显数据,而是将值保存在变量中。当您在变量中拥有所有必需的信息时,则
echo%var1%,%var2%,…>>file.txt
上面提到的脚本运行得非常好。但作为批处理脚本的新手,我无法理解某些事情。首先是
“tokens=2-7”
的含义。当我将其更改为
“tokens=1-6”
时,输出发生了变化。第二件事是,在
echo
语句中,您使用了
%%e、%%b、%%c、%%d、%%g、%%f
。为什么不按字母顺序
%%b、%%c、%%d、%%e、%%f、%%g
?(这可能是一个愚蠢的疑问,对此表示歉意。)
@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