Jump to content

Script working directory issue


FaridAgl
 Share

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))
EndFunc

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
Link to comment
Share on other sites

  • Moderators

D4RKON3,

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

M23

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:

Spoiler

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

 

Link to comment
Share on other sites

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

Func _FileOpenDialog_Ex($sTitle, $sInitDir, $sFilter, $iOptions = 0, $sDefaultName = "", $hWnd = 0)
    #cs
        https://groups.google.com/forum/?hl=en&fromgroups=#!topic/microsoft.public.vc.mfc/HafQr4gIRY0
        The problem is that GetOpenFileName changes the current directory to the
        last browsed one. The current directory and any of its parents cannot be
        deleted.
    #ce
    Local $sWorkingDir = @WorkingDir
    Local $sFilename = FileOpenDialog($sTitle, $sInitDir, $sFilter, $iOptions, $sDefaultName, $hWnd)
    Local $iError = @error
    FileChangeDir($sWorkingDir)
    Return SetError($iError, 0, $sFilename)
EndFunc   ;==>_FileOpenDialog_Ex
Link to comment
Share on other sites

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

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