Thursday, November 20, 2014

windows - How to Create individual text files from a wmic output


I want to create individual text files from each entry in a wmic output.


So far I have



WMIC.exe /Node:localhost /Namespace:\\root\SecurityCenter2 Path AntiVirusProduct Get displayName /Format:csv>AVlist.txt
WMIC.exe /Node:localhost /Namespace:\\root\SecurityCenter2 Path AntiSpywareProduct Get displayName /Format:csv>>AVlist.txt



that give me a list with the following



Node,displayName
MAINCOMPUTER,Number1 Antivirus
MAINCOMPUTER,Number2 Antivirus
Node,displayName
MAINCOMPUTER,Number2 Antivirus
MAINCOMPUTER,Number1 Antispyware



What I want to do is create a series of text files that would be labled



Number1 Antivirus.txt
Number2 Antivirus.txt
Number1 Antispyware.txt



without having any duplicates, without overwriting any existing files and without saving the "Node,displayName" headers created each time wmic is run...


Now I've been struggling with this for a few days and have come up with a huge mess of a file that heavily relies on creating temp files and deleting them after... not elegant, overly complicated, I just hate it. So, I'm wondering if any wizards around here would have a simpler solution?


Here's the mess I've made so far. Don't look too closely at the shoddy coding.



WMIC.exe /Node:localhost /Namespace:\\root\SecurityCenter2 Path AntiVirusProduct Get displayName /Format:csv>AVlist.txt
WMIC.EXE /Node:localhost /Namespace:\\root\SecurityCenter2 Path AntiSpywareProduct Get displayName /Format:csv>>AVlist.txt
:: remove white spaces
for /f "skip=1 delims=" %%a in (AVlist.txt) do (
set str=%%a
set str=%str:'=%
set str=%str:,=%
echo %str% >>%temp%\filetmp.txt
)
xcopy %temp%\filetmp.txt %~dp0\AVlist.txt /y
del %temp%\filetmp.txt /f /q


:: remove duplicate lines
setlocal enabledelayedexpansion
set inputfile=AVlist.txt
set outputfile=AVlist2.txt


echo File to be processed
echo.
type %inputfile%
echo.


if exist sorted.txt del sorted.txt
sort %inputfile% /O %temp%\sorted.txt


if exist %outputfile% del %outputfile%
set lastline=
for /f "delims==" %%L in (sorted.txt) do (
set thisline=%%L
if not "!thisline!"=="!lastline!" echo !thisline!>>%outputfile%
set lastline=%%L
)


del sorted.txt


echo Duplicates removed:
echo.
setlocal disabledelayedexpansion


:: remove header
ren AVlist2.txt AVlist2.txt.old
findstr /v /b /c:"Node,displayName" AVlist2.txt.old > AVlist2.txt
type avlist2.txt
pause



Yikes!


Answer



If I understand right what you try to accomplish, this batch will do:


@Echo off
For /f "skip=1 tokens=1,2 delims=," %%A in (
'WMIC.exe /Namespace:\\root\SecurityCenter2 Path AntiVirusProduct Get displayName /Format:csv^|find "," '
) Do For /f "delims=" %%C in ("%%B") Do Find /I "%%A,%%C" "%%C.txt" >NUL 2>&1 ||(>> "%%C.txt" Echo:%%A,%%C)
For /f "skip=1 tokens=1,2 delims=," %%A in (
'WMIC.exe /Namespace:\\root\SecurityCenter2 Path AntiSpywareProduct Get displayName /Format:csv^|find ","'
) Do For /f "delims=" %%C in ("%%B") Do Find /I "%%A,%%C" "%%C.txt" >NUL 2>&1 ||(>> "%%C.txt" Echo:%%A,%%C)




  • There is no need for temporary files and sorting,

  • directly parsing wmic output with a for /f and

  • skip=1 to get rid of the header,

  • tokens=1,2 delims=," to split the lines and store node to %%A and displayname to %%B

  • find to check if the entry is already present in the destination file.


EDIT3 Fixed batch, should work now.


No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...