Maxime Posted November 21, 2018 Posted November 21, 2018 (edited) hello i'm working to save and restore vars in a txt file. saving is ok but i wand to restore vars in my text file i have line like // variables naissance=1948 jour=7 i write this $File = FileOpen("variables.txt", 0) While 1 $Line = FileReadLine($File) If @error = -1 Then ExitLoop if StringInStr($Line, "=")>0 and StringInStr($Line, "/")=0 then Local $variable_chaine = StringSplit($Line, "=") ConsoleWrite($variable_chaine[1]&"="&$variable_chaine[2]&@CRLF) EndIf Wend FileClose($file) i have $variable_chaine[1] (naissance or jour) and i want to have vars like : $naissance , $jour i want to generate vars and not write each time if $variable_chaine[1]="naissance" then $naissance=$variable_chaine[2] EndIf if $variable_chaine[1]="jour" then $jour=$variable_chaine[2] EndIf someone can help me ? thanks Edited November 21, 2018 by Maxime
Moderators Melba23 Posted November 21, 2018 Moderators Posted November 21, 2018 Moved to the appropriate forum, as the Developer General Discussion forum very clearly states: Quote General development and scripting discussions. If it's super geeky and you don't know where to put it - it's probably here. Do not create AutoIt-related topics here, use the AutoIt General Help and Support or AutoIt Technical Discussion forums. Moderation Team Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
Moderators Melba23 Posted November 21, 2018 Moderators Posted November 21, 2018 Maxime, Why not use an ini file and the Ini* functions to save and reload your variables - it would make it much easier. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
Maxime Posted November 21, 2018 Author Posted November 21, 2018 i know, i found at start #include <MsgBoxConstants.au3> #include <WinAPIFiles.au3> Example() Func Example() ; Crée une constante locale avec le nom complet du fichier qui sera lu/écrit Local Const $sFilePath = _WinAPI_GetTempFileName(@TempDir) ; Ecrit la valeur de 'AutoIt' à la clé "Title" et dans la section "General". IniWrite($sFilePath, "General", "Title", "AutoIt") ; Lit la valeur correspondante à la clé 'Title' dans la section 'General'. Local $sRead = IniRead($sFilePath, "General", "Title", "Default Value") ; Affiche la valeur retournée par IniRead. MsgBox($MB_SYSTEMMODAL, "", "La valeur correspondante à la clé 'Title' dans la section 'General' est: " & $sRead) ; Supprime la clé 'Title'. IniDelete($sFilePath, "General", "Title") ; Lit le fichier INI pour la valeur de 'Title' dans la section 'General'. $sRead = IniRead($sFilePath, "General", "Title", "Default Value") ; Affiche la valeur retournée par IniRead. Comme il n'y a pas de clé stocké la valeur sera la 'Valeur par défaut' passée à IniRead. MsgBox($MB_SYSTEMMODAL, "", "La valeur correspondante à la clé 'Title' dans la section 'General' est: : " & $sRead) ; Supprime le fichier INI. FileDelete($sFilePath) EndFunc ;==>Example but i n this example i should write each time for each var on line $sRead = IniRead($sFilePath, "General", "Title", "Default Value")
Moderators Melba23 Posted November 21, 2018 Moderators Posted November 21, 2018 (edited) Maxime, I am afraid I do not understand. The script you posted shows that you know how to use the Ini* functions - so what is the problem in using them instead of the convoluted code you posted in the OP? M23 P.S. Explique en francais si tu preferes. Edited November 21, 2018 by Melba23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
Maxime Posted November 21, 2018 Author Posted November 21, 2018 Génial quelqu'un qui parle français (je déconne) mais c'est tellement plus simple. En fait je veux passer beaucoup de variables dans un fichier ini ou txt mais pour les récupérer je veux éviter de taper 50 fois la même ligne. Dans mon exemple je voudrais nommer la variable $naissance en sachant que je récupère $variable_chaine[1]="naissance" ou ="jour" ou autre (et que je veux faire la même chose pour $jour, $lieu, $etc... donc soit je tape autant de fois qu'il y a de variables (ça va être un peu long) if $variable_chaine[1]="naissance" then $naissance=$variable_chaine[2] if $variable_chaine[1]="jour" then $jour=$variable_chaine[2] if $variable_chaine[1]="lieu" then $lieu=$variable_chaine[2] soit je passe par des arrays mais je me retrouve à taper autant de lignes
Moderators Melba23 Posted November 21, 2018 Moderators Posted November 21, 2018 Maxime, One of the problems of extracting data from a file is the requirement to parse the data - which normally means a repetitive process to read each and every variable. But you could do something like this: expandcollapse popup#include <Array.au3> ; Create a suitable ini file if it does not exist $sIniPath = @ScriptDir & "\temp.ini" $sIniFileContent = "[variables]" & @CRLF & _ "naissance=1948" & @CRLF & _ "jour=7" & @CRLF & _ "lieu=Neuilly-Plaisance" If Not FileExists($sIniPath) Then FileWrite($sIniPath, $sIniFileContent) EndIf ; ---------------------------------------------- ; Define the variables you wish to use Global $sNaissance, $sJour, $sLieu, $sMois ; Save the base names of the variables in an array Global $aData[] = ["naissance", "jour", "lieu", "mois"] ; Read the ini file section $aVariables = IniReadSection($sIniPath, "variables") ; And here it is if you want to see it ;_ArrayDisplay($aVariables, "", Default, 8) ; Now loop through the variable names For $i = 0 To UBound($aData) - 1 ; Find the relevant entry in the ini file $iIndex = _ArraySearch($aVariables, $aData[$i]) ; And assign the value to the variable If @error Then Assign("s" & $aData[$i], "Data not stored") Else Assign("s" & $aData[$i], $aVariables[$iIndex][1]) EndIf Next ; And here they are! ConsoleWrite($sNaissance & @CRLF) ConsoleWrite($sJour & @CRLF) ConsoleWrite($sLieu & @CRLF) ConsoleWrite($sMois & @CRLF) And let the loop do all the work once you have defined the initial variables and entered their names into an array. Pose les questions si necessaire. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
Moderators Melba23 Posted November 21, 2018 Moderators Posted November 21, 2018 (edited) Maxime, One of the problems of extracting data from a file is the requirement to parse the data - which normally means a repetitive process to read each and every variable. But you could do something like this: ; Create a suitable ini file if it does not exist $sIniPath = @ScriptDir & "\temp.ini" $sIniFileContent = "[variables]" & @CRLF & _ "naissance=1948" & @CRLF & _ "jour=7" & @CRLF & _ "lieu=Neuilly-Plaisance" If Not FileExists($sIniPath) Then FileWrite($sIniPath, $sIniFileContent) EndIf ; ---------------------------------------------- ; Define the variables you wish to use Global $sNaissance, $sJour, $sLieu, $sMois ; Save the base names of the variables in an array Global $aData[] = ["naissance", "jour", "lieu", "mois"] ; Now loop through the variable names For $i = 0 To UBound($aData) - 1 ; Find the relevant entry in the ini file Assign("s" & $aData[$i], IniRead($sIniPath, "variables", $aData[$i], "Data not stored")) Next ; And here they are! ConsoleWrite($sNaissance & @CRLF) ConsoleWrite($sJour & @CRLF) ConsoleWrite($sLieu & @CRLF) ConsoleWrite($sMois & @CRLF) And let the loop do all the work once you have defined the initial variables and entered their names into an array. Pose les questions si necessaire. M23 Edited November 21, 2018 by Melba23 Simplified code Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
Maxime Posted November 23, 2018 Author Posted November 23, 2018 merci je vais regarder cela (j'avais trop de taff, c'est deux derniers jours)
Maxime Posted November 23, 2018 Author Posted November 23, 2018 ben c'est génial avec ,2 pour déclarer la variable en global MERCI Assign("s" & $aData[$i], IniRead($sIniPath, "variables", $aData[$i], "Data not stored",2))
Moderators Melba23 Posted November 23, 2018 Moderators Posted November 23, 2018 Maxime, The variables are already declared as Global earlier in the script so there is no need to use that flag when Assigning them a value. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
Maxime Posted November 23, 2018 Author Posted November 23, 2018 yes but i don't want to declare each before. func lire_ini($ini) If Not FileExists($ini) Then FileWriteLine ($ini, "// Fichier des variables") Else Global $sauvegarde_variables=1 $File = FileOpen($ini, 0) While 1 $Line = FileReadLine($File) If @error = -1 Then ExitLoop if StringInStr($Line, "=")>0 and StringInStr($Line, "/")=0 then Local $variable_chaine = StringSplit($Line, "=") Assign( $variable_chaine[1], $variable_chaine[2] ,2) ConsoleWrite($variable_chaine[1]&"="&$variable_chaine[2]&@CRLF) EndIf Wend FileClose($file) ConsoleWrite("---end---"&@CRLF) endif EndFunc lire_ini("fichier.ini")
Moderators Melba23 Posted November 23, 2018 Moderators Posted November 23, 2018 Maxime, It is very poor coding practice to declare Global variables inside a function, but it is your funeral. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
mikell Posted November 23, 2018 Posted November 23, 2018 5 hours ago, Maxime said: yes but i don't want to declare each before It is not absolutely needed - but this might cause trouble later in the script $sIniPath = @ScriptDir & "\temp.ini" $sIniFileContent = "[variables]" & @CRLF & _ "naissance=1948" & @CRLF & _ "jour=7" & @CRLF & _ "lieu=Neuilly-Plaisance" If Not FileExists($sIniPath) Then FileWrite($sIniPath, $sIniFileContent) EndIf ; ---------------------------------------------- Global $aData = IniReadSection($sIniPath, "variables") For $i = 1 To $aData[0][0] Assign("s" & $aData[$i][0], $aData[$i][1], 2) Next ConsoleWrite($sNaissance & @CRLF) ConsoleWrite($sJour & @CRLF) ConsoleWrite($sLieu & @CRLF)
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now