Jump to content
Sign in to follow this  
SuperFletch

Read specific part of a text file:

Recommended Posts

SuperFletch

I need to read a very specific part of a text file and I'm struggling:

My text file lists software (and GUIDs for software) that a particular PC has installed.

A single line of the text file reads like this and typically there may be up to 500 lines per file.

PCNAME {24531475-94B9-4A91-B4AA-16158C6AE026} SOFTWARE NAME VER.NUM.OF.SW SOFTWARE SUPPLIER DATE

I know the name of the piece of software and I can find that inside the text file no problem, but I need to get the GUID out.

Here's what I've tried....

This didn't work, I tried using _StringBetween and stating the PCName and the Software to return the GUID but it didn't work.

#Include<File.au3>
#Include <String.au3>
$Hostname = ("PCNAME")
$File = ("C:\Remote Uninstall\PCNAME.txt")

$FileData = FileRead($File)
$GUID = _StringBetween($FileData,$Hostname,"SOFTWARE NAME")
MsgBox(0,"GUID",$GUID)

I also tried

$Hostname = ("PCNAME")
$File = ("C:\Remote Uninstall\PCNAME.txt")
$FileData = FileRead($File)
$Position = StringInStr($FileData,"SOFTWARE NAME")

MsgBox(0,"SOFTWARE NAME","String SOFTWARE NAME occurs at position - " & $Position)

$StartPos = $Position - 39                     ;take away 39 characters from the position found to move to the start of the GUID

FileOpen($File,2)
FileSetPos($File,$StartPos,0)                ;set position in the file
FileRead($File,38)                                 ;tried reading 38 characters from the start position.

I feel String Regular Expression is probably what I need but I can't see how I'd use it?

Any ideas welcome, I've attached a sample text file.

Share this post


Link to post
Share on other sites
JohnOne

#Include<Array.au3>
#Include <String.au3>
$Hostname = ("PCNAME")
$File = ("C:\Remote Uninstall\PCNAME.txt")

$FileData = FileRead($File)
$GUID = _StringBetween($FileData,$Hostname,"SOFTWARE NAME")
_ArrayDisplay($GUID)

_StringBetween returns an array.


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites
Mechaflash

never used stringbetween for anything... but wouldn't

$GUID = _StringBetween($FileData, '{', '}')

work for that?

EDIT: Also as John stated, it returns an array. So it would be more like

$x = 0
$sGUID = ''
$GUID = _StringBetween($FileData, '{', '}')

If @error then
  msgbox(0,"","Sorry charlie... couldn't find it")
else
  for $item in $GUID
    $sGUID&=$item & @CRLF
    $x+=1
  next
  msgbox(0,"", "Found a total of " & $x & " entries:" & @CRLF & $sGUID)
endif
Edited by Mechaflash

Spoiler

“Hello, ladies, look at your man, now back to me, now back at your man, now back to me. Sadly, he isn’t me, but if he stopped using ladies scented body wash and switched to Old Spice, he could smell like he’s me. Look down, back up, where are you? You’re on a boat with the man your man could smell like. What’s in your hand, back at me. I have it, it’s an oyster with two tickets to that thing you love. Look again, the tickets are now diamonds. Anything is possible when your man smells like Old Spice and not a lady. I’m on a horse.”

 

Share this post


Link to post
Share on other sites
Nessie

I dont see the sample text file ;) But you can do something like that

$Hostname = "PCNAME"

$sString = "PCNAME   {24531475-94B9-4A91-B4AA-16158C6AE026} SOFTWARE NAME VER.NUM.OF.SW SOFTWARE SUPPLIER DATE"

$sPattern = $Hostname & "   {(.*?)}"
$aRegex = StringRegExp($sString, $sPattern, 3)

If @error Then
    MsgBox(0,"Error", "Unable to get the GUID")
    Exit
EndIf

MsgBox(0, "Done", "Your software GUID is: " & $aRegex[0])

Hi!

Edited by Nessie

My UDF: NetInfo UDF Play with your network, check your download/upload speed and much more! YTAPI Easy to use YouTube API, now you can easy retrive all needed info from a video. NavInfo Check if a specific browser is installed and retrive other usefull information. YWeather Easy to use Yahoo Weather API, now you can easily retrive details about the weather in a specific region. No-IP UDF Easily update your no-ip hostname(s).

