Jump to content

skiping inwrite if already in ini doesnt work


Recommended Posts

This is the script i´m currently working on:

#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Include <FTPEx.au3>

If Not FileExists(@ScriptDir & "\Settings.ini") Then
        IniWrite(@ScriptDir & "\Settings.ini", "Servermanager", "Server", "ftp.myserver1.com")  
        IniWrite(@ScriptDir & "\Settings.ini", "Servermanager", "Port", "21")   
        IniWrite(@ScriptDir & "\Settings.ini", "Servermanager", "User", "Admin")    
        IniWrite(@ScriptDir & "\Settings.ini", "Servermanager", "Password", "123456")   
EndIf

$Server = IniRead(@ScriptDir & "\Settings.ini", "Servermanager", "Server", "NotFound")
$Port = IniRead(@ScriptDir & "\Settings.ini", "Servermanager", "Port", "NotFound")
$User = IniRead(@ScriptDir & "\Settings.ini", "Servermanager", "User", "NotFound")
$Password = IniRead(@ScriptDir & "\Settings.ini", "Servermanager", "Password", "NotFound")

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Syncer 0.1", 615, 438, 657, 348)
$Server_Label = GUICtrlCreateLabel("Server", 96, 32, 36, 17)
$Server_Input = GUICtrlCreateInput($Server, 144, 32, 145, 21)
$Port_Label = GUICtrlCreateLabel("Port", 368, 32, 36, 17)
$Port_Input = GUICtrlCreateInput($Port, 416, 32, 57, 21)
$User_Label = GUICtrlCreateLabel("User", 96, 88, 36, 17)
$User_Input = GUICtrlCreateInput($User, 144, 88, 113, 21)
$Password_Label = GUICtrlCreateLabel("Password", 96, 128, 36, 17)
$Password_Input = GUICtrlCreateInput($Password, 144, 128, 113, 21)
$Save_Button = GUICtrlCreateButton("Save", 104, 264, 129, 33)

$File_Button = GUICtrlCreateButton("File", 264, 264, 129, 33)
$Folder_Button = GUICtrlCreateButton("Folder", 440, 264, 129, 33)
$Button4 = GUICtrlCreateButton("Button4", 104, 328, 129, 25)
$Button5 = GUICtrlCreateButton("Button5", 264, 328, 129, 33)
$Button6 = GUICtrlCreateButton("Button6", 448, 336, 121, 33)
;$hLog_Edit = GUICtrlCreateEdit("", 168, 96, 465, 321)
#EndRegion ### END Koda GUI section ###

GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit        
        Case $Save_Button
            $Server = GUICtrlRead($Server_Input)
            IniWrite(@ScriptDir & "\Settings.ini", "Servermanager", "Server", $Server)  
            $Port = GUICtrlRead($Port_Input)
            IniWrite(@ScriptDir & "\Settings.ini", "Servermanager", "Port", $Port)
            $User = GUICtrlRead($User_Input)
            IniWrite(@ScriptDir & "\Settings.ini", "Servermanager", "User", $User)
            $Password = GUICtrlRead($Password_Input)        
            IniWrite(@ScriptDir & "\Settings.ini", "Servermanager", "Password", $Password)
        Case $File_Button
            $File_Var = FileOpenDialog("Select file(s):", "D:\", "All (*.*)|Images (*.jpg;*.bmp;*.png;*.psd;*.gif)|Videos (*.avi;*.mpg;*.mp4;*.mov;*.flv;*.mkv)|Music (*.mp3;*.wav;*.flac;*.cue)", 1 + 2 + 4)   ;more       
            If @error Then ContinueLoop
            $OnlyFileName = StringSplit($File_Var,"|")
            $Folder = $OnlyFileName[1]
            $Count = $OnlyFileName[0]
            While $Count > 1 ; ohne folder .. > 0 = mit folder
                $Count_Exists = IniReadSection(@ScriptDir & "\Files.ini", "Files")
                If @error Then ContinueLoop
                $INI_Lines = 0
                For $i = 1 To $Count_Exists[0][0]
                    $INI_Lines = $INI_Lines + 1
                Next
                For $i = 1 To $Count_Exists[0][0]
                    If $Count_Exists[$i][1] = $Folder & "\" & $OnlyFileName[$Count] Then                        
                    ;   If $Count_Exists[$i][0] = $Folder Then
                        ;msgbox(0, "test", $Count_Exists[$i][1] & " is " & $OnlyFileName[$Count])
                    Else
                            msgbox(0, "test", $Folder & "\" & $OnlyFileName[$Count])
                            ;msgbox(0, "test", $Count_Exists[$i][1])
                                IniWrite(@ScriptDir & "\Files.ini", "Files", "File" & $INI_Lines + 1, $Folder & "\" & $OnlyFileName[$Count])
                        ;EndIf
                    EndIf
                Next
                $Count = $Count - 1
                ;GUICtrlSetData($hFile, $sTmpFile); GUI will be updated at next iteration   
            Wend
        Case $Folder_Button
            $var1 = FileSelectFolder("Choose a folder.", "")
        

    EndSwitch
        
WEnd

So $File_Button should open a dialog where u can select files.. if those files are already saved inside the files.ini, they wont be added.. so only new files are getting added to the ini.

with single files it works.. but when selecting multiple files it wont work.. and i dont get why :S

thanks

Link to comment
Share on other sites

Because this line: If $Count_Exists[$i][1] = $Folder & "\" & $OnlyFileName[$Count] Then is only checking 1 line in the INI file against 1 file being added. You need to loop through the entire section for every file to check against, or use _ArraySearch() to see if the file name is in the ini file.

Although I'm not sure why you'd need to worry about this, because if the new value and the old value are the same, then it will just overwrite it, not add it.

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

Link to comment
Share on other sites

well the script currently adds whatever you select at the bottom of the .ini .. so if you add 20 files , saved & exit , then the next time you open it and add another 5 files, it will be added below those 20 files.. also if they are duplicates.

Link to comment
Share on other sites

If the 2 keys are the same, the values will be overwritten. If you're changing the keys, then it will add them. All I can suggest is to correct your search loop and that should fix the problem.

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

Link to comment
Share on other sites

well yes it will overwrite them.. but what if i add the keys in a different alignment

e.g. i add:

key1=a

key2=b

key3=c

and then i add

key1=b

key2=c

the output would be:

key1=b

key2=c

key3=c

or am i wrong? atleast thats what i got when trying it out :S

so i tried to make a script which wont overwrite/add anything unless it doesnt exist in the .ini file.. if so, it will add it as a new key with the next logical number. because i wont need/dont want 2 same values.

will try to fix the search loop tomorrow :huh2:

thanks

Edited by aphesia
Link to comment
Share on other sites

Here's your script rewritten and updated to search the files correctly. I have added some error checking into it and eliminated some redundant looping that wasn't necessary.

#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <FTPEx.au3>
#include <array.au3>
If Not FileExists(@ScriptDir & "\Settings.ini") Then
    IniWrite(@ScriptDir & "\Settings.ini", "Servermanager", "Server", "ftp.myserver1.com")
    IniWrite(@ScriptDir & "\Settings.ini", "Servermanager", "Port", "21")
    IniWrite(@ScriptDir & "\Settings.ini", "Servermanager", "User", "Admin")
    IniWrite(@ScriptDir & "\Settings.ini", "Servermanager", "Password", "123456")
EndIf

$Server = IniRead(@ScriptDir & "\Settings.ini", "Servermanager", "Server", "NotFound")
$Port = IniRead(@ScriptDir & "\Settings.ini", "Servermanager", "Port", "NotFound")
$User = IniRead(@ScriptDir & "\Settings.ini", "Servermanager", "User", "NotFound")
$Password = IniRead(@ScriptDir & "\Settings.ini", "Servermanager", "Password", "NotFound")

#region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Syncer 0.1", 615, 438, 657, 348)
$Server_Label = GUICtrlCreateLabel("Server", 96, 32, 36, 17)
$Server_Input = GUICtrlCreateInput($Server, 144, 32, 145, 21)
$Port_Label = GUICtrlCreateLabel("Port", 368, 32, 36, 17)
$Port_Input = GUICtrlCreateInput($Port, 416, 32, 57, 21)
$User_Label = GUICtrlCreateLabel("User", 96, 88, 36, 17)
$User_Input = GUICtrlCreateInput($User, 144, 88, 113, 21)
$Password_Label = GUICtrlCreateLabel("Password", 96, 128, 36, 17)
$Password_Input = GUICtrlCreateInput($Password, 144, 128, 113, 21)
$Save_Button = GUICtrlCreateButton("Save", 104, 264, 129, 33)

$File_Button = GUICtrlCreateButton("File", 264, 264, 129, 33)
$Folder_Button = GUICtrlCreateButton("Folder", 440, 264, 129, 33)
$Button4 = GUICtrlCreateButton("Button4", 104, 328, 129, 25)
$Button5 = GUICtrlCreateButton("Button5", 264, 328, 129, 33)
$Button6 = GUICtrlCreateButton("Button6", 448, 336, 121, 33)
;$hLog_Edit = GUICtrlCreateEdit("", 168, 96, 465, 321)
#endregion ### END Koda GUI section ###

GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Save_Button
            $Server = GUICtrlRead($Server_Input)
            IniWrite(@ScriptDir & "\Settings.ini", "Servermanager", "Server", $Server)
            $Port = GUICtrlRead($Port_Input)
            IniWrite(@ScriptDir & "\Settings.ini", "Servermanager", "Port", $Port)
            $User = GUICtrlRead($User_Input)
            IniWrite(@ScriptDir & "\Settings.ini", "Servermanager", "User", $User)
            $Password = GUICtrlRead($Password_Input)
            IniWrite(@ScriptDir & "\Settings.ini", "Servermanager", "Password", $Password)
        Case $File_Button
            $File_Var = FileOpenDialog("Select file(s):", "D:\", "All (*.*)|Images (*.jpg;*.bmp;*.png;*.psd;*.gif)|Videos (*.avi;*.mpg;*.mp4;*.mov;*.flv;*.mkv)|Music (*.mp3;*.wav;*.flac;*.cue)", 1 + 2 + 4) ;more
            If @error Then ContinueLoop
            $OnlyFileName = StringSplit($File_Var, "|")
            $Folder = $OnlyFileName[1]
            $Count = $OnlyFileName[0]
            $Count_Exists = IniReadSection(@ScriptDir & "\Files.ini", "Files")
            If @error Then
                IniWrite(@ScriptDir & "\Files.ini", "Files", "File0", "Dummy")
                MsgBox(64, "Error", "Ini file error, created empty ini file")
                ContinueLoop
            EndIf
            $INI_Lines = $Count_Exists[0][0]
            For $I = 2 To $Count
                Local $Test = _ArraySearch($Count_Exists, $OnlyFileName[$I], 0, 0, 0, 1, 0, 1)
                Local $Error = @error
                If $Test = -1 And $Error = 6 Then
                    IniWrite(@ScriptDir & "\Files.ini", "Files", "File" & $INI_Lines + $I, $Folder & $OnlyFileName[$I])
                EndIf
            Next
        Case $Folder_Button
            $var1 = FileSelectFolder("Choose a folder.", "")


    EndSwitch

WEnd

This code will create the Files.ini file if it doesn't exist, or if it exists and there's nothing in the Files section will pad it with a dummy entry, otherwise it crashes. It uses an array search and a For...Next loop to loop through all of your new file entries to check for duplicates. _ArraySearch isn't the fastest search method out there, but without going crazy it will do the trick for a few files at a time.

If you were adding hundreds of files to the ini at a time, I would use the modified _Array2D_binarysearch that I have in my signature. You'd have to sort the array, but it's hundreds of times faster than arraysearch.

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

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...