Jump to content

How to save file/folder path after first time File/Folder selection?.


Recommended Posts

Hi,

I am very new with AutoIt and newbie on AutoIt.  I found this from the doc and I liked what it does, opening the folder location. 
 

#include <MsgBoxConstants.au3>

Example()

Func Example()
    ; Create a constant variable in Local scope of the message to display in FileSelectFolder.
    Local Const $sMessage = "Select a folder"

    ; Display an open dialog to select a file.
    Local $sFileSelectFolder = FileSelectFolder($sMessage, "")
    If @error Then
        ; Display the error message.
        MsgBox($MB_SYSTEMMODAL, "", "No folder was selected.")
    Else
        ; Display the selected folder.
        MsgBox($MB_SYSTEMMODAL, "", "You chose the following folder:" & @CRLF & $sFileSelectFolder)
    EndIf
EndFunc   ;==>Example


But above statements does not save the folder location which I selected. I would appreciate if someone can share me knowledge/code on how to demonstrate this code into 2 functions for opening 2 folder location and saving the path. So if I rerun the functions folder selection won't show up the root dir. Instead, it will show the last selected location.

Thanks :)

Link to comment
Share on other sites

Try:

#include <MsgBoxConstants.au3>

$sFirstFolder = Example()
$sSecondFolder = Example($sFirstFolder)

Func Example($sFolder = "")
    ; Create a constant variable in Local scope of the message to display in FileSelectFolder.
    Local Const $sMessage = "Select a folder"

    ; Display an open dialog to select a file.
    Local $sFileSelectFolder = FileSelectFolder($sMessage, $sFolder)
    If @error Then
        ; Display the error message.
        MsgBox($MB_SYSTEMMODAL, "", "No folder was selected.")
    Else
        ; Display the selected folder.
        MsgBox($MB_SYSTEMMODAL, "", "You chose the following folder:" & @CRLF & $sFileSelectFolder)
        Return $sFileSelectFolder
    EndIf

EndFunc   ;==>Example

 

Link to comment
Share on other sites

Hey Subz!

Thanks for the response. Sorry, I might be confused you with my questions. Let's keep that easier. I want one folder selection as the default function does now but with additional below question.

How do you store/save the selected folder path into an .ini or text file and return it from a variable to use in AutoIt script?. So everytime script run it not always asks for selecting the folder. As it detects the file path .exe exists from ini. Using If FileExists($examplevar) .

I just want folder selection to be only one time asking instead of always. It will ask again for selection if the path doesn't exist or not found. I hope I was clear this time.

Thanks and sorry :) 

Link to comment
Share on other sites

Hi,

This is what I have came up with my own question. But Only thing I am lacking's are.

1. I want to implement it with GUI and a button. So if it pressed the function get executed.
2. It won't asks for selecting folder if FileExists or FilePathExists already from first time button execution.
3. Need to redeclare the path only from "App=" , How do I target it for reusing the path on my script and only if correct path exists based on .exe detection?.

