Jump to content
roeselpi

replace data in inifile by random generation of new data

Recommended Posts

hello,

i would like to create or update the values of an ini-file with the help of a randomized generator that uses specific alpha-numeric digits

for testing i have now created a code that shows how far i have got so far. i think that i am still far away from what i want to do, but see for yourself.

 

#include <MsgBoxConstants.au3>
#include <StringConstants.au3>
#include <Array.au3>
#include <String.au3>

;-----------------------------------generate the ini-file-for-test-purposees:
;----------------------------------------------------------------------------
;----------------------------create the array for the ini-file and display it
Global $values[5] = ["test1", "test2", "test3", "test4", "test5"]
_ArrayDisplay($values)
;------------------------------------------create the ini-file and display it
$filename = "set.ini"
$standard = "chopchop"
$keynames = "key"
$equals = "="
$create = IniWrite($filename, $standard, $keynames,$values[0] _
&@CRLF&$keynames&$equals&$values[1] _
&@CRLF&$keynames&$equals&$values[2] _
&@CRLF&$keynames&$equals&$values[3] _
&@CRLF&$keynames&$equals&$values[4]) ;probably there is an easier way to do this but i do not know how, yet!
;----------------------------------------------------------------------------
;----------------------------------------------------------------------------
;----------------------------------------------------------------------------
;------------------------------------from here the code would normally start:
;----------------------------------------------------------------------------
;------------------------------get the content of the ini-file and display it
$getinifile = "set.ini"
$ini = IniReadSection($getinifile, "chopchop")
_ArrayDisplay($ini, "complete ini-file")

;----------------------------------display that data that needs to be changed
Global $datatochange = _ArrayExtract($ini, 1, -1, 1, 1)
_ArrayDisplay($datatochange, "values to be edited")

; task at hand now: create randomly generated alpha-numeric string to replace the values

;----------------------------------random-string-chars
Global $alphanumcode = "a,b,c,d,e,f,g,h,i,j,k,0,1,2,3,4,5,&,/,?,\,@,€,$,§,!,#"

Global $randomStr = ""
Global $aSpace[1]
For $i = 1 To 5
    $aSpace[0] = $alphanumcode
    $randomStr &= $aSpace[Random(0)]
Next

MsgBox(0, "String Generator", $randomStr & @CRLF)

this is a list of what i want to do:

 

1. check the existing ini-file to see how many settings there are

2. create as many random-generated 3-8 long alpha-numeric codes exitsting only of the specified digits in $alphanumcode, as the amont of settings in the ini

3 replace the existing values within the ini-file with the newly generated ones.

 

i obviosly can read the amount settings in the ini with the _ArrayDisplay() function. i can also add a column for the new data that will be generated with the help of _ArrayColInsert(). now the big questions are: how do i generate a 3-8 digit long random string using only my specified letters,signs and numerals. how do i get that generated data into the new collomn (i think i might know how to do that). then i could delete the original values-column wit _ArrayColDelete(). Then the last thing would be replace the old ini-file with the new ini-file and the new settings to look the same way as the old ini-file did but just with new data as values.

by the way if the "key=" could be numberd that would also be great. (key001=, key002=, key003=, etc.) but that is only optional, because in the usage i go directly to the value-data and use that in an array.

any ideas here on how and what i might be able to do?

any help would be great.

kind regards

Share this post


Link to post
Share on other sites
Posted (edited)
For $c = 0 To 4
    $num = StringRight("00" & $c + 1, 3)
    IniWrite($filename, $standard, $keynames & $num, $values[$c])
Next

That solves your first bit, NOTE - I have used StringRight in case you intend to go beyond 4 for $c.

Not really looked too deeply at the rest, to fully understand what you are up to.

But no need to assign "set.ini" to another variable from what I can see. Why not leave it as $filename

$ini = IniReadSection($filename, "chopchop")
_ArrayDisplay($ini, "complete ini-file")
Edited by TheSaint

AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Share this post


Link to post
Share on other sites

