Jump to content

Need help with FileSelectFolder


Guest
 Share

Recommended Posts

Hello,

I Want to call to function FileSelectFolder() so the the result will be that the full path to the selected folder will be displayed in the GUI in this way:

$Path_to_the_folder & "" & $ScriptName

I did search and I have not found an answer. I of course tried to get this result myself and I could not succeed.


I hope you understand what I want.
Thanks for helpers!
 
Edit:
to be more clear, that's what I want to get:

2ynjmkg.jpg

Edited by Guest
Link to comment
Share on other sites

@ScriptName holds the name of the script, the rest is just what you've posted. Put it in a label or change the title of the GUI to reflect the path and file name.

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

@ScriptName holds the name of the script, the rest is just what you've posted. Put it in a label or change the title of the GUI to reflect the path and file name.

 

But it is not autoit GUI .

or it is?

if so then what is the name of that input (The name of the variable to update) ?

EDIT:

that's what I want to get:

2ynjmkg.jpg

Edited by Guest
Link to comment
Share on other sites

When you open FileSelectFolder, the 4th parameter is the folder to open the select to, set it in your script to open it at wherever you need it to open. If the folder doesn't exist, it might not work. Are you trying to open a folder that has the same name as your script?

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

When you open FileSelectFolder, the 4th parameter is the folder to open the select to, set it in your script to open it at wherever you need it to open. If the folder doesn't exist, it might not work. Are you trying to open a folder that has the same name as your script?

I think you do not understand my goal.

I'm not trying to open a folder.

I want to generate an "installation" path through this GUI.

It does not matter if the folder "Script name"

in the example does not exist. that is the main point.

at the end, that function return a path and the rest of the code will create the path if the path does not exist.

when you install something in some folder you always want to install it under folder that called with the name of the script or some other name..

I want that when you select some folder to install to, you will see immediately in the GUI (GUI of FileSelectFolder) that the path will be:

the selected folder & the name of the program

Edited by Guest
Link to comment
Share on other sites

  • Moderators

I must not understand either. You say you want to get the name of the path from FileSelectFolder, which you would do simply. Please explain in detail what we're missing.