Smart people help me please :(

Code Below:
 

#RequireAdmin
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <AutoItConstants.au3>
#include <Process.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>


Global $ini="app.ini"

If Not FileExists($ini) Then
    $hnd=FileOpen($ini,1)
    FileWrite($hnd,"[App]" & @CRLF)
    FileWriteLine($hnd,"App=")
    FileClose($hnd)
EndIf

    Sleep(1000)

Example()

Func Example()
    ; Create a constant variable in Local scope of the message to display in FileSelectFolder.
    Local Const $sMessage = "Select a folder"

    ; Display an open dialog to select a file.
    Local $sFileSelectFolder = FileSelectFolder($sMessage, "")
    If @error Then
        ; Display the error message.
        MsgBox($MB_SYSTEMMODAL, "", "No folder was selected.")
    Else
        ; Display the selected folder.
      IniWrite($ini,"App","App",$sFileSelectFolder)
      MsgBox($MB_SYSTEMMODAL, "", "You chose the following folder:" & @CRLF & $sFileSelectFolder)
;~          $appf= "" & @ProgramFilesDir & "\App\app.exe"
         $appf= "Drive:\App\app.exe"
         If FileExists($appf) then
            MsgBox(0,"App Folder Found","App Found. Running App...")
         Else
            MsgBox($MB_SYSTEMMODAL, "", "App.exe isn't located in correct path" & @CRLF & "Correct Path: " & $appf)
         EndIf

    EndIf
EndFunc   ;==>Example

 

Link to comment
Share on other sites

Not 100% sure if I read it correctly but maybe something like this:

#include <GUIConstantsEx.au3>
#include <GuiEdit.au3>
#include <MsgBoxConstants.au3>

Global $sFilePath, $sFileName = "app.ini"

Example()

Func Example()
    Local $sAppPath
    Local $hGUI = GUICreate("Example", 365, 30)
    Local $idFilePath = GUICtrlCreateInput("", 5, 5, 250, 21)
        _GUICtrlEdit_SetCueBanner($idFilePath, "<FilePath>\App.exe")
    Local $idSelect = GUICtrlCreateButton("Browse", 260, 5, 100, 21)
        GUICtrlSetState($idSelect, $GUI_FOCUS)
    GUISetState()

    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
            Case $idSelect
                GUICtrlSetData($idFilePath,_FolderSelect())
        EndSwitch
    WEnd
    GUIDelete($hGUI)
EndFunc

Func _FolderSelect()
    Local Const $sMessage = "Select a folder"

    Local $sSelectPath = FileSelectFolder($sMessage, IniRead($sFileName, "App", "App", ""))
    If @error Then
        MsgBox($MB_SYSTEMMODAL, "", "No folder was selected.")
        Return ""
    Else
        $sFilePath = $sSelectPath & "\App.exe"
        If FileExists($sFilePath) then
            IniWrite($sFileName, "App", "App", $sSelectPath)
            Return $sFilePath
        Else
            MsgBox($MB_SYSTEMMODAL, "", $sFilePath & " could not be found")
            Return ""
        EndIf
    EndIf
EndFunc

 

Link to comment
Share on other sites

5 hours ago, Subz said:

Not 100% sure if I read it correctly but maybe something like this:

#include <GUIConstantsEx.au3>
#include <GuiEdit.au3>
#include <MsgBoxConstants.au3>

Global $sFilePath, $sFileName = "app.ini"

Example()

Func Example()
    Local $sAppPath
    Local $hGUI = GUICreate("Example", 365, 30)
    Local $idFilePath = GUICtrlCreateInput("", 5, 5, 250, 21)
        _GUICtrlEdit_SetCueBanner($idFilePath, "<FilePath>\App.exe")
    Local $idSelect = GUICtrlCreateButton("Browse", 260, 5, 100, 21)
        GUICtrlSetState($idSelect, $GUI_FOCUS)
    GUISetState()

    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
            Case $idSelect
                GUICtrlSetData($idFilePath,_FolderSelect())
        EndSwitch
    WEnd
    GUIDelete($hGUI)
EndFunc

Func _FolderSelect()
    Local Const $sMessage = "Select a folder"

    Local $sSelectPath = FileSelectFolder($sMessage, IniRead($sFileName, "App", "App", ""))
    If @error Then
        MsgBox($MB_SYSTEMMODAL, "", "No folder was selected.")
        Return ""
    Else
        $sFilePath = $sSelectPath & "\App.exe"
        If FileExists($sFilePath) then
            IniWrite($sFileName, "App", "App", $sSelectPath)
            Return $sFilePath
        Else
            MsgBox($MB_SYSTEMMODAL, "", $sFilePath & " could not be found")
            Return ""
        EndIf
    EndIf
EndFunc

 

 


Hi Subz,

Thanks for improving the code :) but it still missing this 2 questions.

2. It won't asks for selecting folder if App.exe found from FileExists or FilePathExists already from first time button execution. 
3. Need to redeclare the path only from "App=" , How do I target it for reusing the path on my script and only if correct path exists based on .exe detection?.

The file path selection is working correctly. But what I want is now as I compiled this script into .exe and after selecting the valid path name by the button "browse". it will get stored in app.ini and if I close the compiled .exe and open it again then it will read the app.ini for the selectedFolder path which I used before and shows the value in the input box and use it instead so I don't have to browse every time to select the path again. And once this function set. I want to know how this path I can redeclare on the scripts from $Var which will be used from app.ini. I am guessing it will be this one? ($sSelectPath) .

Appreciate your help. thanks :)

Link to comment
Share on other sites

Yes, In that folder path location app.exe will exist. and as it already written in app.ini I want that to be set and use it again if I rerun the compiled .exe it will read the app.ini again and check for that folder path and file exists (app.exe) from app.ini if it detects the valid link it will be getting used already in your input box. So I don't have to press the browse button again. I hope I was able to explain :(

Edited by goodone
spell mistake
Link to comment
Share on other sites

Try this, just replace Notepad.exe with your app.exe.

#include <GUIConstantsEx.au3>
#include <GuiEdit.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIShPath.au3>

