Jump to content

Recommended Posts

Posted (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 by Maxime
  • Moderators
Posted

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

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

 

  • Moderators
Posted

Maxime,

Why not use an ini file and the Ini* functions to save and reload your variables - it would make it much easier.

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

 

Posted

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
Posted (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 by Melba23

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

 

Posted

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
Posted

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:

#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

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

 

  • Moderators
Posted (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 by Melba23
Simplified code

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

 

  • Moderators
Posted

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

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

 

Posted

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
Posted

Maxime,

It is very poor coding practice to declare Global variables inside a function, but it is your funeral.

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

 

Posted
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   :unsure:

$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)

 

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
×
×
  • Create New...