Jump to content
Sign in to follow this  
Crazyace

External INI file to Control Script Settings

Recommended Posts

Hello All,

I'm about to start working on a log building script and I was thinking about using an external ini file to control settings for the application that the user can edit and have the script use the new settings. Does anyone have any examples by chance of something simple? I just need an idea to help me get started.


"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.""Never, ever, argue with an idiot. They'll drag you down to their level and beat you with experience"

Share this post


Link to post
Share on other sites

You mean an INI file editor? If yes, you should try

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2020-10-10 - Version 1.5.2.1) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2020-06-30 - Version 1.6.2.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (NEW 2020-06-27 - Version 1.3.2.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
Task Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - Wiki

Tutorials:
ADO - Wiki, WebDriver - Wiki

 

Share this post


Link to post
Share on other sites

For one of my projects, I use a text ini file with this format:

val1=true

val2=4

val3="text"

I read it, pass its contents to this function to populate variables from it:

Func ReadInputParms ( $InBuf ) ;$inBuf is the text contents of the ini file

dim $Input

dim $InTokens

dim $KeyVal

dim $Key

Dim $bDimmed = false

$InBuf = StringRight ( $InBuf, StringLen ( $InBuf ) - 1 )

if StringLen ( $InBuf ) = 0 Then

; Process the command line if no buffer was passed in

$Input = $CmdLineRaw

EndIf

$InTokens = StringSplit($InBuf, @CRLF, 1)

Dim $boolval, $numval, $stringval

for $i = 1 to $InTokens[0] ; loop through all input lines, and parse them

if StringLen ( $InTokens [ $i ] ) > 0 Then

Dim $array = StringSplit($InTokens[$i], "=")

$Key = StringLower($array[1])

if StringInStr($Key, chr(34)) > 0 Then $Key = StringReplace($Key, chr(34), "")

if StringInStr($InTokens[$i], "=") > 0 Then

$KeyVal = $array[2]

; remove start and end quotes if needed

if StringLeft($KeyVal, 1) = chr(34) and StringRight($KeyVal, 1) = chr(34) Then

$KeyVal = StringMid($KeyVal, 2, stringlen($KeyVal) - 2)

EndIf

Else

$KeyVal = True

EndIf

select

case StringLower ( $Key ) = "booleanvalue"

$boolval = ( StringLower($KeyVal) = "true" ) ; assign ini value to boolean variable

case StringLower ( $Key ) = "numericvalue"

$numval = $KeyVal * 1 ; assign ini value to numeric value

Case StringLower ( $Key ) = "stringval"

$stringval = $KeyVal ; assign ini value to string variable

case StringLower ( $Key ) = "/?" ; user asked for help, display usage

ShowUsage("")

Exit 0

case Else ; unexpected value, show usage

ShowUsage("Unknown parameter, '" & $InTokens[$i] & "'.")

Exit 1

EndSelect

EndIf

Next

EndFunc

Edited by rodent1

Share this post


Link to post
Share on other sites

You mean an INI file editor? If yes, you should try

Thanks for the reply. Here is an example of what I mean.

So the application will have a button and a textpad. When the button is clicked it will send the following text to the textpad "You press a Button" I would like to have an ini file that button one get's it's text from. So the user could go in and make it say something different.

rodent1 seems to have done something like this before.


"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.""Never, ever, argue with an idiot. They'll drag you down to their level and beat you with experience"

Share this post


Link to post
Share on other sites

Here's something to demo a way to do it. It's very basic, but it allows you to create and change the INI file.

$IniFile = "text.ini"
$ButtonText = IniRead($IniFile, "news", "motd", " Hello ")
GUICreate("test", 200, 100)
$Button = GUICtrlCreateButton(" Say something ", 10, 10)
$Edit = GUICtrlCreateEdit("", 10, 40, 180, 50)
$Button2 = GUICtrlCreateButton(" Change INI text ", 100, 10)
GUISetState()
while 1
 $msg = GUIGetMsg()
 Switch $msg
  Case -3
   Exit
  case $Button
   $ButtonText = IniRead($IniFile, "news", "motd", " Hello ")
   GUICtrlSetData($Edit, $ButtonText)
  Case $Button2
   IniWrite($IniFile, "news", "motd", GUICtrlRead($Edit))
 EndSwitch
WEnd

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

I've used an ini file for an app I made to merge PDFs for a 2000 page state public record file. The application is deployed in 4 offices for the managers, and the ini file and main documents sits on our NAS, available to all of them. When the app starts, the program pulls all of the program parameters: path to the current year's public record file, location of the last backup (for an undo feature), passwords, etc. The file in question gets copied to the local machine and a lock file is created on the NAS while a user is working on it, and all processing takes place on the local machine. When the user is finished, the completed file gets copied back to the NAS and the new parameters are saved to the ini.


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

AutoIt provides functions to read and write ini files.

Ooops - a bit late.

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2020-10-10 - Version 1.5.2.1) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2020-06-30 - Version 1.6.2.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (NEW 2020-06-27 - Version 1.3.2.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
Task Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - Wiki

Tutorials:
ADO - Wiki, WebDriver - Wiki

 

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  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By roeselpi
      hello everybody.
      i am an absolute beginner and have zero experience with programming and / or scripting.
      i have been looking at autoit for about one week and thus do not know and / or understand a lot about it. 
      but: i have started writing a programe. i have not got very far yet, but many hours of tutorial watching and trying out things has brought me to where i am now. to be honest, i did not even think that i would make it this far. trial and error all the way. but i am quite happy to say that my programe works... well... all apart from the most important two functions. but i have given it a good shot and come to the point where my understanding is limited for what i want to do and thus i would need the help of a coding-expert.
      to make a long story short i will show what i have got... i guess everything can be copy and pasted apart from the image. it is just an example image but i will upload it anyway.
      1. make a simple text file and save it as "test.txt" with the following text within it:
      Hammurabi walks into the forest @ 5 o'clock. 2. make a simple text file and save it as "illogic.ini" with the following text within it:
      [alternation] alter=2,4,1 ;- 0 for NO alternation [spacing] steps=3 ;- aplies only if alternation is 0 ;- disable function with 0 but alternation must then aply [syntax] syntax=3,5,2 ;- 0 for RANDOM choice [symbols] symbs=a,b,c,d,e,f,g,h,i,j,k,0,1,2,3,4,5,6 ;- symbols to be used in scramble routine 3. save the image as "illogic.jpg" together with the other two files in order to have the same as me in one directory (illogic). or edit the au3 code to match with the rest.
      4. save the au3 code below as "illogic.au3" in the same directory.
      #include <ButtonConstants.au3> #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> #include <FileConstants.au3> #include <MsgBoxConstants.au3> #include <WinAPIFiles.au3> $ini = "illogic.ini" $sections = IniReadSectionNames($ini) For $a = 1 To UBound($sections) - 1 $keys = IniReadSection($ini, $sections[$a]) For $b = 1 To UBound($keys) - 1 ; ConsoleWrite($keys[$b][1] & @CRLF) ; ShellExecute(IniRead($ini, $sections[$a], $keys[$b][0], "")) Next Next #Region ### START Koda GUI section ### Form=C:\AutoIt3\illogic\illogic-mask.kxf Global $Navigation = GUICreate("illogic - the illogical scrambler", 1002, 653, -1, -1) Global $File = GUICtrlCreateMenu("&File") Global $NewFile = GUICtrlCreateMenuItem("New File", $File) Global $OpenFile = GUICtrlCreateMenuItem("Open File", $File) Global $SaveAs = GUICtrlCreateMenuItem("Save as...", $File) Global $Exit = GUICtrlCreateMenuItem("Exit", $File) Global $etc = GUICtrlCreateMenu("?") Global $About = GUICtrlCreateMenuItem("About", $etc) Global $scramble = GUICtrlCreateButton("ILLOGIC", 48, 568, 145, 33) Global $descramble = GUICtrlCreateButton("LOGIC", 240, 568, 145, 33) Global $exiteer = GUICtrlCreateButton("EXIT", 808, 568, 145, 33) Global $entry = GUICtrlCreateEdit("", 40, 128, 921, 417) GUICtrlSetData(-1, "Empty File...") GUICtrlSetBkColor(-1, 0xE3E3E3) Global $Pic1 = GUICtrlCreatePic("C:\AutoIt3\illogic\illogic.jpg", 40, 16, 921, 89) Global $reset = GUICtrlCreateButton("RESET", 432, 568, 147, 33) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Global $exitbutton $exitbutton = MsgBox(36, "Close Aplication?", "Bye For Now & See You Next Time...") If $exitbutton = 6 Then Exit Case $Exit Global $exitbutton $exitbutton = MsgBox(36, "Close Aplication?", "Bye For Now & See You Next Time...") If $exitbutton = 6 Then Exit Case $exiteer Global $exitbutton $exitbutton = MsgBox(36, "Close Aplication?", "Bye For Now & See You Next Time...") If $exitbutton = 6 Then Exit Case $About MsgBox(0,"About...","illogic v 1.0") Case $SaveAs $EntryData = GUICtrlRead($entry) $SaveLocation = FileSaveDialog("Save file", @ScriptDir, "All Files (*.*)", 16) FileWrite($SaveLocation, $EntryData) Case $NewFile GUICtrlSetData($entry, "New File...") Case $reset GUICtrlSetData($entry, "Reset...") Case $OpenFile $read = FileOpenDialog("Open File", @ScriptDir, "All Files (*.*)", 16) $text = FileRead($read, -1) GUICtrlSetData($entry, $text) FileClose($text) EndSwitch WEnd That is now a working programe. If you open the test.txt file in the programe it will copy the text into the edit box and that is where my problem starts. I would now like the two only unused buttons "illogic" and "logic" to perform what i have intended them to do and that is where the settings from the ini file become important and also the spot where i need help because being a noobie also means not understanding how to go about solving issues where there are no tutorials...
       
      Pressing "illogic" should transform (with the settings from the ini shown above) the text within the edit box like shown below:
      Hchaj16bmemj0u3bdgr0a25bbgi0i5wadaik2alckfks14ceihn13t6egjo tchhj16beefj0o3bdgr0e25sbgi0t5@ad5ik2aoc'fkc14celho13c6egjk3. to explain how the text got scrambled let us look at the ini file:
      the alternation "alter=2,4,1" describes that inbetween each letter or space there must be first two, then next four and finally one letter placed in addition to the existing text
      the steps "steps=3" describes that inbetween each letter or space there must be 3 additional letters placed. this only aplies when alternation=0
      the syntax "syntax=3,5,2" describes which letter must be used in which order. first the 3rd letter then from there on the 5th letter and finally the 2nd letter. 
      the symbols "symbs=a,b,c,d,e,f,g,h,i,j,k,0,1,2,3,4,5,6" describes what spacing letters can or must be used. basically the full alphabet and numerals and possibly even special chars
       
      it took about twenty minutes to do this manually because i kept getting the couting wrong. 
       
      anyway, the illogic button should distort the text in accordence with the settings in the ini file and of course output it in the edit box. then the file should or could be saved as a text file.
       
      i guess you know what the "logic" button is supposed to do... yes, it is supposed to turn the mumble-jumble back into the original text. (as long as the ini file is not altered it should work)
       
      another idea was working with several ini files and adding a choice dropdown for the ini files, but that is not an absolute requirement.
       
      now to my problem:
       
      i have no idea how to carry on form the point i am at now. i have been able to read the ini values into the console but the impossible task begins now trying to make functions or variables for each parameter from the ini-file. that is where i would hope for some help from users who know what they are doing and maybe can take the time to give me a big help. 
       
      i am very much hoping for helpful replies.
       
      kind regards
       
       
      edit: corrected some mistakes.

×
×
  • Create New...