$var = FileSelectFolder("Please choose a folder", "C:\", 2)
    MsgBox(0, "", $var)

-Or, if the path you want doesn't exist yet-

$var = FileSelectFolder("Please choose a folder", "C:\", 2)
$path = DirCreate($var & "\Installation Dir")

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

JLogan3o13,

this is what i have now:

$var = FileSelectFolder("Please choose a folder", "C:\", 2)
$path = DirCreate($var & "\Installation Dir")

what i want to improve is that when you select a folder in the gui of FileSelectFolder, you will see immediately this:

$var & "Installation Dir"

in the GUI.  as you can see in the example image

Edited by Guest
Link to comment
Share on other sites

What have you tried, because the function allows you to specify that already.

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

I am guessing something like this is what is wanted

#RequireAdmin
; create target folder if needed
$folder_created = FileExists(@ProgramFilesDir & '\Target Folder') ? False : (DirCreate(@ProgramFilesDir & '\Target Folder') = 1)
; select a folder
$path = FileSelectFolder('Confirm the path to use. Oh look, "Target Folder" exists!', @ProgramFilesDir, 4, @ProgramFilesDir & '\Target Folder')
If @error Then
    ; cleanup after cancelled selection
    If $folder_created Then DirRemove(@ProgramFilesDir & '\Target Folder')
    MsgBox(0x40030, @ScriptName, 'You cancelled')
Else
    ; show chosen path
    MsgBox(0, @ScriptName, $path)
EndIf
Link to comment
Share on other sites

What have you tried, because the function allows you to specify that already.

this is the closest thing I got:

FileSelectFolder("Please choose a folder", "", 4)

with 4 it display the input area but not the way i want.

this is the the result of this line in my system:

ZekPYuJ.png

Edited by Guest
Link to comment
Share on other sites

  • Moderators

I believe the OP is saying that, when he browses through the treeview and selects a folder, the full path is updated in the window rather than just the selected folder (correct me if I am wrong). I would think you'd need to do a custom gui with your own treeview for this, though I don't see the need as what is returned from FileSelectFolder will be the full path anyway.

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

I believe the OP is saying that, when he browses through the treeview and selects a folder, the full path is updated in the window rather than just the selected folder (correct me if I am wrong). I would think you'd need to do a custom gui with your own treeview for this, though I don't see the need as what is returned from FileSelectFolder will be the full path anyway.

You are right.

I did not expect that FileSelectFolder function does not have the option because I've seen some installations when FileSelectFolder GUI was acted this way.. Maybe it was not FileSelectFolder after all..

Edited by Guest
Link to comment
Share on other sites

  • Moderators

I guess it goes back to my question, since the FileSelectFolder returns the full path, is it really a hard requirement that you see the path update when you select it?

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

I guess it goes back to my question, since the FileSelectFolder returns the full path, is it really a hard requirement that you see the path update when you select it?

This is an important requirement.

Because it saves GUIs to the user.

that way i don't need to create that kind of GUI:

5vx36b.jpg

All the info that the user needs to know will be in FileSelectFolder

Edited by Guest
Link to comment
Share on other sites

FileSelectFolder doesn't have that capability unless you make your own.

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

OK thanks!

After I realized I would have to do it the hard way, i did it!

Please see this code and tell me if it working:

#Include <GUIConstantsEx.au3>
#Include <GUIImageList.au3>
#Include <GUITreeView.au3>
#Include <TreeViewConstants.au3>
#Include <WindowsConstants.au3>
#Include <WinAPIEx.au3>
#Include <Array.au3>

;#AutoIt3Wrapper_Run_AU3Check=n

;~ Opt('MustDeclareVars', 1)

$_FileSelectFolder = _FileSelectFolder("","Choose where you want to place the Script","Script Name")
ConsoleWrite("Selected path:"&@CRLF&$_FileSelectFolder&@CRLF)


; #FUNCTION# ====================================================================================================================
; Name ..........: _FileSelectFolder
; Description ...: Custom FileSelectFolder
; Syntax ........: _FileSelectFolder([$Title = ""[, $Text = ""[, $InstallDir = ""]]])
; Parameters ....: $Title               - [optional] The title of the GUI. Default is "".
;                  $Text                - [optional] The text to show in the GUI. Default is "".
;                  $InstallDir          - [optional] If $InstallDir <> "" then when you select a folder, the path will automatically update with the new folder... Default is "".
; Return values .: 0 if the user exit from the GUI
;                   Path string if the user cliked OK
; Author ........: gil900. Based on:
;                   http://www.autoitscript.com/forum/topic/124430-display-on-the-fly-a-directory-tree-in-a-treeview/
;                   http://www.autoitscript.com/forum/topic/113403-fileispathvalid-udf/
; Modified ......: NONE
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: Yes
; Thanks to .....: Yashied and Shafayat
; ===============================================================================================================================
Func _FileSelectFolder($Title = "",$Text = "",$InstallDir = "")
    Local $OK_Button , $Cancel_Button , $aDrives
    Local $hForm , $hImageList , $hItem , $hNext , $sPath , $Output = 0
    Global $hTreeView , $hSelect = 0, $Path_Input, $Dummy1, $Dummy2 , $GInstallDir = $InstallDir ;, $sRoot = "\" , $Input

    $hForm = GUICreate($Title, 328, 314)
    $Path_Input = GUICtrlCreateInput("Please select a folder", 12, 36, 304, 21)

    $OK_Button = GUICtrlCreateButton("OK", 160, 280, 73, 25)
    $Cancel_Button = GUICtrlCreateButton("Cancel", 239, 280, 73, 25)
    GUICtrlCreateLabel($Text, 16, 11, 300, 17)
    GUICtrlSetFont(-1, 9, 800, 0, "Tahoma")

    GUICtrlCreateTreeView(12, 74, 304, 186, -1, $WS_EX_CLIENTEDGE)
    $hTreeView = GUICtrlGetHandle(-1)
    $Dummy1 = GUICtrlCreateDummy()
    $Dummy2 = GUICtrlCreateDummy()


    If _WinAPI_GetVersion() >= '6.0' Then
        _WinAPI_SetWindowTheme($hTreeView, 'Explorer')
    EndIf

    $hImageList = _GUIImageList_Create(16, 16, 5, 1)
    _GUIImageList_AddIcon($hImageList, @SystemDir & '\shell32.dll', 3)
    _GUIImageList_AddIcon($hImageList, @SystemDir & '\shell32.dll', 4)
    _GUICtrlTreeView_SetNormalImageList($hTreeView, $hImageList)

    $aDrives = DriveGetDrive("FIXED")
    If IsArray($aDrives) Then
        For $a = 1 To $aDrives[0]
            _TVUpdate($hTreeView,_GUICtrlTreeView_AddChild($hTreeView, 0,StringUpper($aDrives[$a]), 0, 0) )
        Next
    EndIf
    _TVUpdate($hTreeView,_GUICtrlTreeView_AddChild($hTreeView, 0,@DesktopDir, 0, 0) )


    GUIRegisterMsg($WM_NOTIFY, 'WM_NOTIFY')
    GUISetState()


    ;If $GInstallDir <> "" Then $GInstallDir = "\"&$GInstallDir

    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE , $Cancel_Button
                $Output = 0
                ExitLoop
            Case $Dummy1 ; Update
                ;_ArrayDisplay(GUIGetMsg(1))

                GUISetCursor(1, 1)
                $hItem = _GUICtrlTreeView_GetFirstChild($hTreeView, GUICtrlRead($Dummy1))
                If $hItem Then
                    While $hItem
                        $hNext = _GUICtrlTreeView_GetNextSibling($hTreeView, $hItem)
                        If Not _TVUpdate($hTreeView, $hItem) Then
                            _GUICtrlTreeView_Delete($hTreeView, $hItem)
                        EndIf
                        $hItem = $hNext
                    WEnd
                    _WinAPI_RedrawWindow($hTreeView)
                EndIf
                GUISetCursor(2, 0)
            Case $OK_Button
                $Output = GUICtrlRead($Path_Input)
                If Not _FileIsPathValid($Output) Then
                    MsgBox(48,"Error","Please select a valid path",0,$hForm)
                Else
                    ExitLoop
                EndIf
        EndSwitch
    WEnd

    GUIDelete($hForm)
    GUIRegisterMsg($WM_NOTIFY, '')
    Return $Output
EndFunc



Func _TVGetPath($hTV, $hItem, $sRoot)

    Local $Path = StringRegExpReplace(_GUICtrlTreeView_GetTree($hTV, $hItem), '([|]+)|(\\[|])', '\\')

    If Not $Path Then
        Return ''
    EndIf
    If Not StringInStr($Path, ':') Then
        Return StringRegExpReplace($sRoot, '(\\[^\\]*(\\|)+)\Z', '\\') & $Path
    EndIf
    Return $Path
EndFunc   ;==>_TVGetPath

Func _TVSetPath($hTV, $hItem, $sRoot)
    ;GUICtrlSetData($Input, _WinAPI_PathCompactPath($hInput, _TVGetPath($hTV, $hItem, $sRoot), 554))
    Local $NewPath = _TVGetPath($hTV, $hItem, $sRoot)
    If $GInstallDir <> "" And StringRight($NewPath,1) <> "\" Then $NewPath = $NewPath&"\"
    GUICtrlSetData($Path_Input,$NewPath&$GInstallDir)
    $hSelect = $hItem
EndFunc   ;==>_TVSetPath

Func _TVUpdate($hTV, $hItem)

    Local $hImageList = _SendMessage($hTV, $TVM_GETIMAGELIST)

    Local $Path = StringRegExpReplace(_TVGetPath($hTV, $hItem, "\"), '\\+\Z', '')
    ;ConsoleWrite($Path&@CRLF)
    ;If DirGetSize($Path) = 0 Then Return 1
    Local $hSearch, $hIcon, $Index, $File

    $hSearch = FileFindFirstFile($Path & '\*')
    If $hSearch = -1 Then
        If Not @error Then
            If FileExists($Path) Then
;               If _WinAPI_PathIsDirectory($Path) Then
;                   ; Access denied
;               EndIf
            Else
                Return 0
            EndIf
        EndIf
    Else
        While 1
            $File = FileFindNextFile($hSearch)
            If @error Then
                ExitLoop
            EndIf
            ;If DirGetSize($Path) = 0 Then Return 0

            If @extended Then
                _GUICtrlTreeView_AddChild($hTV, $hItem, $File, 0, 0)

            EndIf
        WEnd
        FileClose($hSearch)
        ;ConsoleWrite($File&@CRLF)
    EndIf



;~  $hSearch = FileFindFirstFile($Path & '\*')
;~     If $hSearch = -1 Then

;~     Else
;~         While 1
;~             $File = FileFindNextFile($hSearch)
;~             If @error Then
;~                 ExitLoop
;~             EndIf
;~             If Not @extended Then
;~                 $hIcon = _WinAPI_ShellExtractAssociatedIcon($Path & '\' & $File, 1)
;~                 $Index = _GUIImageList_ReplaceIcon($hImageList, -1, $hIcon)
;~                 _GUICtrlTreeView_AddChild($hTV, $hItem, $File, $Index, $Index)
;~                 _WinAPI_DestroyIcon($hIcon)
;~             EndIf
;~         WEnd
;~         FileClose($hSearch)
;~     EndIf


    Return 1
EndFunc   ;==>_TVUpdate


Func WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)
    ;ConsoleWrite($hWnd&" , "&$iMsg&" , "&$wParam&" , "&$lParam&@CRLF)
    Local $tNMTREEVIEW = DllStructCreate($tagNMTREEVIEW, $lParam)
    Local $hItem = DllStructGetData($tNMTREEVIEW, 'NewhItem')
    Local $iState = DllStructGetData($tNMTREEVIEW, 'NewState')
    Local $hTV = DllStructGetData($tNMTREEVIEW, 'hWndFrom')
    Local $ID = DllStructGetData($tNMTREEVIEW, 'Code')
    Local $tTVHTI, $tPoint

    Switch $hTV
        Case $hTreeView
            Switch $ID
                Case $TVN_ITEMEXPANDEDW
                    If Not FileExists(_TVGetPath($hTV, $hItem, "\")) Then
                        _GUICtrlTreeView_Delete($hTV, $hItem)
                        If BitAND($iState, $TVIS_SELECTED) Then
                            _TVSetPath($hTV, _GUICtrlTreeView_GetSelection($hTV), "\")
                        EndIf
                    Else
                        If Not BitAND($iState, $TVIS_EXPANDED) Then
                            _GUICtrlTreeView_SetSelectedImageIndex($hTV, $hItem, 0)
                            _GUICtrlTreeView_SetImageIndex($hTV, $hItem, 0)
                        Else
                            _GUICtrlTreeView_SetSelectedImageIndex($hTV, $hItem, 1)
                            _GUICtrlTreeView_SetImageIndex($hTV, $hItem, 1)
                            If Not _GUICtrlTreeView_GetItemParam($hTV, $hItem) Then
                                _GUICtrlTreeView_SetItemParam($hTV, $hItem, 0x7FFFFFFF)
                                GUICtrlSendToDummy($Dummy1, $hItem)
                            EndIf
                        EndIf
                    EndIf
                Case $TVN_SELCHANGEDW
                    If BitAND($iState, $TVIS_SELECTED) Then
                        If Not FileExists(_TVGetPath($hTV, $hItem, "\")) Then
                            _GUICtrlTreeView_Delete($hTV, $hItem)
                            $hItem = _GUICtrlTreeView_GetSelection($hTV)
                        EndIf
                        If $hItem <> $hSelect Then
                            _TVSetPath($hTV, $hItem, "\")
                        EndIf
                    EndIf
                Case $NM_RCLICK
                        $tPoint = _WinAPI_GetMousePos(1, $hTV)
                        $tTVHTI = _GUICtrlTreeView_HitTestEx($hTV, DllStructGetData($tPoint, 1), DllStructGetData($tPoint, 2))
                        $hItem = DllStructGetData($tTVHTI, 'Item')
                        If BitAND(DllStructGetData($tTVHTI, 'Flags'), $TVHT_ONITEM) Then
                            _GUICtrlTreeView_SelectItem($hTreeView, $hItem)
                            If Not FileExists(_TVGetPath($hTV, $hItem, "\")) Then
                                _GUICtrlTreeView_Delete($hTV, $hItem)
                                $hItem = _GUICtrlTreeView_GetSelection($hTV)
                            Else
                                GUICtrlSendToDummy($Dummy2, $hItem)
                            EndIf
                            If $hItem <> $hSelect Then
                                _TVSetPath($hTV, $hItem, "\")
                            EndIf
                        EndIf
                EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY




; #FUNCTION# ====================================================================================================================
; Name...........: _FileIsPathValid
; Description ...: checks if a File/Directory Path is Valid
; Syntax.........: _FileIsPathValid($Path, $Verbose = 0)
; Source.........: http://www.autoitscript.com/forum/topic/113403-fileispathvalid-udf/
; Parameters ....: $Path - File/Directory Path to work with
; Return values .: Success - True
;                  Failure - False
; Author ........: Shafayat (sss13x@yahoo.com)
; Modified ......: gil900 - removed: $Verbose = 0
; Example .......: Yes
; ===============================================================================================================================
Func _FileIsPathValid($Path)
    Local $PathOri = $Path
    Local $Excluded[8] = [7, "\\", "?", "*", '"', "<", ">", "|"] ;List of invalid characters
    Local $Alphabet = StringSplit("ABCDEFGHIJKLMNOPQRSTUVWXYZ", "")
    Local $Reasons = ""
    Local $Valid = True
    ; Lenght Check
    If StringLen($Path) < 4 Then
        $Reasons = $Reasons & @CRLF & "LENGTH: Entire Pathname must be more than 3 characters (including drive)."
        $Valid = False
    EndIf
    ; Drive Check
    Local $pos = StringInStr($Path, ":\")
    If $pos <> 2 Then
        $Reasons = $Reasons & @CRLF & 'STRUCTURE: Drive letter must be one chars long and must be followed by ":\".'
        $Valid = False
    Else
        Local $chrdrv = StringUpper(StringLeft($Path, 1))
        Local $found = 0
        For $i = 0 To $Alphabet[0]
            If $chrdrv = $Alphabet[$i] Then
                $found = 1
            EndIf
        Next
        If $found = 0 Then
            $Reasons = $Reasons & @CRLF & "ILLEGAL CHARACTER: Illegal character used as drive letter."
            $Valid = False
        EndIf
    EndIf
    $Path = StringTrimLeft($Path, 3)
    ; Path + Name Check
    For $i = 0 To $Excluded[0]
        If StringInStr($Path, $Excluded[$i]) <> 0 Then
            $Reasons = $Reasons & @CRLF & "ILLEGAL CHARACTER: " & $Excluded[$i]
            $Valid = False
        EndIf
    Next
    Return $Valid
EndFunc   ;==>_FileIsPathValid

Improvements are welcome!

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