My Script: Wallpaper Changer Change you wallpaper dinamically, you can also download your wallpaper from your website and share it with all!   My Snippet: _ImageSaveToBMPConvert an image to bmp format. _SciteGOTO Open a file in SciTE at specific fileline. _FileToHex Show the hex code of a specified file

Share this post


Link to post
Share on other sites
SuperFletch

Thanks for the ideas.

Didn't realise _StringBetween returns an Array.

Looks like the text file didn't upload properly so I'll try again now.

Searching between the fancy brackets doesn't work as there are too many entries like that in the file:

Here's some more of the text file to help (in case I can't attach it again).

PCNAME {0B34DB03-1DA3-4C31-B86C-B2CD9692876F} Foundation Stage Profile 3.3.0 FSP\Version3  20111003
PCNAME {0EFDF2F9-836D-4EB7-A32D-038BD3F1FB2A} Security Update for CAPICOM (KB931906) 2.1.0.2 Microsoft Corporation  20110405
PCNAME {0FFC8EC3-38F4-4A3B-9D2D-6A56DDF4C97C} BDE 5.11 for SIMS Workstation Setup   
PCNAME {199B7F78-69B7-47C5-8D4B-A3ED1391FB6B} Microsoft Firewall Client 4.0.3442 Microsoft Corporation  20110401
PCNAME {24531475-94B9-4A91-B4AA-16158C6AE026} SOLUS 3 Agent 3.4.118.0 Capita Business Services Ltd  20120704
PCNAME {28b931ed-c100-474c-8c84-85f403a26a85}.sdb Valid Viewer Redirect Patch   
PCNAME {350C97B0-3D7C-4EE8-BAA9-00BCB3D54227} WebFldrs XP 9.50.7523 Microsoft Corporation  20100906
PCNAME {3C3901C5-3455-3E0A-A214-0B093A5070A6} Microsoft .NET Framework 4 Client Profile 4.0.30319 Microsoft Corporation  20130225
PCNAME {3C3901C5-3455-3E0A-A214-0B093A5070A6}.KB2160841 {3C3901C5-3455-3E0A-A214-0B093A5070A6}.KB2160841     KB2160841
PCNAME {3C3901C5-3455-3E0A-A214-0B093A5070A6}.KB2162169 {3C3901C5-3455-3E0A-A214-0B093A5070A6}.KB2162169     KB2162169
PCNAME {3C3901C5-3455-3E0A-A214-0B093A5070A6}.KB2446708 {3C3901C5-3455-3E0A-A214-0B093A5070A6}.KB2446708     KB2446708
PCNAME {3C3901C5-3455-3E0A-A214-0B093A5070A6}.KB2446708v2 {3C3901C5-3455-3E0A-A214-0B093A5070A6}.KB2446708v2     KB2446708v2
PCNAME {3C3901C5-3455-3E0A-A214-0B093A5070A6}.KB2468871 {3C3901C5-3455-3E0A-A214-0B093A5070A6}.KB2468871     KB2468871
PCNAME {3C3901C5-3455-3E0A-A214-0B093A5070A6}.KB2473228 Update for Microsoft .NET Framework 4 Client Profile (KB2473228)

PCNAME.txt

Share this post


Link to post
Share on other sites
Nessie

Tested and working:

$Hostname = "PCNAME"
$File_Path = @ScriptDir & "\PCNAME.txt"

$Open = FileOpen($File_Path)
$sRead = FileRead($Open)
FileClose($sRead)

$sName = InputBox("Enter the software name", "Enter the software name")

If $sName = "" Then
    MsgBox(16, "Error", "Please insert a valid software name")
    Exit
EndIf

$sPattern = $Hostname & "   {(.*?)}.*" & $sName
$aResult = StringRegExp($sRead, $sPattern, 3)

If @error Then
    MsgBox(16, "Error", "Unable to find the software GUID")
    Exit
EndIf

MsgBox(0, "Done", "Your software GUID is: " & $aResult[0])

Hi!

Edited by Nessie

My UDF: NetInfo UDF Play with your network, check your download/upload speed and much more! YTAPI Easy to use YouTube API, now you can easy retrive all needed info from a video. NavInfo Check if a specific browser is installed and retrive other usefull information. YWeather Easy to use Yahoo Weather API, now you can easily retrive details about the weather in a specific region. No-IP UDF Easily update your no-ip hostname(s).