Global $sFileName = @ScriptDir & "\app.ini"
Global $sFilePath = IniRead($sFileName, "App", "App", "N/A")
    If FileExists($sFilePath) = 0 Then _FileSelect()

Example()

Func Example()
    Local $hGUI = GUICreate("Example", 470, 30)
    Local $idFilePath = GUICtrlCreateInput($sFilePath, 5, 5, 250, 21)
        _GUICtrlEdit_SetCueBanner($idFilePath, "<FilePath>\App.exe")
    Local $idBrowse = GUICtrlCreateButton("Browse", 260, 5, 100, 21)
    Local $idSelect = GUICtrlCreateButton("Run", 365, 5, 100, 21)
        GUICtrlSetState($idSelect, $GUI_FOCUS)

    GUISetState()

    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
            Case $idBrowse
                _FileSelect()
                GUICtrlSetData($idFilePath, $sFilePath)
            Case $idSelect
                If FileExists($sFilePath) Then Run($sFilePath)
        EndSwitch
    WEnd
    GUIDelete($hGUI)
EndFunc

Func _FileSelect()
    Local $sFolderPath = _WinAPI_PathRemoveFileSpec(IniRead($sFileName, "App", "App", ""))
    $sFilePath = FileOpenDialog("Select App Path", $sFolderPath, "Notepad.exe(Notepad.exe)")
    If @error Then
        MsgBox($MB_SYSTEMMODAL, "", "No File was selected.")
        $sFilePath = ""
    Else
        IniWrite($sFileName, "App", "App", $sFilePath)
    EndIf
EndFunc

 

Link to comment
Share on other sites

Hey Subz! Thanks. It's too close now :D

Ok here's what I don't need and need.

I don't need the RUN button and its function.|

and I need to know how can I call that FolderPath only after selection from app.ini (not the .exe). Suppose I selected notepad++.exe from its directory as script does. and now I need to use that selected .exe folder path again within my script. So which Variable should I store it in?. So it works from app.ini?.
example: I need this again for using : C:\Program Files (x86)\Notepad++\
Exe resides here C:\Program Files (x86)\Notepad++\notepad++.exe

Would be nice if you can create a extra MsgBox to echo only folder path from a variable as a demonstration after .exe selected, after line 42.

Thank you!!

Edited by goodone
Link to comment
Share on other sites

Have removed Run, I have renamed the variables to make them more understandable:

$sAppFilePath = Full Path to .\app.exe
$sAppFolderPath = Full Path without \app.exe

 

#include <GUIConstantsEx.au3>
#include <GuiEdit.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIShPath.au3>

Global $sIniPath = @ScriptDir & "\app.ini"
Global $sAppFilePath = IniRead($sIniPath, "App", "App", "N/A")
Global $sAppFolderPath = _WinAPI_PathRemoveFileSpec($sAppFilePath)
If FileExists($sAppFilePath) = 0 Then _FileSelect()

Example()

Func Example()
    Local $hGUI = GUICreate("Example", 365, 30)
    Local $idFilePath = GUICtrlCreateInput($sAppFilePath, 5, 5, 250, 21)
    _GUICtrlEdit_SetCueBanner($idFilePath, "<FilePath>\App.exe")
    Local $idBrowse = GUICtrlCreateButton("Browse", 260, 5, 100, 21)
    GUICtrlSetState($idBrowse, $GUI_FOCUS)

    GUISetState()

    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
            Case $idBrowse
                _FileSelect()
                GUICtrlSetData($idFilePath, $sAppFilePath)
        EndSwitch
    WEnd
    GUIDelete($hGUI)
EndFunc   ;==>Example

Func _FileSelect()
    Local $sFolderPath = _WinAPI_PathRemoveFileSpec(IniRead($sIniPath, "App", "App", ""))
    $sAppFilePath = FileOpenDialog("Select App Path", $sFolderPath, "Notepad.exe(Notepad.exe)")
    If @error Then
        MsgBox($MB_SYSTEMMODAL, "", "No File was selected.")
        $sAppFilePath = ""
        $sAppFolderPath = ""
    Else
        $sAppFolderPath = _WinAPI_PathRemoveFileSpec($sAppFilePath)
        IniWrite($sIniPath, "App", "App", $sAppFilePath)
    EndIf
    MsgBox(64, "App File/Folder Path(s)", "App File Path : " & $sAppFilePath & @CRLF & "App Folder Path : " & $sAppFolderPath)
EndFunc   ;==>_FileSelect

 

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...