Jump to content

How to get currently opened folder's path


Recommended Posts

Hi all,

When my program starts, i need to get the currently opened folder path in windows explorer. How to get the path. Thanks in advance. :)

Spoiler

My Contributions

Glance GUI Library - A gui library based on Windows api functions. Written in Nim programming language.

UDF Link Viewer   --- A tool to visit the links of some most important UDFs 

 Includer_2  ----- A tool to type the #include statement automatically 

 Digits To Date  ----- date from 3 integer values

PrintList ----- prints arrays into console for testing.

 Alert  ------ An alternative for MsgBox 

 MousePosition ------- A simple tooltip display of mouse position

GRM Helper -------- A littile tool to help writing code with GUIRegisterMsg function

Access_UDF  -------- An UDF for working with access database files. (.*accdb only)

 

Link to comment
Share on other sites

@faustf

Thanks for the reply, but no. @ScriptDir is not for my case. I will compile this program as an exe and when i run this program, what ever folder is open in explorer, i need to get it's path. Not the script directory. AFAIK, script directory is where the script/exe file resides. That's not what i want. :)

Spoiler

My Contributions

Glance GUI Library - A gui library based on Windows api functions. Written in Nim programming language.

UDF Link Viewer   --- A tool to visit the links of some most important UDFs 

 Includer_2  ----- A tool to type the #include statement automatically 

 Digits To Date  ----- date from 3 integer values

PrintList ----- prints arrays into console for testing.

 Alert  ------ An alternative for MsgBox 

 MousePosition ------- A simple tooltip display of mouse position

GRM Helper -------- A littile tool to help writing code with GUIRegisterMsg function

Access_UDF  -------- An UDF for working with access database files. (.*accdb only)

 

Link to comment
Share on other sites

1 hour ago, kcvinu said:

When my program starts, i need to get the currently opened folder path in windows explorer.

It is already an older thread, but maybe it is what you are looking for  : windows-explorer-current-folder

Since links get lost sometimes, here for the sake of convenience the related code in a spoiler.

Spoiler
#include <Array.au3>

Local $oErrorHandler = ObjEvent("AutoIt.Error", "_ComErrFunc")

$aWinList=WinList("[REGEXPCLASS:^(Explore|Cabinet)WClass$]")

For $i = 1 To $aWinList[0][0]
    ; Get base folder and selections
    $aSelection = _ExplorerWinGetSelectedItems($aWinList[$i][1])
    ; Display as strings
    ConsoleWrite("Explorer Window: " & $aSelection[1] & " Selection(s)" & @CRLF)
    For $j = 2 To $aSelection[0] + 1
        ConsoleWrite(@TAB & $aSelection[$j] & @CRLF)
    Next
    ; Display as array
    _ArrayDisplay($aSelection, "Explorer Instance #" & $i & " / " & $aWinList[$i][0])
Next

; ==========================================================================================================================

; Func _ObjectSHFolderViewFromWin($hWnd)
;
; Returns an 'ShellFolderView' Object for the given Window handle
;
; Author: Ascend4nt, based on code by KaFu, klaus.s
; ==========================================================================================================================

Func _ObjectSHFolderViewFromWin($hWnd)
    If Not IsHWnd($hWnd) Then Return SetError(1,0,0)
    Local $oShell,$oShellWindows,$oIEObject,$oSHFolderView

    ; Shell Object
    $oShell=ObjCreate("Shell.Application")
    If Not IsObj($oShell) Then Return SetError(2,0,0)

;   Get a 'ShellWindows Collection' object
    $oShellWindows = $oShell.Windows()
    If Not IsObj($oShellWindows) Then Return SetError(3,0,0)

;   Iterate through the collection - each of type 'InternetExplorer' Object

    For $oIEObject In $oShellWindows
        If $oIEObject.HWND = $hWnd Then
            ; InternetExplorer->Document = ShellFolderView object
            $oSHFolderView=$oIEObject.Document
            If IsObj($oSHFolderView) Then Return $oSHFolderView
            Return SetError(4,0,0)
        EndIf
    Next

    Return SetError(-1,0,0)
EndFunc

; ==========================================================================================================================
; Func _ExplorerWinGetSelectedItems($hWnd)
;
;
; Author: klaus.s, KaFu, Ascend4nt (consolidation & cleanup, Path name simplification)
; ==========================================================================================================================

Func _ExplorerWinGetSelectedItems($hWnd)
    If Not IsHWnd($hWnd) Then Return SetError(1,0,'')
    Local $oSHFolderView
    Local $iSelectedItems,$iCounter=2,$aSelectedItems[2] = [0, ""]

    $oSHFolderView=_ObjectSHFolderViewFromWin($hWnd)
    If @error Then Return SetError(@error,0,'')

;   SelectedItems = FolderItems Collection object->Count
    $iSelectedItems = $oSHFolderView.SelectedItems.Count

    Dim $aSelectedItems[$iSelectedItems+2]  ; 2 extra -> 1 for count [0], 1 for Folder Path [1]

    $aSelectedItems[0]=$iSelectedItems
;   ShellFolderView->Folder->Self as 'FolderItem'->Path
    $aSelectedItems[1]=$oSHFolderView.Folder.Self.Path

