[SOLVED] Extract data to .txt file and then sort new file.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
PAB
Posts: 139
Joined: 12 Aug 2019 13:57

[SOLVED] Extract data to .txt file and then sort new file.

#1 Post by PAB » 23 May 2020 12:47

Good evening,

This script extracts the data to "%userprofile%\Desktop\test.txt" exactly as required.
I am having problems though with copy the first 3 rows from the file above and then extracting all the rows with the word FALSE in it and then sorting the file on row 4 downwards.

Code: Select all

@echo off

if exist "%userprofile%\Desktop\test.txt" (Del /f /q "%userprofile%\Desktop\test.txt" >NUL 2>&1)
echo.
driverquery /si > "%userprofile%\Desktop\test.txt"
Here I would like to copy the first 3 rows in "%userprofile%\Desktop\test.txt" into "%userprofile%\Desktop\Drivers1.txt".
Then find all rows with FALSE in from line 4 onwards in "%userprofile%\Desktop\test.txt" and copy it into "%userprofile%\Desktop\Drivers1.txt".
Then alphabetically sort "%userprofile%\Desktop\Drivers1.txt" from row 4 onwards.

Code: Select all

>"%userprofile%\Desktop\Drivers1.txt" findstr "\<FALSE\>" "%userprofile%\Desktop\test.txt"
del "%userprofile%\Desktop\test.txt"
echo.

Pause
First few rows of the original file "%userprofile%\Desktop\test.txt" output . . .

Code: Select all

DeviceName                     InfName       IsSigned Manufacturer             
============================== ============= ======== =========================
Generic volume                 volume.inf    TRUE     Microsoft                
Generic volume                 volume.inf    TRUE     Microsoft                
Generic volume                 volume.inf    TRUE     Microsoft                
Volume Manager                 machine.inf   TRUE     (Standard system devices)
DataTraveler 2.0               wpdfs.inf     TRUE     Kingston                 
UMBus Enumerator               umbus.inf     TRUE     Microsoft                
UMBus Root Bus Enumerator      umbus.inf     TRUE     Microsoft                
Microsoft System Management BI machine.inf   TRUE     (Standard system devices)
Plug and Play Software Device  machine.inf   TRUE     (Standard system devices)
Terminal Server Mouse Driver   machine.inf   TRUE     (Standard system devices)
Terminal Server Keyboard Drive machine.inf   TRUE     (Standard system devices)
Terminal Server Device Redirec machine.inf   TRUE     (Standard system devices)
Printer Port                   msports.inf   TRUE     (Standard port types)    
WAN Miniport (SSTP)            netsstpa.inf  TRUE     Microsoft                
WAN Miniport (PPTP)            netrasa.inf   TRUE     Microsoft                
WAN Miniport (PPPOE)           netrasa.inf   TRUE     Microsoft                
WAN Miniport (IPv6)            netrasa.inf   TRUE     Microsoft                
WAN Miniport (IP)              netrasa.inf   TRUE     Microsoft                
WAN Miniport (Network Monitor) netrasa.inf   TRUE     Microsoft                
WAN Miniport (L2TP)            netrasa.inf   TRUE     Microsoft                
User Mode Driver Frameworks Pl N/A           FALSE    N/A                      
Kernel Mode Driver Frameworks  N/A           FALSE    N/A                      
Remote Access IPv6 ARP Driver  N/A           FALSE    N/A                      
Storage volumes                N/A           FALSE    N/A                      
Dynamic Volume Manager         N/A           FALSE    N/A                      
VgaSave                        N/A           FALSE    N/A                      
Thanks in advance.
Last edited by PAB on 04 Jun 2020 14:31, edited 1 time in total.

Hackoo
Posts: 103
Joined: 15 Apr 2014 17:59

Re: Extract data to .txt file and then sort new file.

#2 Post by Hackoo » 23 May 2020 15:42

Hi :wink:
Did you mean something like that :?: :roll:

Code: Select all

driverquery /SI | find /I "FALSE" | Sort

PAB
Posts: 139
Joined: 12 Aug 2019 13:57

Re: Extract data to .txt file and then sort new file.

#3 Post by PAB » 23 May 2020 18:04

Hackoo wrote:
23 May 2020 15:42
Did you mean something like that :?:

Code: Select all

driverquery /SI | find /I "FALSE" | Sort
Thank you for that, I really appreciate it, it is so close!

I actually want to KEEP the FIRST 3 ROWS [ the titles ] output from driverquery /si, and then have from LINE 4 onwards to SORT alphabetically on the rows with FALSE.

I did achieve it using the below code but it is not very manageable because I need to do the same thing for several other files. I don't really want to have to hard code the FIRST 3 ROWS [ the titles ] in all the other files!

Code: Select all

