Avatar of samiam41
samiam41
Flag for United States of America asked on

Sort command for wmic used in a batch script

Hey Experts.  Wasn't sure if this was even possible with a batch file so if the answer is no, that works for me.  If it can be done but with Powershell or other utility, great, but that isn't an option right now.


Essentially I have an in-house script that our helpdesk launches on the end-users PC that gathers various pieces of info when they are trouble-shooting an issue.  They review the details of the log file and decide how to proceed.  One piece of info captured relates to which Windows Updates are installed.


wmic qfe get hotfixid,installedon >> %logfile%


The output is great but I want the results sorted by the date the update was installed or by the HotFixID.  I wasn't sure what the format option would be for that.


HotFixID   InstalledOn
KB5004333  8/20/2021
KB4498523  7/30/2019
KB4503308  7/25/2019
KB4508433  7/30/2019
KB4509096  7/24/2019
KB4515383  9/12/2019
KB4516115  9/13/2019
KB4517245  11/18/2020

Windows OS* BatchfilesWindows BatchWindows 10

Avatar of undefined
Last Comment
samiam41

8/22/2022 - Mon
NVIT

This sorts by HotFix column into hotfix.txt:
set tempfile=%temp%\temp_1.txt
set logfile=%temp%\hotfix.txt
wmic qfe get hotfixid,installedon >%tempfile%
rem Keep this line else next line doesn't work
type %tempfile% | sort>%logfile%

Open in new window

ASKER CERTIFIED SOLUTION
Lee W, MVP

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
samiam41

ASKER
Thanks NVIT!  

I added your code to the script but it didn't go well.  Here is the original script:

SET mm=%date:~4,2%
SET dd=%date:~7,2%
SET yy=%date:~12,2%
SET hh=%time:~0,2%
SET min=%time:~3,2%
SET ss=%time:~6,2%


SET logfile="c:\users\%username%\desktop\DIAG_%mm%%dd%%yy%_%hh%%min%%ss%.html"


echo ===================================== > %logfile%
echo [SCRIPT DETAILS] >> %logfile%
echo ===================================== >> %logfile%
echo ^<pre^> >> %logfile%
echo TECH ASSIST RUN LOG >> %logfile%
echo Version 1.4 >> %logfile%
echo GoLiveDate 1.27.20 >> %logfile%
echo +++++++++++++++++++++++++++++++++++++ >> %logfile%
echo Script ran at: %time% %date% >> %logfile%
echo +++++++++++++++++++++++++++++++++++++ >> %logfile%
echo. >> %logfile%
echo. >> %logfile%


echo ===================================== >> %logfile%
echo [SYSTEM INFORMATION] >> %logfile%
echo (PC Time/Date, Computer Name, >> %logfile%
echo Boot Cycle, Tasklist, Services) >> %logfile%
echo ===================================== >> %logfile%
echo [PC TIME/DATE/TIME ZONE] >> %logfile%
echo %time% >> %logfile%
echo %date% >> %logfile%
echo [BUILD and VERSION Info] >> %logfile%
systeminfo | findstr /B /C:"OS Name" /C:"OS Version" >> %logfile%
echo. >> %logfile%
wmic qfe get hotfixid,installedon >> %logfile%

Open in new window

However when I add your code, I lose the logfile output.
SET mm=%date:~4,2%
SET dd=%date:~7,2%
SET yy=%date:~12,2%
SET hh=%time:~0,2%
SET min=%time:~3,2%
SET ss=%time:~6,2%


SET logfile="c:\users\%username%\desktop\DIAG_%mm%%dd%%yy%_%hh%%min%%ss%.html"


echo ===================================== > %logfile%
echo [SCRIPT DETAILS] >> %logfile%
echo ===================================== >> %logfile%
echo ^<pre^> >> %logfile%
echo TECH ASSIST RUN LOG >> %logfile%
echo Version 1.4 >> %logfile%
echo GoLiveDate 1.27.20 >> %logfile%
echo +++++++++++++++++++++++++++++++++++++ >> %logfile%
echo Script ran at: %time% %date% >> %logfile%
echo +++++++++++++++++++++++++++++++++++++ >> %logfile%
echo. >> %logfile%
echo. >> %logfile%


