Jump to content

Saving array to AutoIt script for sequential executions


Recommended Posts

Hello,

I've been working on a program where a GUI with an input box and run button. User can enter in a 7 digit code and it will open up a file directory. The user can open up another GUI which enables them to save there own codes with an associated directory. This is done with a 2D array, where there is a column for the code, and a column for the file directory, there is also a column where user could enter in the path to a .exe file if they wanted to open a file instead of just the folder directory.

Example of how the array could look:

;Savedcodes array columns[Codes][Address][EXE]
#include <Array.au3>
initiallist()

Func initiallist();Initialization of array

Global $savedcodes[1][2] ; Array for saving user entered commands
$code = 0
$address = 1

;Default codes
    $savedcodes[UBound($savedcodes)-1][$code] = "ggggggg";###Initialization###
    $savedcodes[UBound($savedcodes)-1][$address] = "G:\"
    _ArrayAdd($savedcodes, "")
    $savedcodes[UBound($savedcodes)-1][$code] = "ddddddd";###Initialization###
    $savedcodes[UBound($savedcodes)-1][$address] = "D:\"
    _ArrayAdd($savedcodes, "")
    $savedcodes[UBound($savedcodes)-1][$code] = "ccccccc";###Initialization###
    $savedcodes[UBound($savedcodes)-1][$address] = "C:\"
    _ArrayAdd($savedcodes, "")
    $savedcodes[UBound($savedcodes)-1][$code] = "hhhhhhh";###Initialization###
    $savedcodes[UBound($savedcodes)-1][$address] = "H:\"


    _ArrayDisplay($savedcodes)

EndFunc

Anyways everything runs pretty smoothly, however I am not sure how to save this array so that if the user was to close the program that they could use the codes they had entered in previously, without having to re-enter them in each time they open it. Any ideas on how best to achieve this?

Users will not have administrative rights.