@echo off
if exist "%userprofile%\Desktop\test.txt" (Del /f /q "%userprofile%\Desktop\test.txt" >NUL 2>&1)
echo. >> "%userprofile%\Desktop\test.txt"
echo DeviceName                     InfName       IsSigned Manufacturer >> "%userprofile%\Desktop\test.txt"
echo ============================== ============= ======== ========================= >> "%userprofile%\Desktop\test.txt"
driverquery /si | find /i "FALSE" | Sort >> "%userprofile%\Desktop\test.txt"
echo.
Pause

Hackoo
Posts: 103
Joined: 15 Apr 2014 17:59

Re: Extract data to .txt file and then sort new file.

#4 Post by Hackoo » 23 May 2020 20:09

Hope this what you want :roll:

Code: Select all

@echo off
Title Extract data to a text file and then sort new file
Set "LogFile=%userprofile%\Desktop\test.txt"

> "%LogFile%" (
	@for /f "tokens=*" %%a in ('Driverquery /SI ^| find /I "IsSigned"') do echo %%a
	@for /f "tokens=*" %%b in ('Driverquery /SI ^| find /I "="') do echo %%b
	Driverquery /SI /NH /FO Table | Find /I "FALSE" | Sort 
)

If Exist "%LogFile%" Start "" "%LogFile%"

PAB
Posts: 139
Joined: 12 Aug 2019 13:57

Re: Extract data to .txt file and then sort new file.

#5 Post by PAB » 24 May 2020 08:34

Hello Hackoo, I hope you and your family are OK and keeping safe!

That code was great thank you.

I have used it [ with adjustments etc ] to sort out all but 2 of my files.
If you could just have a look over the 2 below and point me in the right direction please I would be grateful!

FILE 1:

This is the code . . .

Code: Select all

@echo off

Set "msinfo="C:\Program Files\Common Files\Microsoft Shared\MSInfo\msinfo32.exe""

%msinfo% /categories ProblemDevices /Report "%userprofile%\Desktop\tmp.txt"
Type "%userprofile%\Desktop\tmp.txt" | Find /i "The drivers for this device are not installed" | Sort > "%userprofile%\Desktop\Prob.txt"

Pause
This is the output in "%userprofile%\Desktop\Prob.txt" . . .
Device PNP Device ID Error Code
Not Available HDAUDIO\FUNC_02&VEN_11C1&DEV_1040&SUBSYS_11C10001&REV_1002\4&13EFD460&0&0101 The drivers for this device are not installed.
PCI FLASH Memory PCI\VEN_1524&DEV_0551&SUBSYS_002014C0&REV_01\4&2192B26C&0&24F0 The drivers for this device are not installed.
The code works perfectly, but is there anyway that the code can be condensed to one line please?

FILE 2:

This is the code . . .

Code: Select all

@echo off

wmic /Output:"%userprofile%\Desktop\tmp.txt" PROCESS Get Name, SessionID, WorkingSetSize /Format:Table
type "%userprofile%\Desktop\tmp.txt" | Find /v "0" | Sort > "%userprofile%\Desktop\Process.txt"

Pause
This is the output in "%userprofile%\Desktop\Process.txt" . . .
Name SessionId WorkingSetSize
System Idle Process 0 12288
System 0 1511424
smss.exe 0 577536
csrss.exe 0 3227648
wininit.exe 0 3153920
csrss.exe 1 19681280
winlogon.exe 1 4042752
services.exe 0 5771264
lsass.exe 0 2043904
I need it to find ZERO only and then SORT the file.
Is there also anyway that the code can be condensed to one line please?

I have tried . . .

Code: Select all

type "%userprofile%\Desktop\Proc.txt" | Find 0        | Sort > "%userprofile%\Desktop\Process.txt"
type "%userprofile%\Desktop\Proc.txt" | Find "0"      | Sort > "%userprofile%\Desktop\Process.txt"
type "%userprofile%\Desktop\Proc.txt" | Find /I "0"   | Sort > "%userprofile%\Desktop\Process.txt"
type "%userprofile%\Desktop\Proc.txt" | Find /I 0     | Sort > "%userprofile%\Desktop\Process.txt"
type "%userprofile%\Desktop\Proc.txt" | Find /v 0     | Sort > "%userprofile%\Desktop\Process.txt"
type "%userprofile%\Desktop\Proc.txt" | Find /v "0"   | Sort > "%userprofile%\Desktop\Process.txt"
type "%userprofile%\Desktop\Proc.txt" | Find /v  eq 0 | Sort > "%userprofile%\Desktop\Process.txt"
This works for just sorting but I can't get it to find 0 [ zero ] . . .

Code: Select all

type "%userprofile%\Desktop\Proc.txt" | Sort > "%userprofile%\Desktop\Proc1.txt"
Thanks in advance.

UPDATE:

I found a solution . . .

Code: Select all

@echo off

wmic /Output:"%userprofile%\Desktop\tmp.txt" PROCESS Get Name, SessionID, WorkingSetSize /Format:Table
Type "%userprofile%\Desktop\tmp.txt" | findstr "\<0\>" | Sort > "%userprofile%\Desktop\Process.txt"