echo ===================================== >> %logfile%
echo [SYSTEM INFORMATION] >> %logfile%
echo (PC Time/Date, Computer Name, >> %logfile%
echo Boot Cycle, Tasklist, Services) >> %logfile%
echo ===================================== >> %logfile%
echo [PC TIME/DATE/TIME ZONE] >> %logfile%
echo %time% >> %logfile%
echo %date% >> %logfile%
echo [BUILD and VERSION Info] >> %logfile%
systeminfo | findstr /B /C:"OS Name" /C:"OS Version" >> %logfile%
set tempfile=%temp%\temp_1.txt
set logfile=%temp%\hotfix.txt
wmic qfe get hotfixid,installedon >%tempfile%
rem Keep this line else next line doesn't work
type %tempfile% | sort>%logfile%

Open in new window

samiam41

ASKER
@Lee, of course it would be something easy!

"Sort" did work, is there a way to sort it ascending (looks like descending now)?
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
samiam41

ASKER
wmic qfe get hotfixid | sort /r

Open in new window

/r

Thanks for pointing me in the right direction!!  Much appreciated.
NVIT

wmic qfe get hotfixid,installedon >> %logfile%

Open in new window


For me, it places blank lines between each valued line. That's why I made a 2nd file,  i.e.

HotFixID   InstalledOn  

KB4562830  1/6/2021     

KB4570334  9/27/2020    

KB4577266  9/27/2020    

KB4577586  2/23/2021    
...

Open in new window

oBdA

Sorting by ID is not the same as sorting by date; here's your output sorted by date, for example:
2019-07-24 KB4509096
2019-07-25 KB4503308
2019-07-30 KB4498523
2019-07-30 KB4508433
2019-09-12 KB4515383
2019-09-13 KB4516115
2020-11-18 KB4517245
2021-08-20 KB5004333

Open in new window


So for the fun of it, here's a version that actually sorts by date (note: there are two additional lines at the top as well, it's not just the part from line 41 on):
@echo off
setlocal enabledelayedexpansion

SET mm=%date:~4,2%
SET dd=%date:~7,2%
SET yy=%date:~12,2%
SET hh=%time:~0,2%
SET min=%time:~3,2%
SET ss=%time:~6,2%


SET logfile="c:\users\%username%\desktop\DIAG_%mm%%dd%%yy%_%hh%%min%%ss%.html"


echo ===================================== > %logfile%
echo [SCRIPT DETAILS] >> %logfile%
echo ===================================== >> %logfile%
echo ^<pre^> >> %logfile%
echo TECH ASSIST RUN LOG >> %logfile%
echo Version 1.4 >> %logfile%
echo GoLiveDate 1.27.20 >> %logfile%
echo +++++++++++++++++++++++++++++++++++++ >> %logfile%
echo Script ran at: %time% %date% >> %logfile%
echo +++++++++++++++++++++++++++++++++++++ >> %logfile%
echo. >> %logfile%
echo. >> %logfile%


echo ===================================== >> %logfile%
echo [SYSTEM INFORMATION] >> %logfile%
echo (PC Time/Date, Computer Name, >> %logfile%
echo Boot Cycle, Tasklist, Services) >> %logfile%
echo ===================================== >> %logfile%
echo [PC TIME/DATE/TIME ZONE] >> %logfile%
echo %time% >> %logfile%
echo %date% >> %logfile%
echo [BUILD and VERSION Info] >> %logfile%
systeminfo | findstr /B /C:"OS Name" /C:"OS Version" >> %logfile%
echo. >> %logfile%

set TmpFile=%Temp%\~Hotfix.tmp
set InstalledOn=
>>%logfile% echo InstalledOn HotFixID
(for /f "delims=" %%a in ('wmic.exe qfe get HotFixID^,InstalledOn /value') do (for /f "delims=" %%b in ("%%a") do (
	set %%b
	if defined InstalledOn (
		for /f "tokens=1-3 delims=/" %%d in ("!InstalledOn!") do (
			if %%d lss 10 (set mm=0%%d) else (set mm=%%d)
			if %%e lss 10 (set dd=0%%e) else (set dd=%%e)
			set yy=%%f
		)
		echo !yy!-!mm!-!dd!  !HotFixID!
		set InstalledOn=
	)
))) >"%TmpFile%"
sort.exe "%TmpFile%" >> %logfile%
del "%TmpFile%"

Open in new window

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
samiam41

ASKER
I definitely appreciate the follow-up suggestions and clarifications.  I hope to be able to return to this project tomorrow (Thu) as we're implementing an enterprise wide policy change in printer driver install and needed this functionality in the Tech Assist app for trouble-shooting.