Sign in to follow this  
Followers 0
LuisMartins

Read determined value of .INF file

19 posts in this topic

#1 ·  Posted (edited)

Hi there,

So I got this .INF file, example:

// Some text
// Another text
Version=1.0

And I'm trying to get only the value 1.0 in my script.

I tried:

IniRead(@ScriptDir & "\myapp\version.inf", "", "version", "NULL")

I guess it didn't work because it doesn't have sections like an .INI .

I'm also trying FileSetPos but no luck with it. :(

Any help?

Thanks in advance!

Edited by LuisMartins

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

I believe you need a section in your .ini file. For example:

[Description]
Version=1.0

Edit: I see you mentioned that already. You'll have to parse it somehow.

Edited by lopolop

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

The thing is, the .INF file may be updated and I'd rather not have to edit it everytime to add a Section.

And IniRead would be perfect, because it has this thing that when the value isn't found is assumes the defined default value, in my case "NULL".

The closer I can get is with FileReadLine, which as the name says, gives me the line I want, but I still need just the value after Version=

Edited by LuisMartins

Share this post


Link to post
Share on other sites

Unfortunately your INF file isn't in the correct format to be used with the INI* functions. In fact, it doesn't appear to be in the correct format for an INF file either, seeing as how the INF and INI formats are the same. You're going to have to do some parsing of the file, perhaps with FileRead and StringRegExp to determine if the version line is in there or not.

1 person likes this

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

LuisMartins,

Here is a technique that you can build on (this is one of many ways to do this):

#include <array.au3>

; create a test file to simulate your .INF file

local $str = '// Some text' & @crlf & '// Another text' & @crlf & 'Version=1.0' & @crlf & 'more data of any kind'
local $hfl = fileopen(@scriptdir & 'tmp010.inf',2)
filewrite($hfl,$str)
fileclose($hfl)
$hfl = 0

; read inf file to string

local $str = fileread(@scriptdir & 'tmp010.inf')

; search for your string

local $srch = stringregexp($str,'(?i)version.*',3)
if @error = 1 then
    ConsoleWrite('String not found' & @LF)
    Exit
endif

; display results

_arraydisplay($srch)

kylomas

1 person likes this

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Thank you, both answers were useful.

But still, my bad, forgot to mention, there's still one problem.

I have another string before "Version=1.0", which is "firstversion=0.1", example:

// Some text
// Another text
firstversion=0.1
Version=1.0

So, assuming that its value may also get updated, I don't think I can really rely on StringRegExp, I guess.

Edited by LuisMartins

Share this post


Link to post
Share on other sites

Maybe you should post a sanitized version of the complete file, this way we know what we're dealing with, and not having you move the goal posts whenever something is posted.

1 person likes this

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

// NetworkVersion is the version in the wire protcol between client<->server,
// its not used for matchmaking, PatchVersion is used for that to ensure
// we don't get cross connecting to different releases.  Only bump this if
// you know an incompatible change has happened to the network protocol in
// game and old demos will not work.
NetworkVersion=2.0.4.2
PatchVersion=2.1.1.8
ProductName=left4dead2
appID=550

My goal is to get the number 2.1.1.8 and only that number. Considering the versions may change with updates I want my application to always stay up to date whenever they change without having to do it manually myself.

Share this post


Link to post
Share on other sites

This is why you should have posted what you're actually looking at instead of being so obtuse in the first posts. This is only slightly like what you were asking for in the first place. The word version appears in that file several times. If we had known that before we could have come up with a much better answer for you earlier.

local $str = fileread(@scriptdir & 'test.ini'); change this to your file name 

; search for your string

local $srch = stringregexp($str,'(?i)PatchVersion=(.*)',3)
if @error = 1 then
    ConsoleWrite('String not found' & @LF)
    Exit
endif
_ArrayDisplay($srch)
1 person likes this

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

Ok thanks, that actually shows only the value.

But I'm not able to compare the returned value of $srch with another value, like:

If $srch = "2.1.1.7" Then
     MsgBox("", "Error", "Your version is not up-to-date.")
EndIf

Any advice?

Edited by LuisMartins

Share this post


Link to post
Share on other sites

It returns an array, you'd need to write it like this.

If $srch[0] = "2.1.1.7" Then
     MsgBox("", "Error", "Your version is not up-to-date.")
EndIf
1 person likes this

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

Ok thank you again, it's working great now. :)

Share this post


Link to post
Share on other sites

Just out of curiosity is the version number always a single digit e.g. d.d.d.d?


_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 04/09/2015

Share this post


Link to post
Share on other sites

may want to also add a boundry:

#include <Array.au3>
$str = "NetworkVersion=2.0.4.2" & @CRLF & _
"PatchVersion=2.1.1.8" & @CRLF & _
"LastPatchVersion=2.1.1.7" & @CRLF & _
"ProductName=left4dead2" & @CRLF & _
"appID=550"

local $srch = stringregexp($str,'(?i)bPatchVersion=(.*)',3)
_ArrayDisplay($srch) ; returns one result
local $srch = stringregexp($str,'(?i)PatchVersion=(.*)',3)
_ArrayDisplay($srch) ; returns two results
1 person likes this

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

may want to also add a boundry:

Can't hurt, although not necessary if they don't use "PatchVersion=" anywhere else in the file. But better to play it safe.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

Just out of curiosity is the version number always a single digit e.g. d.d.d.d?

Yes, it is.

may want to also add a boundry:

#include $str = "NetworkVersion=2.0.4.2" & @CRLF & _ "PatchVersion=2.1.1.8" & @CRLF & _ "LastPatchVersion=2.1.1.7" & @CRLF & _ "ProductName=left4dead2" & @CRLF & _ "appID=550" local $srch = stringregexp($str,'(?i)\bPatchVersion=(.*)',3) _ArrayDisplay($srch) ; returns one result local $srch = stringregexp($str,'(?i)PatchVersion=(.*)',3) _ArrayDisplay($srch) ; returns two results
Well like I said, those versions will much probably get updated and therefere I can't rely setting them manually... :(

Share this post


Link to post
Share on other sites

My example was just to demonstrate finding the actual data you need, when tokens exist with similar names.


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

Thanks for the heads up.

Share this post


Link to post
Share on other sites

#19 ·  Posted (edited)

$MyFile = "./~tempfile.inf"
;
; generate testfile
;
FileWrite($MyFile, _
"// NetworkVersion is the version in the wire protcol between client<->server," & @CRLF & _
"// its not used for matchmaking, PatchVersion is used for that to ensure" & @CRLF & _
"// we don't get cross connecting to different releases.  Only bump this if" & @CRLF & _
"// you know an incompatible change has happened to the network protocol in" & @CRLF & _
"// game and old demos will not work." & @CRLF & _
"NetworkVersion=2.0.4.2" & @CRLF & _
"PatchVersion=2.1.1.8" & @CRLF & _
"LastPatchVersion=2.1.1.7" & @CRLF & _
"ProductName=left4dead2" & @CRLF & _
"appID=550")
;
; here the working code
;
IniWriteSection($MyFile&".tmp.ini","MyData",FileRead($MyFile))
$result=IniRead($MyFile&".tmp.ini","MyData","PatchVersion","0.0.0.0")
FileDelete($MyFile&".tmp.ini")
MsgBox(262144," ",$result,0)
;
; cleanup testfile
;
FileDelete($MyFile)
;

Edited by Exit

My UDFs, Abbrevs and Snippets
If you like my post, just click the like button :) 

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  
Followers 0