Pause
Is there anyway though that the 2 lines can be condensed to one line please?

pieh-ejdsch
Posts: 239
Joined: 04 Mar 2014 11:14
Location: germany

Re: Extract data to .txt file and then sort new file.

#6 Post by pieh-ejdsch » 24 May 2020 09:28

Hallo PAB,

use this

Code: Select all

find " 0 "
Phil

PAB
Posts: 139
Joined: 12 Aug 2019 13:57

Re: Extract data to .txt file and then sort new file.

#7 Post by PAB » 24 May 2020 17:02

pieh-ejdsch wrote:
24 May 2020 09:28
use this

Code: Select all

find " 0 "
Thanks Phil, that does exactly what I was after!

PAB
Posts: 139
Joined: 12 Aug 2019 13:57

Re: Extract data to .txt file and then sort new file.

#8 Post by PAB » 24 May 2020 17:22

What I would finally like to do is to search a .txt file for the following sentences and have the rows that they appear in output to a file.

The code below works great when finding "The drivers for this device are not installed". . . .

Code: Select all

@echo off

Set "msinfo="C:\Program Files\Common Files\Microsoft Shared\MSInfo\msinfo32.exe""

%msinfo% /categories ProblemDevices /Report "%userprofile%\Desktop\tmp.txt"
Type "%userprofile%\Desktop\tmp.txt" | Find /i "The drivers for this device are not installed" | Sort > "%userprofile%\Desktop\Prob.txt"

Pause
What I would like to do is to search for a lot more sentences as well. Here are just a few [ there are 50 in total ] . . .
“This device is not configured correctly.“
“The driver for this device might be corrupted, or your system may be running low on memory or other resources.“
“Windows cannot identify this hardware because it does not have a valid hardware identification number.“
“This device cannot start.“
“This device cannot find enough free resources that it can use.“
“This device cannot work properly until you restart your computer. “
“Windows cannot identify all the resources this device uses.“
I have tried for the last five hours to come up with a method/solution to achieve this but to no avail.

I think it probably needs an array or something similar!

Thanks in advance.

Hackoo
Posts: 103
Joined: 15 Apr 2014 17:59

Re: Extract data to .txt file and then sort new file.

#9 Post by Hackoo » 24 May 2020 22:25

The help of this command may be put you on the right direction :wink: findstr /?
/G:StringsFile Get search string from a file (/ stands for console).
You need to put all your strings into a file and use this syntax :

Code: Select all

findstr /G:"Filter_File.txt"

So you can write something like that :

Code: Select all

@echo off
Color 0A & Mode 70,3
Title Extract data to .txt file and then sort new file.
echo(
echo         Please be patient ... this action can took a while ...
msinfo32.exe /categories ProblemDevices /Report "%userprofile%\Desktop\tmp.txt"
Set "Filter_File=%~dp0Filter.txt"

>"%Filter_File%" (
	echo This device is not configured correctly.
	echo The driver for this device might be corrupted, or your system may be running low on memory or other resources.
	echo Windows cannot identify this hardware because it does not have a valid hardware identification number.
	echo This device cannot start.
	echo This device cannot find enough free resources that it can use.
	echo This device cannot work properly until you restart your computer.
	echo Windows cannot identify all the resources this device uses.
	echo The drivers for this device are not installed
)

Type "%userprofile%\Desktop\tmp.txt" | Findstr /I /G:"%Filter_File%" | Sort > "%userprofile%\Desktop\Prob.txt"
If exist "%userprofile%\Desktop\Prob.txt" Start "" "%userprofile%\Desktop\Prob.txt"

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Extract data to .txt file and then sort new file.

#10 Post by dbenham » 25 May 2020 10:28

Your original request to preserve the first 3 title lines, and then extract and sort the remaining lines that contain FALSE, can be easily solved with JSORT.BAT, plus a little sideways thinking with your use of FINDSTR.

JSORT is a hybrid JScript/batch with a number of useful options:
  • Skip n lines (don't sort them - keep them in place)
  • Case sensitive sorts
  • Limit the number of sorted lines to print, useful for getting the top n records
  • Sort numeric strings (or substrings) numerically
  • Only write unique lines
  • Sort each line based on substring starting at position P within the nth token
The other needed trick is to think of your search in reverse - find all lines that don't contain TRUE. This will leave the first 3 lines, as well as all the data lines that include FALSE.

So your original task can be as simple as:

Code: Select all

driverquery /si | findstr /v "\<TRUE\>" | jsort /s 3 /i
I assume you want your sort to ignore case so I included the /i option. Unlike the native SORT command, JSORT differentiates by case unless the /i option is used.


Dave Benham

PAB
Posts: 139
Joined: 12 Aug 2019 13:57

Re: Extract data to .txt file and then sort new file.

#11 Post by PAB » 27 May 2020 03:14

Thank you to everyone who contributed to my request, it is VERY much appreciated!

Post Reply