Avatar of lo ahmed
lo ahmed
 asked on

empty lines in .csv file when running batch script

My csv file is showing blank lines in between of each output, anyway to get rid of this? my code is below.
Thank you!

@echo off
setlocal EnableDelayedExpansion

set listfile=C:\machines.txt
set outputfile=C:\dell\%computername%.csv

(
  for /F "usebackq" %%i in ("%listfile%") do (
    for /f "skip=2 tokens=2 delims=," %%A in ('wmic /node:%%i computersystem get name /format:csv 

2^>NUL') do (
      echo ComputerSystem : %%A  
  )
 for /f "skip=2 tokens=2 delims=," %%A in ('wmic /node:%%i Cpu get Name /format:csv 2^>NUL') do ( 
echo Cpu : %%A
    )
    for /f "skip=2 tokens=2 delims=," %%A in ('wmic /node:%%i path win32_VideoController get name 

/format:csv 2^>NUL') do (
      echo VideoController: %%A
    )
    for /f "skip=2 tokens=2 delims=," %%A in ('wmic /node:%%i ComputerSystem get 

TotalPhysicalMemory /format:csv 2^>NUL') do (

      set TotalPhysicalMemory=%%A
      set TotalPhysicalMemory=!TotalPhysicalMemory:~0,-10!
            echo  TotalPhysicalMemory=!TotalPhysicalMemory! MB
    
    )
    echo Drives:
    for /f "skip=2 tokens=2-4 delims=," %%A in ('wmic /node:%%i LogicalDisk Where DriveType^=3 Get 

Caption^,FreeSpace^,Size /format:csv 2^>NUL') do (
      set Caption=%%A
      set Free=%%B
      set Size=%%C
      set Free=!Free:~0,-10!
      set Size=!Size:~0,-10!
     echo !Caption!, Free=!Free! MB, Size=!Size! MB
echo --------------------------------------------------------------------------------
    )
  ) 
) > "%outputfile%"

Open in new window

Windows Batch

Avatar of undefined
Last Comment
Bill Prew

8/22/2022 - Mon
Bill Prew

Okay, because the WMIC output is a little odd, adding some extra line feeds where we don't really want them, a technique that is used is to pass the output of the WMIC command through a second FOR /F command, which will remove the extra line feeds.  Makes the code a bit longer, but gets the result you want.

I also noticed you changed this:

set Free=!Free:~0,-9!
set Size=!Size:~0,-9!
echo !Caption!, Free=!Free! GB, Size=!Size! GB

to this:

set Free=!Free:~0,-10!
set Size=!Size:~0,-10!
echo !Caption!, Free=!Free! MB, Size=!Size! MB

If you really wanted MB instead of GB then that is wrong.  It would need to be:

set Free=!Free:~0,-6!
set Size=!Size:~0,-6!
echo !Caption!, Free=!Free! MB, Size=!Size! MB

The -9, -10, and -6 are telling the SET command to remove the rightmost 9, 10 or 6 characters.  So in the case of a large number, it has the following effect:

set n=12345678901
echo !n:~0,-9!
echo !n:~0,-10!
echo !n:~0,-6!

produces:

12
1
12345

Each position removed from the right is the same as dividing by 10.  So removing 2 positions is the same as dividing by 100, etc.  As a result, to get GB you need to remove 9 positions, and to get MB you need to remove 6 positions.

@echo off
setlocal EnableDelayedExpansion

set listfile=C:\machines.txt
set outputfile=C:\dell\%computername%.csv

(
    for /F "usebackq" %%i in ("%listfile%") do (
        for /f "skip=2 tokens=*" %%w in ('wmic /node:%%i computersystem get name /format:csv 2^>NUL') do (
            for /f "tokens=2 delims=," %%A in ("%%w") do (
                echo ComputerSystem : %%A
            )
        )

        for /f "skip=2 tokens=*" %%w in ('wmic /node:%%i Cpu get Name /format:csv 2^>NUL') do ( 
            for /f "tokens=2 delims=," %%A in ("%%w") do (
                echo Cpu : %%A
            )
        )
    
        for /f "skip=2 tokens=*" %%w in ('wmic /node:%%i path win32_VideoController get name /format:csv 2^>NUL') do (
            for /f "tokens=2 delims=," %%A in ("%%w") do (
                echo VideoController: %%A
            )
        )

        for /f "skip=2 tokens=*" %%w in ('wmic /node:%%i ComputerSystem get TotalPhysicalMemory /format:csv 2^>NUL') do (
            for /f "tokens=2 delims=," %%A in ("%%w") do (
                set TotalPhysicalMemory=%%A
                set TotalPhysicalMemory=!TotalPhysicalMemory:~0,-9!
                echo TotalPhysicalMemory=!TotalPhysicalMemory! GB
            )
        )

        echo Drives:
        for /f "skip=2 tokens=*" %%w in ('wmic /node:%%i LogicalDisk Where DriveType^=3 Get Caption^,FreeSpace^,Size /format:csv 2^>NUL') do (
            for /f "tokens=2-4 delims=," %%A in ("%%w") do (
                set Caption=%%A
                set Free=%%B
                set Size=%%C
                set Free=!Free:~0,-9!
                set Size=!Size:~0,-9!
                echo !Caption!, Free=!Free! GB, Size=!Size! GB
                echo --------------------------------------------------------------------------------
            )
        )
    ) 
) > "%outputfile%"

Open in new window


»bp
lo ahmed

ASKER
i apologize, i sent you the test code i was using for something, my current code is actually using GB. So i should add another for command to get rif of these lines?

thank you

 
@echo off
setlocal EnableDelayedExpansion

set listfile=C:\machines.txt
set outputfile=C:\dell\%computername%.csv

(
  for /F "usebackq" %%i in ("%listfile%") do (
    for /f "skip=2 tokens=2 delims=," %%A in ('wmic /node:%%i computersystem get name /format:csv 

2^>NUL') do (
      echo ComputerSystem : %%A  
  )
 for /f "skip=2 tokens=2 delims=," %%A in ('wmic /node:%%i Cpu get Name /format:csv 2^>NUL') do ( 
echo Cpu : %%A
    )
    for /f "skip=2 tokens=2 delims=," %%A in ('wmic /node:%%i path win32_VideoController get name 

/format:csv 2^>NUL') do (
      echo VideoController: %%A
    )
    for /f "skip=2 tokens=2 delims=," %%A in ('wmic /node:%%i ComputerSystem get 

TotalPhysicalMemory /format:csv 2^>NUL') do (

      set TotalPhysicalMemory=%%A
      set TotalPhysicalMemory=!TotalPhysicalMemory:~0,-10!
            echo  TotalPhysicalMemory=!TotalPhysicalMemory! GB
    
    )
    echo Drives:
    for /f "skip=2 tokens=2-4 delims=," %%A in ('wmic /node:%%i LogicalDisk Where DriveType^=3 Get 

Caption^,FreeSpace^,Size /format:csv 2^>NUL') do (
      set Caption=%%A
      set Free=%%B
      set Size=%%C
      set Free=!Free:~0,-10!
      set Size=!Size:~0,-10!
     echo !Caption!, Free=!Free! GB, Size=!Size! GB
echo --------------------------------------------------------------------------------
    )
  ) 
) > "%outputfile%"

Open in new window

ASKER CERTIFIED SOLUTION
Bill Prew

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
lo ahmed

ASKER
Great, this works. Thank you!
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Bill Prew

Welcome.


»bp