FMS

File write in specifik location

10 posts in this topic

#1 ·  Posted (edited)

Hello,

 

I've this code that I'm writing.
The problem I've got is in the function "create_new_buttons_section".
This will create a new "section" in the ini file

At first run of this script he will check iff the ini file is present, and if not make a new one.
The new ini file is in a surten order like :

[btn101]
[btn102]
[btn104]
[btn201]
[btn202]

In this example:
if the button btn103 is missing function "create_new_buttons_section" will make a new "section" for btn103.
Afther this function the ini file will look like:
 

[btn101]
[btn102]
[btn104]
[btn201]
[btn202]
[btn103]

For human readeble , it's better if it will be in a number order.
Does somebody know how to do this?

 

#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <GUIConstantsEx.au3>
#include <TabConstants.au3>
#include <File.au3>
#include <WindowsConstants.au3>
#include <MsgBoxConstants.au3>

check_files_exist()

$Form1 = GUICreate("Form1", 320, 260, 192, 324);(b,h,,),30
$Tab1 = GUICtrlCreateTab(5, 5, 310, 250) ;(,,b,h)
   $TabSheet1 = GUICtrlCreateTabItem("Typ it in")
      $Btn101 = GUICtrlCreateButton(Read_from_ini("btn","btn101","button_name","Ini_Err_btn_name"), 15, 40, 75, 25)
      $Btn102 = GUICtrlCreateButton(Read_from_ini("btn","btn102","button_name","Ini_Err_btn_name"), 125, 40, 75, 25)
      $Btn103 = GUICtrlCreateButton(Read_from_ini("btn","btn103","button_name","Ini_Err_btn_name"), 230, 40, 75, 25)
      $Btn104 = GUICtrlCreateButton(Read_from_ini("btn","btn104","button_name","Ini_Err_btn_name"), 15, 70, 75, 25)
      $Btn105 = GUICtrlCreateButton(Read_from_ini("btn","btn105","button_name","Ini_Err_btn_name"), 125, 70, 75, 25)
      $Btn106 = GUICtrlCreateButton(Read_from_ini("btn","btn106","button_name","Ini_Err_btn_name"), 230, 70, 75, 25)
  $TabSheet2 = GUICtrlCreateTabItem("Batch")
      $Btn201 = GUICtrlCreateButton(Read_from_ini("btn","btn201","button_name","Ini_Err_btn_name"), 15, 40, 75, 25)
      $Btn202 = GUICtrlCreateButton(Read_from_ini("btn","btn202","button_name","Ini_Err_btn_name"), 125, 40, 75, 25)
GUICtrlCreateTabItem("")
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
         Case $Btn101
            MsgBox($MB_SYSTEMMODAL, "function", "button pushed")
         Case $Btn102
            MsgBox($MB_SYSTEMMODAL, "function", "button pushed")
         Case $Btn101
            MsgBox($MB_SYSTEMMODAL, "function", "button pushed")
         Case $Btn102
            MsgBox($MB_SYSTEMMODAL, "function", "button pushed")
         Case $Btn101
            MsgBox($MB_SYSTEMMODAL, "function", "button pushed")
         Case $Btn102
            MsgBox($MB_SYSTEMMODAL, "function", "button pushed")
         Case $Btn201
            MsgBox($MB_SYSTEMMODAL, "function", "button pushed")
         Case $Btn202
            MsgBox($MB_SYSTEMMODAL, "function", "button pushed")
    EndSwitch
WEnd


Func check_files_exist()
   If FileExists(@SCRIPTDIR & "\Buttons.ini") Then
      Global $ini_location_buttons = @SCRIPTDIR & "\Buttons.ini"
   Else
      If Not _FileCreate(@SCRIPTDIR & "\Buttons.ini") Then
         MsgBox($MB_SYSTEMMODAL, "Error", " Error Creating/Resetting ini.      error:" & @error)
      Else
         Global $ini_location_buttons = @SCRIPTDIR & "\Buttons.ini"
         create_new_buttons_ini()
      EndIf
   EndIf
EndFunc

Func create_new_buttons_ini();create ini file until button 3

   $file = FileOpen($ini_location_buttons, 1)

      Local $i = 1
      local $j = 0

      Do
         if $i = 10 then $j = ""
         FileWrite($file, "[btn1" & $j & $i & "]"& @CRLF)
         FileWrite($file, "button_name=Button " & $i & @CRLF)
         $i = $i + 1
      Until $i = 3
   FileClose($file)
EndFunc

Func Read_from_ini($soort,$section,$key,$value=NULL)
   if $soort = "btn" Then
      $ini_location_local = $ini_location_buttons
   ElseIf $soort = "app" Then
      $ini_location_local = $ini_location_app
   ElseIf $soort = "db" Then
      $ini_location_local = $ini_location_db
   EndIf

   $sRead = IniRead($ini_location_local, $section, $key, $value)

   if $sRead = "Ini_Err_btn_name" Then
      create_new_buttons_section($section)
      ;$sRead = "not known"
   EndIf

   Return $sRead
EndFunc

Func create_new_buttons_section($btn_name)
   $pre_name = StringRight($btn_name, 2)

   if $pre_name < 10 then
      $pre_name = StringRight($btn_name, 1)
   EndIf

   $file = FileOpen($ini_location_buttons, 1)
      FileWrite($file, "[" & $btn_name & "]"& @CRLF)
      FileWrite($file, "button_name=Button " & $pre_name & @CRLF)
   FileClose($file)
   RestartApp();RestartApp("MsgBox")
