Jump to content
Sign in to follow this  

Script working directory issue

Recommended Posts


ConsoleWrite("Original working directory: " & @CRLF & GetCurrentDirectory() & @CRLF & @CRLF)

FileSaveDialog("", "", "", 0, "")
ConsoleWrite("Working directory after FileSaveDialog: " & @CRLF & GetCurrentDirectory() & @CRLF & @CRLF)

FileOpenDialog("", "", "", 0, "")
ConsoleWrite("Working directory after FileOpenDialog: " & @CRLF & GetCurrentDirectory() & @CRLF & @CRLF)

FileSelectFolder("", "", 0, "")
ConsoleWrite("Working directory after FileSelectFolder (It's just OK): " & @CRLF & GetCurrentDirectory() & @CRLF)

Func GetCurrentDirectory()
    Local $avResult

    $avResult = DllCall("kernel32.dll", "DWORD", "GetCurrentDirectory", _
            "DWORD", 0, _
            "ptr", 0)
    If ((@error) Or ($avResult = 0)) Then Return SetError(1, 0, "")

    Local $tBuffer = DllStructCreate("CHAR[" & $avResult[0] & "]")

    $avResult = DllCall("kernel32.dll", "DWORD", "GetCurrentDirectory", _
            "DWORD", $avResult[0], _
            "ptr", DllStructGetPtr($tBuffer, 1))
    If ((@error) Or ($avResult = 0)) Then Return SetError(2, 0, "")

    Return SetError(0, 0, DllStructGetData($tBuffer, 1))

The above code demonstrates the situation.

In the beginning the working directory is just fine.

Then after FileSaveDialog it gets changed to the selected directory, also after FileOpenDialog it gets changed to the new selected dir.

However FileSelectFolder is just fine and doesn't change the working directory.

I guess it's that kind of bugs which you will not get noticed about but will ruin every single line of code which you write based on script's relative path.

Edited by D4RKON3

Share this post

Link to post
Share on other sites


It is a Windows "feature" and not an AutoIt "bug" - and as they are all direct API calls there is not a lot that can be done about it. :(

The fact that @WorkingDir changes on a successful return is mentioned in the Help file for FileOpen/SaveDialog. ;)


Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:


ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area


Share this post

Link to post
Share on other sites

Here's my work-around I use in AMT...

Func _FileOpenDialog_Ex($sTitle, $sInitDir, $sFilter, $iOptions = 0, $sDefaultName = "", $hWnd = 0)
        The problem is that GetOpenFileName changes the current directory to the
        last browsed one. The current directory and any of its parents cannot be
    Local $sWorkingDir = @WorkingDir
    Local $sFilename = FileOpenDialog($sTitle, $sInitDir, $sFilter, $iOptions, $sDefaultName, $hWnd)
    Local $iError = @error
    Return SetError($iError, 0, $sFilename)
EndFunc   ;==>_FileOpenDialog_Ex

Share this post

Link to post
Share on other sites


Yes, you are right, I didn't checked that part of help file.


Thanks, you gave me the idea.

Share this post

Link to post
Share on other sites

Nice workaround KaFu.. I wasn't aware that the issue was documented, and have only seen it crop up intermittently.

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