that is perfect! the keys are numbered now. i just tested it with more values. i used 10 values now and it works like a charm. it numbers all the keys perfectly. great. thanks a lot.

i did however replace the "4" with: UBound($values) - 1

that is already great and works for up to 999 values (i guess). 

now the harder thing will be the rest of what i want to do:

get the data from the ini & count it and then make an identical number of random generated values to replace the existing ones by overwriting the existing ini file.

of course with the help of the specified $alphanumcode digits for use in the random generated values.

Share this post


Link to post
Share on other sites
#include <MsgBoxConstants.au3>
#include <StringConstants.au3>
#include <Array.au3>
#include <String.au3>

;-----------------------------------generate the ini-file-for-test-purposees:
;----------------------------------------------------------------------------
;----------------------------create the array for the ini-file and display it
Global $values[10] = ["test1", "test2", "test3", "test4", "test5", "test6", "test7", "test8", "test9", "test0"]
_ArrayDisplay($values)
;------------------------------------------create the ini-file and display it
$filename = "set.ini"
$standard = "chopchop"
$keynames = "key"

For $c = 0 To UBound($values) - 1
    $num = StringRight("00" & $c + 1, 3)
    IniWrite($filename, $standard, $keynames & $num, $values[$c])
Next

;------------count the values and generate the same amount of random strings

For $c = 0 To UBound($values) - 1
    $generate = _Randomstring(5)
    MsgBox(0, '', $generate)
Next

Func _Randomstring($length)
    $chars= StringSplit("abcdefghijk012345&/?\@€$§!#","")
    $String = ""
    $i=0
    Do
        If $length<=0 then ExitLoop
        $String &=  $chars[Random(1,$chars[0])]
        $i += 1
    Until $i = $length
    Return $String
EndFunc

okay, now i have the same amount of randomly-generated-data as the amount of keys. how do i "catch" them and fill an array with that, so that i can replace the original ini-file with the newly generated one?

Share this post


Link to post
Share on other sites

got it.

Global $filename = "set.ini"
Global $standard = "chopchop"
Global $keynames = "key"

Global $values[10] = ["test1", "test2", "test3", "test4", "test5", "test6", "test7", "test8", "test9", "test0"]
_ArrayDisplay($values)

For $c = 0 To UBound($values) - 1
    $num = StringRight("00" & $c + 1, 3)
    IniWrite($filename, $standard, $keynames & $num, $values[$c])
Next

$ini = IniReadSection($filename, "chopchop")
$ini = _ArrayExtract($ini, 1, -1, 1, 1)
_ArrayDisplay($ini)

Global $testarray[1]

For $c = 0 To UBound($ini) - 1
    $generate = _Randomstring(5)
    _ArrayAdd($testarray,$generate,1)
    ;_ArrayDisplay($testarray)
Next

_ArrayDisplay($testarray)
_ArrayDelete($testarray, 0)
_ArrayDisplay($testarray)

FileOpen($filename,2)

For $c = 0 To UBound($testarray) - 1
    $num = StringRight("00" & $c + 1, 3)
    IniWrite($filename, $standard, $keynames & $num, $testarray[$c])
Next

FileClose($filename)

Func _Randomstring($length)
    $chars= StringSplit("abcdefghijk012345&/?\@€$§!#","")
    $String = ""
    $i=0
    Do
        If $length<=0 then ExitLoop
        $String &=  $chars[Random(1,$chars[0])]
        $i += 1
    Until $i = $length
    Return $String
EndFunc

it does exactly what i wanted it to do.

1. it generates a random predefined code as many times as values within the original ini-file,

2. it generates a totally new ini-file and overwrites the existing one with the newly generated data.

perfect. 

Share this post


Link to post
Share on other sites

Why are you deleting and recreating the ini file? You can just rewrite the contents and any keys that already exist will have their values changed.

Plus, you never close the file properly, you're using FileOpen to delete its contents, but you're using FileClose with a filename instead of a handle, so you never actually close it. If you just want to delete the file, use FileDelete instead of FileOpen.


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

