Jump to content
Sign in to follow this  
Bkay8686

Simple search for text in text file

Recommended Posts

Bkay8686

Hello All,

Very new to AutoIT and scripting in general.  I am having a difficult time finding any relevant help to the simple task I need to execute.  Essentially, I have a text file that is an output of WMIC that shows all software installed on a machine.  I need to search for a specific product name.  If it exists, I will do X, if not do Y.  However, I cannot figure out the best way to do so.  I've looked into FileOpenFileRead and FileReadLine but I can't seem to figure out how to use them for what I need.  I would love something that functions like ControlGetText but I am obviously not opening a window, I am searching in a text file. Any and all help would be greatly appreciated.

Share this post


Link to post
Share on other sites
Melba23

Bkay8686,

First, welcome to the AutoIt forums. :)

What you want to do is very easy - read the file into a variable and check if the required text is present:

If StringInStr(FileRead("FileName"), "AppName") Then
    ; Code if app name found
Else
    ; Code if not found
EndIf
Please ask if you have any questions. :)

M23


Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______Open Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
Bkay8686

Melba23,

Thank you SO much.  I have spent about day and a half trying to find the best way to do this.  I appreciate the quick and thorough response. :thumbsup:

Share this post


Link to post
Share on other sites
Melba23

Bkay8686,

Glad I could help. :)

M23


Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______Open Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
Bkay8686

Since I was helped so quickly, I figured I would post the lines that do exactly what I needed:

1. Export all installed software on a machine into a text file

2. Search for a specific software nameversion

3. Create a "results" file that displays if what I needed was installed or not

The purpose behind this is to allow for less-technical folks to see if our servers are setup properly before our stores open.  Hope this helps someone else...

;Run WMIC.exe to export all software to a text file
RunWait("C:\WINDOWS\System32\wbem\wmic.exe /output:C:\software.txt product get Name, Version" , "C:\WINDOWS\System32\wbem" , @SW_HIDE)

;Assign a variable to the results file that will need to be created
Global $results = FileOpen("C:\Results.txt" , $FO_APPEND)

;Search for specific text line in the WMIC.exe output file and write the success\failure to the results.txt file
If StringInStr(FileRead("C:\software.txt"), "My Software Example") Then
      FileWriteLine($results , "My Software Example is installed")
 Else
      FileWriteLine($results , "ATTENTION My Software Example is NOT installed")
   EndIf
   
FileClose($results)

Thanks again Melba

Share this post


Link to post
Share on other sites
jdelaney

If you use an INI, or XML file, it will be easier to create, and then re-read, later.

; once you have the collection of apps:
Local $aApplications[5][2]=[ _
["app1","1.1.2.3"], _
["app2","2.1.2.3"], _
["app3","3.1.2.3"], _
["app4","4.1.2.3"], _
["app5","5.1.2.3"]]

$sXMLOutput = @DesktopDir & "\Test.xml"
; output to xml
$oXML = ObjCreate("Microsoft.XMLDOM")
$oXML.loadXML("<Applications></Applications>")
$oApplications = $oXML.documentelement

For $i = 0 To UBound($aApplications)-1
    $oApplication = $oXML.createElement("Application")
    $oApplication.setAttribute("name",$aApplications[$i][0])
    $oApplication.setAttribute("version",$aApplications[$i][1])
    $oApplications.appendChild($oApplication)
Next
ConsoleWrite($oXML.xml)
$oXML.save($sXMLOutput)

; example of querying later
$oApplications = $oXML.SelectNodes("//Application")
For $oApplication In $oApplications
    ConsoleWrite("Application Name=" & $oApplication.getAttribute("name") & ", version=" & $oApplication.getAttribute("version") & @CRLF)
Next
ConsoleWrite(@CRLF)
; example of querying specific application
$oApplication = $oXML.SelectSingleNode("//Application[@name='app3']")
ConsoleWrite("Application Name=" & $oApplication.getAttribute("name") & ", version=" & $oApplication.getAttribute("version") & @CRLF)

output, example:

Application Name=app1, version=1.1.2.3
Application Name=app2, version=2.1.2.3
Application Name=app3, version=3.1.2.3
Application Name=app4, version=4.1.2.3
Application Name=app5, version=5.1.2.3

Application Name=app3, version=3.1.2.3

But regexp's would work too, as long as you had some sort of naming convention durring the writing.

Edited by jdelaney

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×