Sign in to follow this  
Followers 0
Earthquake

Problem with Filename

3 posts in this topic

#1 ·  Posted (edited)

Hi Snipez,

I wanted to write a little INI writer.

The functionality is:

Select a displayed name (is needed for another program, which reads the INI).

Specify the file (via a little Browse Button).

Save the record (write displayed name and the path of the program into an array).

And last but not least a finish button, to write the INI.

I've got the program finished so far, but the users of this program want me to change the Browse Button, that not C: is displayed, but the folder in which the last Program was selected (and saved).

When I select a Program via Browse, the path may be "C:/Testfolder/Program.exe". I want to delete the "Program.exe" entry to save the "C:/Testfolder" in a new variable to use it with the Browse Button.

Here's the code (I know its a GUI, but the function should be more standard coding :whistle: ):

#include <GUIConstants.au3>

GUI()
Func GUI()
    
Local $Array1[1], $Array2[1]
Local $iniFilename
Local $Browse



    $iniDirectory = (@ScriptDir)
    $iniFilename = (@ScriptDir & '\Scriptlibary.ini')
    
    If FileExists($iniFilename) Then
        $Backup = MsgBox(1,"Backup","Do you want a backup of the old Scriptlibary.ini?")
        Select
            Case $Backup = $GUI_EVENT_CLOSE
                Exit
            Case $Backup = 1
                DirCreate($iniDirectory & "\BackupINI")
                FileDelete($iniDirectory & "\BackupINI\Scriptlibary (Backup).ini")
                FileCopy($iniFilename, $iniDirectory & '\BackupINI\Scriptlibary (Backup).ini')
                MsgBox(4096,"Stored",'Backup has been created and stored in ' & @ScriptDir & '\Backup\Scriptlibary.ini')
            Case $Backup = 2
                
            
        EndSelect
        
    EndIf


    $Counter=0
    GUICreate(".ini Creator for Tool Launcher", 395,165, -1, -1, -1, 0x00000018)
    GUICtrlCreateLabel(".ini Creator for GPSD Tool Launcher",  110, 15, 200)
    GUICtrlCreateLabel("Displayed name:",  10, 50, 100)
    GUICtrlCreateLabel("Path of the program:",  10, 85, 100)
    GUICtrlCreateLabel("Record Nr. " & $Counter +1,  120, 114, 100)
    GUICtrlCreateLabel("Note: Only saved records will appear in the .ini!",  90, 140, 250)

    $NameProgram = GUICtrlCreateInput("Name", 110,  48, 180, 20)
    $ProgramPath = GUICtrlCreateInput("Path", 110,  81, 115, 20)
    $Browse = GUICtrlCreateButton ("Browse...", 235, 81, 55, 20)
    $NextButton = GUICtrlCreateButton("Save", 300,  43, 80, 60)
    $OkButton = GUICtrlCreateButton("Finish", 300, 110, 80, 20)

    GUISetState()

    $msg = 0
    
    While $msg <> $GUI_EVENT_CLOSE
        $msg = GUIGetMsg()
        Select
            Case $msg = $GUI_EVENT_CLOSE
                Exit
            Case $msg = $OkButton
                If $Counter = 0 Then
                    $Counter = $Counter + 1
                    
                    ReDim $Array1[$Counter]
                    ReDim $Array2[$Counter]
                    
                    $Array1[$Counter - 1] = GUICtrlRead($NameProgram)
                    $Array2[$Counter - 1] = GUICtrlRead($ProgramPath)
                    
                EndIf
                
                ExitLoop
            Case $msg = $NextButton
                $Counter = $Counter + 1
                
                ReDim $Array1[$Counter]
                ReDim $Array2[$Counter]
                $Array1[$Counter - 1] = GUICtrlRead($NameProgram)
                $Array2[$Counter - 1] = GUICtrlRead($ProgramPath)
                
                GUICtrlSetData($NameProgram,"")
                GUICtrlSetData($ProgramPath,"")
                
                GUICtrlCreateLabel("Record Nr. " & $Counter +1,  120, 114, 100)
                
            Case $msg = $Browse
                $Specified = FileOpenDialog("Select the tool, you want to insert","C:\", "Executes (*.exe)")
                If @error Then
                    MsgBox(4096,"Error", "No file chosen")
                    $Specified = StringReplace($Specified, "1", "Path")
                Else
                    $Specified = StringReplace($Specified, "|", @CRLF)
                EndIf
                GUICtrlSetData($ProgramPath, $Specified)
                
        EndSelect
        
    WEnd
    
    FileOpen($iniFilename,2)
    FileWriteLine ($iniFilename,"[START]")
    FileWriteLine ($iniFilename,"")
    FileWriteLine ($iniFilename,"Amount=" & $Counter)
    FileWriteLine ($iniFilename,"")
    FileWriteLine ($iniFilename,"[Names]")
    FileWriteLine ($iniFilename,"")

    For $iCount = 0 To UBound($Array1) - 1
        FileWriteLine ($iniFilename,'Script' & $iCount + 1 & '=' & $Array1[$iCount])
        FileWriteLine ($iniFilename,$Array1[$iCount] & '=' & $Array2[$iCount])
    Next
    FileClose($iniFilename)
    MsgBox(4096,"Done!", ".ini File has been written")


EndFunc

Thx a lot!

EDIT: The INI will be writen in the folder, you save the program (but I bet u see the code and know more about it, then me :) )

Edited by Earthquake

Yes, I am a noob! But at least I try to learn!Moddingtech.de - Home of the german Modding-Scene!

Share this post


Link to post
Share on other sites



Why not use an ini file to save the path

$Specified = FileOpenDialog("Select the tool, you want to insert", IniRead(@ScriptDir & '\Settings.ini', 'Paths', 'SaveDir', 'C:\') , "Executes (*.exe)")
If @error Then
    MsgBox(4096,"Error", "No file chosen")
    $Specified = StringReplace($Specified, "1", "Path")
Else
    IniWrite(@ScriptDir & '\Settings', 'Paths', 'SaveDir', StringLeft($Specified, StringInStr($Specified, '\', 0, -1)-1))
    $Specified = StringReplace($Specified, "|", @CRLF)
EndIf

The FileOpenDialog() can read from the Settings.ini, and later can be saved to it.

Share this post


Link to post
Share on other sites

This was quick! I already thougt of something like the new .ini File, but I wasnt sure.

Thanks a lot!!!

P.S. You had a little error in the code, but I found it :whistle:

IniWrite(@ScriptDir & '\Settings[u].ini[/u]', 'Paths', 'SaveDir', StringLeft($Specified, StringInStr($Specified, '\', 0, -1)-1))

---- Problem solved ----


Yes, I am a noob! But at least I try to learn!Moddingtech.de - Home of the german Modding-Scene!

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
Sign in to follow this  
Followers 0