Mandar Shinde
Mandar Shinde

Reputation: 1745

Creating Comma Separated Values in Batch Scripting

I have written a piece of code in Batch script which is somewhat like :

( 
    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

The contents of file.txt are :

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

I want above information to be stored in CSV format as follows :

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

How can this be achieved?

Upvotes: 0

Views: 2337

Answers (2)

MC ND
MC ND

Reputation: 70923

@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

In case you doub why the two for commands, the output of the WMIC contains an aditional carriage return character that has to be removed.

Upvotes: 1

Mark Setchell
Mark Setchell

Reputation: 207375

Something like this:

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

Upvotes: 0

Related Questions