My Script: Wallpaper Changer Change you wallpaper dinamically, you can also download your wallpaper from your website and share it with all!   My Snippet: _ImageSaveToBMPConvert an image to bmp format. _SciteGOTO Open a file in SciTE at specific fileline. _FileToHex Show the hex code of a specified file

Share this post


Link to post
Share on other sites
SuperFletch

Not for me it isn't!!

I get what you're doing and the StringRegExp seems sensible.

It just seems to @error when trying to apply the StringRegExp command, so I get the second message box.

I've tried stripping out some of the other variables, but I'm not getting anywhere.

Thanks anyway - you've given me some more ideas.

Share this post


Link to post
Share on other sites
Nessie

Have you tried the code after i have edit it? Can you give me the name of the software that you are looking for and doesn't work on my code?

Edit:

Try this:

$Hostname = "PCNAME"
$File_Path = @ScriptDir & "\PCNAME.txt"

$Open = FileOpen($File_Path)
$sRead = FileRead($Open)
FileClose($sRead)

$sName = InputBox("Enter the software name", "Enter the software name")

If $sName = "" Then
    MsgBox(16, "Error", "Please insert a valid software name")
    Exit
Else ;Escape special chars
    $sName = StringReplace($sName, ".", "\.")
    $sName = StringReplace($sName, "(", "\(")
    $sName = StringReplace($sName, ")", "\)")
    $sName = StringReplace($sName, "{", "\{")
    $sName = StringReplace($sName, "}", "\}")
EndIf

$sPattern = $Hostname & ".*{(.*?)}.*" & $sName
$aResult = StringRegExp($sRead, $sPattern, 3)

If @error Then
    MsgBox(16, "Error", "Unable to find the software GUID")
    Exit
EndIf

MsgBox(0, "Done", "Your software GUID is: " & $aResult[0])

Hi!

Edited by Nessie

My UDF: NetInfo UDF Play with your network, check your download/upload speed and much more! YTAPI Easy to use YouTube API, now you can easy retrive all needed info from a video. NavInfo Check if a specific browser is installed and retrive other usefull information. YWeather Easy to use Yahoo Weather API, now you can easily retrive details about the weather in a specific region. No-IP UDF Easily update your no-ip hostname(s).

My Script: Wallpaper Changer Change you wallpaper dinamically, you can also download your wallpaper from your website and share it with all!   My Snippet: _ImageSaveToBMPConvert an image to bmp format. _SciteGOTO Open a file in SciTE at specific fileline. _FileToHex Show the hex code of a specified file

Share this post


Link to post
Share on other sites
JohnOne

#include<Array.au3>
#include <String.au3>

$txtline = "PCNAME {0FFC8EC3-38F4-4A3B-9D2D-6A56DDF4C97C} BDE 5.11 for SIMS Workstation Setup"
$host = ("PCNAME")
$software = "BDE 5.11 for SIMS Workstation Setup"
;$File = ("C:\Remote Uninstall\PCNAME.txt")
;$FileData = FileRead($File)

$id = _GetGUID($txtline, $host, $software)

MsgBox(0, $software & " GUID", $id)

Func _GetGUID($FileData, $Hostname, $SoftwareName)
    $GUID = _StringBetween($FileData, $Hostname & " ", " " & $SoftwareName)
    Return $GUID[0]
EndFunc   ;==>_GetGUID

Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites
Mechaflash

@OP I would say the way to approach will all depend on what you plan to do with the data after you've retrieved it?

Is the purpose to be able to search for a specific GUID via specifying the PCNAME and program name? Do you want to retrieve a list of all UIDs on the machine? Specify intention to receive a more adept solution.


Spoiler

“Hello, ladies, look at your man, now back to me, now back at your man, now back to me. Sadly, he isn’t me, but if he stopped using ladies scented body wash and switched to Old Spice, he could smell like he’s me. Look down, back up, where are you? You’re on a boat with the man your man could smell like. What’s in your hand, back at me. I have it, it’s an oyster with two tickets to that thing you love. Look again, the tickets are now diamonds. Anything is possible when your man smells like Old Spice and not a lady. I’m on a horse.”

 

Share this post


Link to post
Share on other sites
SuperFletch

