2014-03-27 52 views
0

我已經寫了一張在批處理腳本代碼分隔這有點像:創建逗號批處理腳本

( 
    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 

這是如何實現的?

回答

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 

在你DOUB爲什麼兩個for命令時,WMIC的輸出包含已被刪除的aditional的回車符情況。

+0

完美! 如果我想添加IP地址和MAC地址,該怎麼辦? –

+0

@MandarShinde該數據與'wmic nicconfig'一起提供。但是你需要首先確定你想從哪個網點獲取數據,如果是ip4或ip6或者兩者都有。獲取數據的方式與發佈的代碼相同。檢索一組字段並將它們存儲在變量中,檢索第二組並將其存儲在變量中。然後將變量內容輸出到輸出文件中。 –

+0

如果我必須執行另一個'wmic'命令(比如'wmic cpu')並將其輸出追加到上述命令('wmic computersystem')的輸出旁邊,以便它們出現在同一行中,該怎麼辦? –

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 
+0

試過這個。它只是在輸出文件中顯示一系列逗號。 –

+0

如果您在控制檯輸入它,則需要將%%更改爲%。如果將它保存在批處理文件中並運行它,它的寫法是正確的,而不是手動輸入。 –

+0

不,我在批處理文件中輸入並執行它。 –