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 offsetlocal EnableDelayedExpansionset listfile=C:\machines.txtset 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! MBecho -------------------------------------------------------------------------------- ) ) ) > "%outputfile%"
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 offsetlocal EnableDelayedExpansionset listfile=C:\machines.txtset 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%"
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 offsetlocal EnableDelayedExpansionset listfile=C:\machines.txtset 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! GBecho -------------------------------------------------------------------------------- ) ) ) > "%outputfile%"
I also noticed you changed this:
set Size=!Size:~0,-9!
echo !Caption!, Free=!Free! GB, Size=!Size! GB
to this:
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 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:
echo !n:~0,-9!
echo !n:~0,-10!
echo !n:~0,-6!
produces:
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.
Open in new window
»bp