Jump to content

Make the FileOpenDialog only allow a selection of a specified file


Recommended Posts

Hey AutoIt'ers,

I'm trying to remove a GUI button of "Redefine" because I want to remove the possibility of the user selecting the wrong file whilst in the FileOpenDialog window.

My problem is this:

When I use the FileOpenDialog after defining the default name for the field and putting a filter of Example.exe and using "If FileExists ("Example.exe") Then", I can still select shortcuts/link.url files within the same folder (..if FileExists).

How can I make it so that the FOD box will not allow any other file besides Example.exe to be selected and if wrong, allow user to try again?

My Code:

Func selectfile()

$bfbc2config = @ScriptDir&"\BFBC2 - Tweaker Prefs.ini"

    $Define = _ExtMsgBox ( 32, "OK|Cancel", "|[< Writing Preferences File >]|", "'BFBC2 - Tweaker Prefs.ini' will be witten to the directory this program was launched from:"&@CRLF&"¬ "&@ScriptDir&""&@CRLF&@CRLF&"This file will store the tweaker's user-defined preferences. You will only have to do this once."&@CRLF&@CRLF&"Press OK to define your BFBC2Game.exe location.."&@CRLF&@CRLF&"NOTE:"&@CRLF&"For Steam users the .exe will be located at:"&@CRLF&" ¬ Steam\SteamApps\Common\Battlefield Bad Company 2"&@CRLF, "", $Main)
        If $Define = 1 Then

            $fod = FileOpenDialog ("|[< Choose File >]|", @ProgramFilesDir&"\Electronic arts\Battlefield Bad Company 2", "Locate: (BFBC2Game.exe)", 1, "BFBC2Game.exe", $Main )



            If FileExists ("BFBC2Game.exe") Then
                IniWrite ( $bfbc2config, "BFBC2 Launch", "Path", $fod )
                IniWrite ( $bfbc2config, "BFBC2 Tweaker", "Author", "Orkhayiq" )
                
                    $BFBC2Loc = IniRead ( $bfbc2config, "BFBC2 Launch", "Path", "" )
                        
                        $WorkingDIR_SPLIT = StringSplit ( $BFBC2Loc, "\BFBC2Game.exe", 1)
                        
                        $WorkingDIR = $WorkingDIR_SPLIT[1]
                        
                            Run($BFBC2Loc, $WorkingDIR)
                            
                            Sleep(200)
                    
                                ExitProgram()
                                
            ElseIf Not FileExists("BFBC2Game.exe") Then
                $NotSelected = _ExtMsgBox ( 48, "Retry|Nevermind", "|[< Wrong Selection >]|", "You didn't select BFBC2Game.exe", "", $Main )
                
                    If $NotSelected = 1 Then
                        selectfile()

                    Else
                    EndIf

            EndIf
            

        If @Error Then
            $NotSelected = _ExtMsgBox ( 48, "Ok", "|[< Error >]|", "The program encountered an error.", "", $Main )
                
                If $NotSelected = 1 Then
                EndIf
        
        EndIf

        If $Define = 2 Then
        EndIf
        
    EndIf

EndFunc

Also: How can you write an .ini to the same location of the compiled .exe? ..and what would I write for "$bfbc2config = ???" to always search for the .ini in the place of the running compiled exe?

TIA!

Edited by Orkhayiq
Link to comment
Share on other sites

How can I make it so that the FOD box will not allow any other file besides Example.exe to be selected and if wrong, allow user to try again?

FileOpenDialog("Select Example.exe", @ScriptDir, "Example (Example.exe)")

Did you even read the documentation :mellow:?

Ok so you did -- I was just actually reading your first post :( Anyway, when I use the above code I cannot, as you say, select links or anything else than Example.exe in the directory.

You do know FileExists and FileOpenDialog are not linked in any way right? I'm not sure why you would want to use $fod at all, as the location of that game is standard isn't it? Why let the user select it anyway? You already fill in the full path to the game folder, so just append BFBC2Game.exe :lol:

How can you write an .ini to the same location of the compiled .exe? ..and what would I write for "$bfbc2config = ???" to always search for the .ini in the place of the running compiled exe?

You can use @ScriptDir to identify the directory the script is running from. Edited by dani
Link to comment
Share on other sites

When I drag a shortcut link to a program from my desktop into the same folder as BFBC2Game.exe it is visible and selectable by the FOD which then writes it to the .ini if I 'accidentally' select it.(I'm just trying to make it as user-error free as possible. I don't want this accident to be possible.)

The location of the game is standard, for most gamers who'd install the game. Though people like me who use partitions install games to different drives than just C:\ This is why the option remains.

They aren't linked to each other? Hmm, It's only there to allow the error msgbox to appear if no file was selected. I don't know a better way.

You can use @ScriptDir to identify the directory the script is running from.

Thanks for the @ScriptDir command and the rest. I will look into it. :mellow:

Edited by Orkhayiq
Link to comment
Share on other sites

The FileOpenDialog still allows selection of .url and .ink (any shortcuts)..

It should only allow one file to be selected excluding shortcut links.

Is there a way to stop the user from being able to see shortcuts in the FileOpenDialog browser or just not allow them to be selected?

Also, is there another/better way to display my Error message of "You didn't select BFBC2Game.exe" without using the If command? It shows up when the user presses cancel rather than if they select the wrong file.

Link to comment
Share on other sites

  • 2 months later...

The FileOpenDialog still allows selection of .url and .ink (any shortcuts)..

It should only allow one file to be selected excluding shortcut links.

Is there a way to stop the user from being able to see shortcuts in the FileOpenDialog browser or just not allow them to be selected?

Also, is there another/better way to display my Error message of "You didn't select BFBC2Game.exe" without using the If command? It shows up when the user presses cancel rather than if they select the wrong file.

what are u making anyway?

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