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

MAINCOMPUTER,Number1 Antivirus
MAINCOMPUTER,Number2 Antivirus
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
type %inputfile%

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:
setlocal disabledelayedexpansion

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



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.

  • Pretty close, and much cleaner than my previous mess... I did make the change for the namespace tag since I didn't realize the forum doesn't like double backslashes... But back to the code, I do still get the following error message "The filename, directory name, or volume label syntax is incorrect." – Flaver-D Jun 2 '17 at 19:44
  • Did an edit to the answer. To know what/where the error is run the batch with echo on and a pause at the end. – LotPings Jun 2 '17 at 21:08
  • I just ran the code in a shell and it seems the problem is in the (>>"%%B.txt" Echo:%%A,%%B) part. I also fixed the namespace address so you'll be able to run it on your machine. – Flaver-D Jun 3 '17 at 18:28
  • Problem is the damn wmic output in Unicode which is converted to current codepage but leaves cr,cr,lf sequences which have to be filtered out. On the way to change the above batch. – LotPings Jun 3 '17 at 18:55
  • I too can't figure out why MS decided to do this... That's why in my script attempt I removed every space in the entries. It did help a bit... I think. – Flaver-D Jun 4 '17 at 15:37

Here's what I finally ended up making

Not as nice as the answer up above but it still works.

For /f "skip=2 tokens=1,2 delims=," %%A in (     
  'WMIC.exe /Node:localhost /Namespace:\\\root\SecurityCenter2 Path AntiVirusProduct Get displayName /Format:csv '
) Do (    
set str=%%a     
set str=%str:'=%    
set str=%str:,=%    
)|Find /i "%%A,%%B" "%%B.txt" >NUL 2>&1|(echo.>>"%%B.txt")    

But since I like single line scripts, I'll be ditching this solution for the cleanest looking one

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.