AnonymousX

Saving array to AutoIt script for sequential executions

11 posts in this topic

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?

 

Share this post


Link to post
Share on other sites



IniWrite to the users Appdata dir or you can use registry key, Ini is usually easier to manage when you want to re-use the data again.

Share this post


Link to post
Share on other sites
7 minutes ago, Subz said:

IniWrite to the users Appdata dir or you can use registry key, Ini is usually easier to manage when you want to re-use the data again.

I'm unfamiliar with what those are. How does that work? Does that require admin rights?

Share this post


Link to post
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

 

1 person likes this

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

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

Share this post


Link to post
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!

Share this post


Link to post
Share on other sites

Thanks, I'm glad to help, I know what it was like when I first started so happy to give back to the community.

Thanks again.

1 person likes this

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

#10 ·  Posted

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

 

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

  • Similar Content

    • AstronomerDaz
      By AstronomerDaz
      Having migrated my PC across to W10, I am getting an odd behaviour when executing a script that works perfectly under W7... Wondered if anyone else had come across this:
      So, my script runs an external program called Deep Sky Planner. It opens two reports to calculate positions in the night sky for each of the 8 planets, plus the Moon and Sun. It then saves each of these reports as a HTML file, and a PDF file. I upload these to my astronomical societies web-site. One report is a set of basic data, then a more detailed one is run.
      Each report is named with the month-year-object, so 1015Venus.html or 1015Sun_det.pdf
      Under W7 (64bit), no problem. On my W10 machine, the change of the filename to be saved is not recognised by the 'Save As' dialogue box, even though the new file name is clearly there in the text edit box! So, I get an error saying that the file already exists!
      I've attached a picture of the dialog box error so you can see what I mean.
      I've also attached my code (feel free to rip this apart )
      Has anyone else seen this? Any suggestions??
       
      Thanks
      Daz

      DSP_This_Month.au3