EndFunc

Func RestartApp($switch=NULL)
   if $switch = "MsgBox" Then
      Local $iMsgBoxAnswer = MsgBox(308, "Restarting...", "Are you sure you want to restart?")
      If $iMsgBoxAnswer <> 6 Then Return

      If @Compiled Then
        Run(FileGetShortName(@ScriptFullPath))
      Else
        Run(FileGetShortName(@AutoItExe) & " " & FileGetShortName(@ScriptFullPath))
      EndIf
      Exit
   Else
      If @Compiled Then
        Run(FileGetShortName(@ScriptFullPath))
      Else
        Run(FileGetShortName(@AutoItExe) & " " & FileGetShortName(@ScriptFullPath))
      EndIf
      Exit
   EndIf
EndFunc

 

Edited by FMS

as finishing touch god created the dutch

Share this post


Link to post
Share on other sites



#3 ·  Posted (edited)

The issue you're going to run into with INI files is that the sections, or their contents, are not in any type of order, so unless you destroy the contents and rewrite them in the order you want (as J1 said above) the order isn't guaranteed.

Edited by BrewManNH

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

Thanks for your input @JohnOne and @BrewManNH.

humm this does look a good way to try this.
But the noob I'm whit autoit say's, thats complicated.
Also I dont know where to start whit Read the complete ini and organize the complete ini.
Espacialy when all the key's in a section can vary.
Can somybody help me whit this or push me in the right direction?


as finishing touch god created the dutch

Share this post


Link to post
Share on other sites

Simplest way that comes to mind is to ...

1. rename original ini file

2. create new real name ini file

3. read renamed ini file

4. write new stuff to new real name ini file

here is something to get you started, the renaming and creating new file are omitted, this should be enough for you to understand...

#include <Array.au3> 

$sIni = "Temp.ini"

$aSectionNames = IniReadSectionNames($sIni)

_ArrayDisplay($aSectionNames, "Unsorted") ;what it did look like
_ArraySort($aSectionNames)
_ArrayDisplay($aSectionNames, "Sorted") ; what it does look like

For $i = 1 To $aSectionNames[0]
    $aTempSection = IniReadSection($sIni, $aSectionNames[$i])
    ;Write section to new ini file
    _ArrayDisplay($aTempSection) ;just to show
Next

 


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

many thanks @JohnOne :) this totaly helps me.
Also thanks for the explanation, this is very helpfull for the newby inside me ;)
I'll give it a try.


as finishing touch god created the dutch

Share this post


Link to post
Share on other sites

 

Hello,

This weekend  I was trying the option what @JohnOne was explaining.
I think I am prety far but something is going wrong here :(
Could somebody tell me what I'm doing wrong here?

Func sort_and_rebuild_ini($filename)

$ini_location = @SCRIPTDIR & "\" & $filename

FileMove ( $ini_location , @SCRIPTDIR & "\old_" & $filename , 1 )

$ini_location_old = @SCRIPTDIR & "\old_" & $filename

$SectionNames_from_ini = IniReadSectionNames($ini_location_old)

;_ArrayDisplay($SectionNames_from_ini, "Unsorted") 
_ArraySort($SectionNames_from_ini)
;_ArrayDisplay($SectionNames_from_ini, "Sorted")

$file = FileOpen($ini_location_buttons, 1)

Local $i = 1
Local $j = 1

Do
   FileWrite($file, "[" & $SectionNames_from_ini[$i] & "]" & @CRLF)
   
   Do
       $TempSection = IniReadSection($ini_location_old, $SectionNames_from_ini[$i][$j])
      ;_ArrayDisplay($TempSection) ;just to show
      FileWrite($file, $TempSection) & @CRLF)
      $j = $j + 1
   Until $j = $TempSection[0]
   $i = $i + 1
Until $i = $SectionNames_from_ini[0]

FileClose($file)

EndFunc

 

 

 


as finishing touch god created the dutch

Share this post


Link to post
Share on other sites

You should always outline what exactly is going wrong.

You seem to be overcomplicating this.

Look at the functions IniReadSection and IniWriteSection.

Add the new section as normal before you sort and re-write it.


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

 

For futher reference :)
This is what i bluid ....
maybe usefull for somebody, or if sombody knows a better way -> I'm all ears ..

Func rebuild_ini()
$ini_location = @SCRIPTDIR & "\ini.ini"

   FileMove ( $ini_location , @SCRIPTDIR & "\old_ini.ini" , 1 )
   $old_file = @SCRIPTDIR & "\old_ini.ini"
   $SectionNames = IniReadSectionNames($old_file)
   _ArraySort($SectionNames)

   $openfile = FileOpen($ini_location, 1)

   For $i = 1 To $SectionNames[0]
       $key = IniReadSection($old_file, $SectionNames[$i])
      For $j = 1 To $key[0][0]
         iniWrite($ini_location, $SectionNames[$i], $key[$j][0],$key[$j][1])
      Next
    Next

   FileClose($openfile)
   FileDelete($old_file)
EndFunc

 

Edited by FMS

as finishing touch god created the dutch

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