;   ShellFolderView->SelectedItems = FolderItems Collection object
    $oSelectedFolderItems = $oSHFolderView.SelectedItems

#cs
    ; For ALL items in an Explorer window (not just the selected ones):
    $oSelectedFolderItems = $oSHFolderView.Folder.Items
    ReDim $aSelectedItems[$oSelectedFolderItems.Count+2]
#ce

    For $oFolderItem In $oSelectedFolderItems
        $aSelectedItems[$iCounter] = $oFolderItem.Path
        $iCounter += 1
    Next

    Return SetExtended($iCounter-2,$aSelectedItems)
EndFunc   ;==>_ExplorerWinGetSelectedItems

Func _ComErrFunc($oError)
    ConsoleWrite("COM Error occurred:"  & @CRLF & _
        "Number: " & @TAB & $oError.number & @CRLF & _
        "Windescription:" & @TAB & $oError.windescription & @CRLF & _
        "Description is: " & @TAB & $oError.description & @CRLF & _
        "Source is: " & @TAB & $oError.source & @CRLF & _
        "Helpfile is: " & @TAB & $oError.helpfile & @CRLF & _
        "Helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _
        "Lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _
        "Scriptline is: " & @TAB & $oError.scriptline & @CRLF & _
        "Retcode is: " & @TAB & $oError.retcode & @CRLF & @CRLF)
EndFunc   ;==>_ComErrFunc

 

 

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

What if you have more than one folder open? What folder considered the one that you need?

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

@Musashi,

That Function will loop through all opened explorer windows. I just need only the last used/opened window. 

Anyways, it seems that things are little bit different than i expected. I thought that the folder path would be a simple property of Shell object.(like $Shell.ActiveFolder.Path). But this Function shows that it is not. Thanks for pointing me the right direction.

 

@MrCreatoR,

If there is more than one folder opened, then i need the last opened folder's address.

Spoiler

My Contributions

Glance GUI Library - A gui library based on Windows api functions. Written in Nim programming language.

UDF Link Viewer   --- A tool to visit the links of some most important UDFs 

 Includer_2  ----- A tool to type the #include statement automatically 

 Digits To Date  ----- date from 3 integer values

PrintList ----- prints arrays into console for testing.

 Alert  ------ An alternative for MsgBox 

 MousePosition ------- A simple tooltip display of mouse position

GRM Helper -------- A littile tool to help writing code with GUIRegisterMsg function

Access_UDF  -------- An UDF for working with access database files. (.*accdb only)

 

Link to comment
Share on other sites

@argumentum,

Thanks a lot. It will give the active explorer window's title. Now, i can get the hWnd of active explorer window and tweak Melba's KaFu's & Ascend4nt's solution which @Musashi pointed.

Edited by kcvinu
Spoiler

My Contributions

Glance GUI Library - A gui library based on Windows api functions. Written in Nim programming language.

UDF Link Viewer   --- A tool to visit the links of some most important UDFs 

 Includer_2  ----- A tool to type the #include statement automatically 

 Digits To Date  ----- date from 3 integer values

PrintList ----- prints arrays into console for testing.

 Alert  ------ An alternative for MsgBox 

 MousePosition ------- A simple tooltip display of mouse position

GRM Helper -------- A littile tool to help writing code with GUIRegisterMsg function

Access_UDF  -------- An UDF for working with access database files. (.*accdb only)

 

Link to comment
Share on other sites

@argumentum, It seems that, i can achieve my goal only with your single line code if i turn on "Display full path in title bar" option in folder options. :)

Spoiler

My Contributions

Glance GUI Library - A gui library based on Windows api functions. Written in Nim programming language.

UDF Link Viewer   --- A tool to visit the links of some most important UDFs 

 Includer_2  ----- A tool to type the #include statement automatically 

 Digits To Date  ----- date from 3 integer values

PrintList ----- prints arrays into console for testing.

 Alert  ------ An alternative for MsgBox 

 MousePosition ------- A simple tooltip display of mouse position

GRM Helper -------- A littile tool to help writing code with GUIRegisterMsg function

Access_UDF  -------- An UDF for working with access database files. (.*accdb only)

 

Link to comment
Share on other sites

1 hour ago, KaFu said:

Not Melbas solution (this time🤣).

No, surprisingly not :lol:.

Normally I always name the author(s) when mentioning sources, so that acknowledgements go to the right address. Since they are listed in the function headers, I assumed that would be sufficient. For reasons of politeness among programmers, here is the corresponding addendum :

Author : @Ascend4nt , based on code by @KaFu , klaus.s

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

@KaFu

Oops Sorry, my mistake. I only see that the code was posted by Melba. I didn't read the comments in code. My bad. I will edit my reply. Sorry for the mistake.

Spoiler

My Contributions

Glance GUI Library - A gui library based on Windows api functions. Written in Nim programming language.

UDF Link Viewer   --- A tool to visit the links of some most important UDFs 

 Includer_2  ----- A tool to type the #include statement automatically 

 Digits To Date  ----- date from 3 integer values

PrintList ----- prints arrays into console for testing.

 Alert  ------ An alternative for MsgBox 

 MousePosition ------- A simple tooltip display of mouse position

GRM Helper -------- A littile tool to help writing code with GUIRegisterMsg function

Access_UDF  -------- An UDF for working with access database files. (.*accdb only)

 

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