@BrewManNH it is all very nice for you to say, you know what you are doing. i still am a noobie trying to learn and grasp the concepts of how to do things i would like the script to do. the reason why i used FileOpen() is because when i did not do this, then it kept adding one more value at the end. making 10 values suddenly 11 values. that was not exactly what i had envisioned. i do not quite understand what you meen by handle becaues i basically did what the help file showed me to do:

; Open the file for reading and store the handle to a variable.
    Local $hFileOpen = FileOpen($sFilePath, $FO_READ)
    
    ; ... more code here ...
    
; Read the contents of the file using the handle returned by FileOpen.
    Local $sFileRead = FileRead($hFileOpen)

   ; ... more code here ...
   
; Close the handle returned by FileOpen.
    FileClose($hFileOpen)

maybe i do not understand the help file correctly, but as far as i could tell, it closes the file with the same parameters that it opened it with, so that is why i decided to use the name of the file, as for me that seemed to be the same, more or less at least.

 

sure there might be a way that is a lot easier, but as i have stated on numerous occasions: i am a noobie and am learning with the trial and error philosophy. i would not have known how to acomplish this:

Quote

You can just rewrite the contents and any keys that already exist will have their values changed.

do not get me wrong: i am not complaining and i am very greatful for any advice and any help that is offered to me, because every little bit lets me learn something new and that can not be a bad thing. just do not expect a noobie to understand everything he/she/it is doing.

kind regards

Share this post


Link to post
Share on other sites
39 minutes ago, roeselpi said:

maybe i do not understand the help file correctly, but as far as i could tell, it closes the file with the same parameters that it opened it with

You should look at that example again, it doesn't use the filename to close it, it uses the handle that FileOpen returns to close it. $sFilePath vs $hFileOpen.

Here's your script rewritten so that you're not manipulating the arrays so much. This makes it a lot easier to follow the program flow as well. no $testarray, just the array $ini being used.

#include <Array.au3>
Global $filename = "set.ini"
Global $standard = "chopchop"
Global $keynames = "key"

Global $values[10] = ["test1", "test2", "test3", "test4", "test5", "test6", "test7", "test8", "test9", "test0"]
_ArrayDisplay($values)

For $Loop = 0 To UBound($values) - 1
     $num = StringRight("00" & $Loop + 1, 3)
     IniWrite($filename, $standard, $keynames & $num, $values[$Loop])
Next

Global $ini = IniReadSection($filename, "chopchop")
;~ $ini = _ArrayExtract($ini, 1, -1, 1, 1)
_ArrayDisplay($ini, "$start $ini")


For $Loop = 1 To $ini[0][0]
     $ini[$Loop][1] = _Randomstring(5)
;~      _ArrayAdd($testarray, $generate, 1)
     ;_ArrayDisplay($testarray)
Next

_ArrayDisplay($ini, " end $ini")
;~ _ArrayDelete($testarray, 0)
;~ _ArrayDisplay($testarray)

;~ FileOpen($filename, 2)

For $Loop = 1 To UBound($ini) - 1
     IniWrite($filename, $standard, $ini[$Loop][0], $ini[$Loop][1])
Next

;~ FileClose($filename)

Func _Randomstring($length)
     $chars = StringSplit("abcdefghijk012345&/?\@€$§!#", "")
     $String = ""
     $i = 0
     Do
          If $length <= 0 Then ExitLoop
          $String &= $chars[Random(1, $chars[0])]
          $i += 1
     Until $i = $length
     Return $String
EndFunc   ;==>_Randomstring

 


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

wow! i see i still have a lot to learn. i see that i am lacking the ability to envision a two dimensional array and how, when and why to use [0] ore [1] or both. amazing. half as long but does the same and does not use a second array to write the data to. i think it quite fantastic that it is possible to overwrite in one and the same array even though there is no _ArrayOverwrite() function or something simular available. thank you so very much.

i am glad to say that i am more or less on track and am quite happy with myself that at least i found a solution to my initial problem, even if it is/was a 'dirty' solution.

kind regards

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

×
×
  • Create New...