Sorry for the slow reply.

The intention is to use AutoIt to find out the GUID (for the Software entry named SOLUS 3 Agent).

Once I have the GUID I can then automate an uninstall of that software via msiexec

I'll always have the computer name and the software name, but the GUID will always be different.

PCNAME {24531475-94B9-4A91-B4AA-16158C6AE026} SOLUS 3 Agent 3.4.118.0 Capita Business Services Ltd 20120704

Edited by SuperFletch

Share this post


Link to post
Share on other sites
SuperFletch

Thanks for all the help guys....

Ended up with this:

Totally different but it worked (extracts the entire line of text with the right software on and then edits the string until I only have what is needed).

$Line = 1
$Found = 1
$Success = 1
Do
$Data = FileReadLine(@ScriptDir & "\PCNAME.txt",$Line)
IF StringInStr($Data," SOLUS 3 Agent ") = 0 Then
$Line = $Line + 1
Else
$Success = 2
EndIf
Until $Success = 2

$Data = StringReplace($Data,"PCNAME","",1)
$Data = StringReplace($Data," Capita Business Services Ltd ","",1)
$Data = StringReplace($Data,"{","",1)
$Data = StringReplace($Data,"}","",1)
$Data = StringStripWS($Data,8)
$GUID = StringTrimRight($Data,28)

MsgBox(0,"GUID Extracted","SOLUS 3 GUID IS - " & $GUID)
Edited by SuperFletch

Share this post


Link to post
Share on other sites
Mechaflash

$Line = 1
$Found = 1
$Success = 1
Do
$Data = FileReadLine(@ScriptDir & "\PCNAME.txt",$Line)
IF StringInStr($Data," SOLUS 3 Agent ") = 0 Then
$Line = $Line + 1
Else
$Success = 2
EndIf
Until $Success = 2

$Data = StringReplace($Data,"PCNAME","",1)
$Data = StringReplace($Data," Capita Business Services Ltd ","",1)
$Data = StringReplace($Data,"{","",1)
$Data = StringReplace($Data,"}","",1)
$Data = StringStripWS($Data,8)
$GUID = StringTrimRight($Data,28)

MsgBox(0,"GUID Extracted","SOLUS 3 GUID IS - " & $GUID)

eww... kill it with fire!


Spoiler

“Hello, ladies, look at your man, now back to me, now back at your man, now back to me. Sadly, he isn’t me, but if he stopped using ladies scented body wash and switched to Old Spice, he could smell like he’s me. Look down, back up, where are you? You’re on a boat with the man your man could smell like. What’s in your hand, back at me. I have it, it’s an oyster with two tickets to that thing you love. Look again, the tickets are now diamonds. Anything is possible when your man smells like Old Spice and not a lady. I’m on a horse.”

 

Share this post


Link to post
Share on other sites
Mechaflash

Problem one is you're only looking for the first instance of "SOLUS 3 Agent" and then leaving at that. Notice in the file you provided, there are actually two instances of SOLUS 3 Agent.

Please use this... working example with the file you provided.

Local $aGUID , $sProg = "SOLUS 3 Agent", $pName = "PCNAME", $sData, $sGUID

$sData = FileRead(@ScriptDir & "\PCNAME.txt")
$aGUID = StringRegExp($sData, '(?i:' & $pName & '.*{)(.*)(?i:}.*' & $sProg & '.*)', 3)
If @error then
  msgbox(0,"","GUID for " & $sProg & " was not found.")
else
  for $item in $aGUID
    $sGUID&=$item & @CRLF
  next
  msgbox(0,"", "Found a total of " & UBound($aGUID) & " GUIDs for program " & $sProg & ":" & @CRLF & @CRLF & $sGUID)
endif

Notice that $aGUID will hold all instances of it in a 0-based array. Much easier to manage.


Spoiler

“Hello, ladies, look at your man, now back to me, now back at your man, now back to me. Sadly, he isn’t me, but if he stopped using ladies scented body wash and switched to Old Spice, he could smell like he’s me. Look down, back up, where are you? You’re on a boat with the man your man could smell like. What’s in your hand, back at me. I have it, it’s an oyster with two tickets to that thing you love. Look again, the tickets are now diamonds. Anything is possible when your man smells like Old Spice and not a lady. I’m on a horse.”

 

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  

×