The only way I can think of how to achieve this goal is by writing to the file, which I'd be totally happy doing. However I wouldn't know how to delete old lines that exist. I have a function already, that is able to read through a file and look for specific markers (say: ###Initialization###) and return that line number.  If the marker was detected I could delete that line, and then add new lines for every element of array using the _FileWriteToLine(). Any idea if there is a function instead of writing to a specific line it would delete it?

 

Link to comment
Share on other sites

No it doesn't require admin rights, below is a basic example:

#include <Array.au3>
Global $savedcodes[1][2] ; Array for saving user entered commands
initiallist()

Func initiallist();Initialization of array
Local $hFilePath = @AppDataDir & '\Your Application'
    If FileExists($hFilePath) = 0 Then DirCreate($hFilePath)
$code = 0
$address = 1

;Default codes
    $savedcodes[UBound($savedcodes)-1][$code] = "ggggggg";###Initialization###
    $savedcodes[UBound($savedcodes)-1][$address] = "G:\"
    IniWrite($hFilePath & '\SavedCodes.ini', 'SavedCodes', $savedcodes[UBound($savedcodes)-1][$code], $savedcodes[UBound($savedcodes)-1][$address])
    _ArrayAdd($savedcodes, "")
    $savedcodes[UBound($savedcodes)-1][$code] = "ddddddd";###Initialization###
    $savedcodes[UBound($savedcodes)-1][$address] = "D:\"
    IniWrite($hFilePath & '\SavedCodes.ini', 'SavedCodes', $savedcodes[UBound($savedcodes)-1][$code], $savedcodes[UBound($savedcodes)-1][$address])
    _ArrayAdd($savedcodes, "")
    $savedcodes[UBound($savedcodes)-1][$code] = "ccccccc";###Initialization###
    $savedcodes[UBound($savedcodes)-1][$address] = "C:\"
    IniWrite($hFilePath & '\SavedCodes.ini', 'SavedCodes', $savedcodes[UBound($savedcodes)-1][$code], $savedcodes[UBound($savedcodes)-1][$address])
    _ArrayAdd($savedcodes, "")
    $savedcodes[UBound($savedcodes)-1][$code] = "hhhhhhh";###Initialization###
    $savedcodes[UBound($savedcodes)-1][$address] = "H:\"
    IniWrite($hFilePath & '\SavedCodes.ini', 'SavedCodes', $savedcodes[UBound($savedcodes)-1][$code], $savedcodes[UBound($savedcodes)-1][$address])

    _ArrayDisplay($savedcodes)
    ShellExecute($hFilePath) ;~ Should open the ini in notepad.exe

EndFunc

 

Link to comment
Share on other sites

27 minutes ago, Subz said:

No it doesn't require admin rights, below is a basic example

 

How would I then make array equal to that file next time I run script?

#include <File.au3>
#include <Array.au3>

Global $savedcodes[1][2] ; Array for saving user entered commands

if not FileExists(@AppDataDir & '\Your Application\SavedCodes.ini') then 
    initiallist()
else
    getarray()
endif

_ArrayDisplay

Func initiallist();Initialization of array
Local $hFilePath = @AppDataDir & '\Your Application'
    If FileExists($hFilePath) = 0 Then DirCreate($hFilePath)
$code = 0
$address = 1

;Default codes
    $savedcodes[UBound($savedcodes)-1][$code] = "ggggggg";###Initialization###
    $savedcodes[UBound($savedcodes)-1][$address] = "G:\"
    IniWrite($hFilePath & '\SavedCodes.ini', 'SavedCodes', $savedcodes[UBound($savedcodes)-1][$code], $savedcodes[UBound($savedcodes)-1][$address])
    _ArrayAdd($savedcodes, "")
    $savedcodes[UBound($savedcodes)-1][$code] = "ddddddd";###Initialization###
    $savedcodes[UBound($savedcodes)-1][$address] = "D:\"
    IniWrite($hFilePath & '\SavedCodes.ini', 'SavedCodes', $savedcodes[UBound($savedcodes)-1][$code], $savedcodes[UBound($savedcodes)-1][$address])
    _ArrayAdd($savedcodes, "")
    $savedcodes[UBound($savedcodes)-1][$code] = "ccccccc";###Initialization###
    $savedcodes[UBound($savedcodes)-1][$address] = "C:\"
    IniWrite($hFilePath & '\SavedCodes.ini', 'SavedCodes', $savedcodes[UBound($savedcodes)-1][$code], $savedcodes[UBound($savedcodes)-1][$address])
    _ArrayAdd($savedcodes, "")
    $savedcodes[UBound($savedcodes)-1][$code] = "hhhhhhh";###Initialization###
    $savedcodes[UBound($savedcodes)-1][$address] = "H:\"
    IniWrite($hFilePath & '\SavedCodes.ini', 'SavedCodes', $savedcodes[UBound($savedcodes)-1][$code], $savedcodes[UBound($savedcodes)-1][$address])

    ShellExecute($hFilePath) ;~ Should open the ini in notepad.exe

    Return 1

EndFunc

Func getarray()

;****How do I make $savedcodes to that ini file


EndFunc

 

Edited by AnonymousX
Link to comment
Share on other sites

9 minutes ago, Subz said:
Local $hFilePath = @AppDataDir & '\Your Application'
Local $aSavedCodes = IniReadSection($hFilePath & '\SavedCodes.ini', 'SavedCodes')
_ArrayDisplay($aSavedCodes)

 

Thanks Subz!

While I have your attention, I wanted to mention I have been doing a ton of searching on this forum for various things, and your name noticeably stood out as someone who contributed to many of the various things. So I really do thank you for all your contributions not to just helping me with this but with the various other solutions you've helped others with! So big thanks from me!

Link to comment
Share on other sites

44 minutes ago, Subz said:
$savedcodes[UBound($savedcodes)-1][0] = "ddddddd"
 $savedcodes[UBound($savedcodes)-1][1] = "D:\"
 $savedcodes[UBound($savedcodes)-1][2] = "test"
 
 IniWrite($hFilePath & '\SavedCodes.ini', 'SavedCodes', $savedcodes[UBound($savedcodes)-1][0], $savedcodes[UBound($savedcodes)-1][1],$savedcodes[UBound($savedcodes)-1][2]);This is not allowed

 

Your method words perfectly if I'm only using 2 columns in the array, but doesn't seem to work if there is 3 columns. Is there a way to modify this so that when writing to the file I can get in all 3 columns?

Thanks

Edited by AnonymousX
Link to comment
Share on other sites

Unfortunately no, you could write the strings to a single line and then use StringSplit for example:

#include <Array.au3>
Global $hFilePath = @AppDataDir & '\Your Application'
    If FileExists($hFilePath) = 0 Then DirCreate($hFilePath)

Global $aSavedCodes[1][3] ; Array for saving user entered commands
_InitialList("ggggggg", "G:\", "test")
_InitialList("ddddddd", "D:\", "test")
_InitialList("ccccccc", "C:\", "test")
_InitialList("hhhhhhh", "H:\", "test")
$aSavedCodes[0][0] = UBound($aSavedCodes) -1

_ArrayDisplay($aSavedCodes)

_ReadSaveCodes()

_ArrayDisplay($aSavedCodes)

Func _InitialList($sCode, $sRootPath, $sFolderPath)
    _ArrayAdd($aSavedCodes, $sCode & '|' & $sRootPath & '|' & $sFolderPath)
    IniWrite($hFilePath & '\SavedCodes.ini', 'SavedCodes', $sCode, $sRootPath & ';' & $sFolderPath)
EndFunc

Func _ReadSaveCodes()
    Local $aIniSavedValues
    Local $aIniReadSavedCodes = IniReadSection($hFilePath & '\SavedCodes.ini', 'SavedCodes')
    Local $aIniSavedCodes[$aIniReadSavedCodes[0][0] + 1][3]
    For $i = 1 To $aIniReadSavedCodes[0][0]
        $aIniSavedCodes[$i][0] = $aIniReadSavedCodes[$i][0]
        $aIniSavedValues = StringSplit($aIniReadSavedCodes[$i][1], ';')
        For $j = 1 To $aIniSavedValues[0]
            $aIniSavedCodes[$i][$j] = $aIniSavedValues[$j]
        Next
    Next
EndFunc

 

Link to comment
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
 Share

×
×
  • Create New...