How to retrieve full path of currently opened folder?

Get help with using AutoHotkey and its commands and hotkeys
User avatar
Sabestian Caine
Posts: 467
Joined: 12 Apr 2015, 03:53

How to retrieve full path of currently opened folder?

04 Aug 2020, 12:09

Hello,

I want to retrieve full path of currently opened folder and for this i tried these codes-

Code: Select all

  ControlGetText, o, ToolbarWindow322, a
StringTrimLeft, o, o, 9
MsgBox % o
It works fine when path is something like this-
04_08_20 @10_35_42.PNG
04_08_20 @10_35_42.PNG (79.73 KiB) Viewed 6153 times
But, It doesn't work when path is something like this-
04_08_20 @10_37_42.PNG
04_08_20 @10_37_42.PNG (84.78 KiB) Viewed 6153 times
Please tell me how to get full path of any currently opened folder?

Thanks a lot..
I don't normally code as I don't code normally.
YOU do what YOU want, but happens what HE wants.
YOU do what HE wants, will happen what YOU want.
teadrinker
Posts: 1785
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to retrieve full path of currently opened folder?

04 Aug 2020, 13:31

Hi
Try this:

Code: Select all

$F10:: MsgBox % Explorer_GetActiveFolderPath()

Explorer_GetActiveFolderPath() {
   WinGetClass, winClass, % "ahk_id" . hWnd := WinExist("A")
   if !(winClass ~="Progman|WorkerW|(Cabinet|Explore)WClass")
      Return
   
   shellWindows := ComObjCreate("Shell.Application").Windows
   if (winClass ~= "Progman|WorkerW")  ; IShellWindows::Item:    https://goo.gl/ihW9Gm
                                       ; IShellFolderViewDual:   https://goo.gl/gnntq3
      shellFolderView := shellWindows.Item( ComObject(VT_UI4 := 0x13, SWC_DESKTOP := 0x8) ).Document
   else {
      for window in shellWindows       ; ShellFolderView object: https://goo.gl/MhcinH
         if (hWnd = window.HWND) && (shellFolderView := window.Document)
            break
   }
   Return shellFolderView.Folder.Self.Path
}
BoBo
Posts: 3631
Joined: 13 May 2014, 17:15

Re: How to retrieve full path of currently opened folder?

04 Aug 2020, 13:38

You can use the "copy as path"-context menu option. Unless teadrinker shows up again ... :lol:

Code: Select all

!c::    ; press Alt+c once you've selected a file/folder within Windows Explorer
    Send % {Shift}{RButton} p    ; 'p' is valid for German language settings. Change accordingly.
    MsgBox % ClipBoard
    Return
teadrinker
Posts: 1785
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to retrieve full path of currently opened folder?

04 Aug 2020, 13:46

@BoBo, I'm afraid you misunderstand the task. :)
User avatar
Sabestian Caine
Posts: 467
Joined: 12 Apr 2015, 03:53

Re: How to retrieve full path of currently opened folder?

05 Aug 2020, 11:19

teadrinker wrote:
04 Aug 2020, 13:31
Hi
Try this:

Code: Select all

$F10:: MsgBox % Explorer_GetActiveFolderPath()

Explorer_GetActiveFolderPath() {
   WinGetClass, winClass, % "ahk_id" . hWnd := WinExist("A")
   if !(winClass ~="Progman|WorkerW|(Cabinet|Explore)WClass")
      Return
   
   shellWindows := ComObjCreate("Shell.Application").Windows
   if (winClass ~= "Progman|WorkerW")  ; IShellWindows::Item:    https://goo.gl/ihW9Gm
                                       ; IShellFolderViewDual:   https://goo.gl/gnntq3
      shellFolderView := shellWindows.Item( ComObject(VT_UI4 := 0x13, SWC_DESKTOP := 0x8) ).Document
   else {
      for window in shellWindows       ; ShellFolderView object: https://goo.gl/MhcinH
         if (hWnd = window.HWND) && (shellFolderView := window.Document)
            break
   }
   Return shellFolderView.Folder.Self.Path
}
Thanks dear teadrinker for your good codes.. but when i run these codes they show me like this-
05_08_20 @9_46_48.PNG
05_08_20 @9_46_48.PNG (81.62 KiB) Viewed 4908 times
Which, I think, is not the full path of Document's folder. The full path of documents folder is C:\Users\htc\Documents Please help in this regard... Thanks a lot...
I don't normally code as I don't code normally.
YOU do what YOU want, but happens what HE wants.
YOU do what HE wants, will happen what YOU want.
teadrinker
Posts: 1785
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to retrieve full path of currently opened folder?

05 Aug 2020, 12:13

It's another representation of the same folder. ::{031E4825-7B94-4DC3-B131-E946B44C8DD5}\Documents.library-ms is the valid path, you can see this:

Code: Select all

Run ::{031E4825-7B94-4DC3-B131-E946B44C8DD5}\Documents.library-ms
User avatar
Sabestian Caine
Posts: 467
Joined: 12 Apr 2015, 03:53

Re: How to retrieve full path of currently opened folder?

08 Aug 2020, 11:51

teadrinker wrote:
05 Aug 2020, 12:13
It's another representation of the same folder. ::{031E4825-7B94-4DC3-B131-E946B44C8DD5}\Documents.library-ms is the valid path, you can see this:

Code: Select all

Run ::{031E4825-7B94-4DC3-B131-E946B44C8DD5}\Documents.library-ms
Thanks dear teadrinker... your codes are successfully running library folder when i use this

Code: Select all

Run ::{031E4825-7B94-4DC3-B131-E946B44C8DD5}\Documents.library-ms
but, I want to calculate the size of library folder then this path is not working.

Please consider another example-
Suppose, I want to get the size of Documents's folder then I am trying to use these codes-

Code: Select all

 msgbox % Clipboard:= Explorer_GetActiveFolderPath()
  
  SetBatchLines, -1  ; Make the operation run at maximum speed.
FolderSize = 0
Loop, %Clipboard%\*.*, , 1
    FolderSize += %A_LoopFileSize%
MsgBox Size is %FolderSize% bytes.


Explorer_GetActiveFolderPath() {
   WinGetClass, winClass, % "ahk_id" . hWnd := WinExist("A")
   if !(winClass ~="Progman|WorkerW|(Cabinet|Explore)WClass")
      Return
   
   shellWindows := ComObjCreate("Shell.Application").Windows
   if (winClass ~= "Progman|WorkerW")  ; IShellWindows::Item:    https://goo.gl/ihW9Gm
                                       ; IShellFolderViewDual:   https://goo.gl/gnntq3
      shellFolderView := shellWindows.Item( ComObject(VT_UI4 := 0x13, SWC_DESKTOP := 0x8) ).Document
   else {
      for window in shellWindows       ; ShellFolderView object: https://goo.gl/MhcinH
         if (hWnd = window.HWND) && (shellFolderView := window.Document)
            break
   }
   Return shellFolderView.Folder.Self.Path
 }
now the path which is retrieved by your codes does not work in calculating the size of Documents's folder.

You can see in this image what is retrieved by your codes-
08_08_20 @10_15_12.PNG
08_08_20 @10_15_12.PNG (97.54 KiB) Viewed 4337 times
08_08_20 @10_16_14.PNG
08_08_20 @10_16_14.PNG (78.05 KiB) Viewed 4337 times


while, when I manually put the full path of Document's folder which is C:\Users\htc\Documents then it correctly shows the size of document's folder.

Please solve this problem sir..

Thanks a lot...
I don't normally code as I don't code normally.
YOU do what YOU want, but happens what HE wants.
YOU do what HE wants, will happen what YOU want.
User avatar
boiler
Posts: 5909
Joined: 21 Dec 2014, 02:44

Re: How to retrieve full path of currently opened folder?

08 Aug 2020, 12:43

Do not put % signs around the variable in FolderSize += %A_LoopFileSize%. See When to use percents.

By the way, this will not give you the total size of files in the folder in cases where you have sub-folders inside the folder which also contain files. You would need to recurse into sub-folders to get the total size.
User avatar
Sabestian Caine
Posts: 467
Joined: 12 Apr 2015, 03:53

Re: How to retrieve full path of currently opened folder?

08 Aug 2020, 13:30

boiler wrote:
08 Aug 2020, 12:43
Do not put % signs around the variable in FolderSize += %A_LoopFileSize%. See When to use percents.

By the way, this will not give you the total size of files in the folder in cases where you have sub-folders inside the folder which also contain files. You would need to recurse into sub-folders to get the total size.
Thanks boiler for your kind reply but this is not the problem of % signs as i directly borrowed the above line form ahk documentation, Please see this-
08_08_20 @11_54_35.PNG
08_08_20 @11_54_35.PNG (28 KiB) Viewed 3904 times
This is the problem of correct path of respective folder. In this regard, i have already mentioned that when i use full path of document's folder which is C:\Users\htc\Documents then the above codes works fine, but when i use this path ::{031E4825-7B94-4DC3-B131-E946B44C8DD5}\Documents.library-ms then it does not show the size of the folder. Please solve this issue.
Thanks..
I don't normally code as I don't code normally.
YOU do what YOU want, but happens what HE wants.
YOU do what HE wants, will happen what YOU want.
User avatar
boiler
Posts: 5909
Joined: 21 Dec 2014, 02:44

Re: How to retrieve full path of currently opened folder?

08 Aug 2020, 13:41

OK. Sorry. I'm surprised that works. The fact that it does work is disconcerting. I was also wrong in that you did include the option for recursing folders. :oops:
teadrinker
Posts: 1785
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to retrieve full path of currently opened folder?

08 Aug 2020, 15:56

If such folder contains any items, you could try this workaround:

Code: Select all

$F10:: MsgBox % Explorer_GetActiveFolderPath()

Explorer_GetActiveFolderPath() {
   WinGetClass, winClass, % "ahk_id" . hWnd := WinExist("A")
   if !(winClass ~="Progman|WorkerW|(Cabinet|Explore)WClass")
      Return
   
   shellWindows := ComObjCreate("Shell.Application").Windows
   if (winClass ~= "Progman|WorkerW")  ; IShellWindows::Item:    https://goo.gl/ihW9Gm
                                       ; IShellFolderViewDual:   https://goo.gl/gnntq3
      shellFolderView := shellWindows.Item( ComObject(VT_UI4 := 0x13, SWC_DESKTOP := 0x8) ).Document
   else {
      for window in shellWindows       ; ShellFolderView object: https://goo.gl/MhcinH
         if (hWnd = window.HWND) && (shellFolderView := window.Document)
            break
   }
   folderPath := shellFolderView.Folder.Self.Path
   if RegExMatch(folderPath, "i)\.library-ms$") {
      for item in shellFolderView.Folder.Items {
         itemPath := item.Path
         break
      }
      SplitPath, itemPath,, dir
      (dir && folderPath := dir)
   }
   Return folderPath
}
teadrinker
Posts: 1785
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to retrieve full path of currently opened folder?

08 Aug 2020, 16:25

No, the post above is wrong. Folders with addresses ending with ".library-ms" are not actually folders. They are really files called "libraries". So, trying to get the size of a "folder" like this doesn't make sense. The Documents library ::{031E4825-7B94-4DC3-B131-E946B44C8DD5}\Documents.library-ms is not the same as "My Documents" folder. A library can contain item references from different folders.
User avatar
Sabestian Caine
Posts: 467
Joined: 12 Apr 2015, 03:53

Re: How to retrieve full path of currently opened folder?

09 Aug 2020, 08:57

teadrinker wrote:
08 Aug 2020, 15:56
If such folder contains any items, you could try this workaround:

Code: Select all

$F10:: MsgBox % Explorer_GetActiveFolderPath()

Explorer_GetActiveFolderPath() {
   WinGetClass, winClass, % "ahk_id" . hWnd := WinExist("A")
   if !(winClass ~="Progman|WorkerW|(Cabinet|Explore)WClass")
      Return
   
   shellWindows := ComObjCreate("Shell.Application").Windows
   if (winClass ~= "Progman|WorkerW")  ; IShellWindows::Item:    https://goo.gl/ihW9Gm
                                       ; IShellFolderViewDual:   https://goo.gl/gnntq3
      shellFolderView := shellWindows.Item( ComObject(VT_UI4 := 0x13, SWC_DESKTOP := 0x8) ).Document
   else {
      for window in shellWindows       ; ShellFolderView object: https://goo.gl/MhcinH
         if (hWnd = window.HWND) && (shellFolderView := window.Document)
            break
   }
   folderPath := shellFolderView.Folder.Self.Path
   if RegExMatch(folderPath, "i)\.library-ms$") {
      for item in shellFolderView.Folder.Items {
         itemPath := item.Path
         break
      }
      SplitPath, itemPath,, dir
      (dir && folderPath := dir)
   }
   Return folderPath
}
Thanks you very much dear teadrinker. Everything is working great except one thing.
When i connect my mobile phone to PC and open its internal storage or external storage folder then it does not show its full path.
please look this-
09_08_20 @7_25_26.PNG
09_08_20 @7_25_26.PNG (94.54 KiB) Viewed 1604 times
Please tell me is there any solution to this problem?

Thanks a lot...

Kindly see this related topic also-
https://www.autohotkey.com/boards/viewtopic.php?f=17&t=79653
I don't normally code as I don't code normally.
YOU do what YOU want, but happens what HE wants.
YOU do what HE wants, will happen what YOU want.
teadrinker
Posts: 1785
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to retrieve full path of currently opened folder?

09 Aug 2020, 16:06

It is the full path, for me my script shows the same:
 
 Image

To launch such path you need to escape all the commas via accent like this `,`,.
User avatar
Sabestian Caine
Posts: 467
Joined: 12 Apr 2015, 03:53

Re: How to retrieve full path of currently opened folder?

12 Aug 2020, 12:26

teadrinker wrote:
09 Aug 2020, 16:06
It is the full path, for me my script shows the same:
 
 Image

To launch such path you need to escape all the commas via accent like this `,`,.
Dear teadrinker you really helped me a lot.. Please tell me one last thing-
If I want to copy few files from my mobile phone to PC using FileCopy command, then would this path work?
I mean in that case these codes does not work as there is not complete/uniform source path.
please see these codes-

Code: Select all

FileCopy, ::{20D04FE0-3AEA-1069-A2D8-08002B30309D}\\\?\usb#vid_22d9&pid_2764#zlmnp74dnjoj8tfe#{6ac27878-a6fa-4155-ba85-f98f491d4f33}\SID-{10001,,53529321472}\{420EA89F-0000-0000-0000-000000000000}\{E41123DC-FFFF-FFFF-0000-000000000000}, C:\My Folder
Please provide solution to this problem.
Thanks a lot..
I don't normally code as I don't code normally.
YOU do what YOU want, but happens what HE wants.
YOU do what HE wants, will happen what YOU want.
teadrinker
Posts: 1785
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to retrieve full path of currently opened folder?

12 Aug 2020, 14:28

Try this:

Code: Select all

CopyFile("::{20D04FE0-3AEA-1069-A2D8-08002B30309D}\\\?\usb#vid_22d9&pid_2764#zlmnp74dnjoj8tfe#{6ac27878-a6fa-4155-ba85-f98f491d4f33}\SID-{10001,,53529321472}\{420EA89F-0000-0000-0000-000000000000}\{E41123DC-FFFF-FFFF-0000-000000000000}", "C:\My Folder")

CopyFile(filePath, destDir) {
   RegExMatch(filePath, "(?<sourceDir>.+)\\", _)
   
   Shell := ComObjCreate("Shell.Application")
   SourceFolder := Shell.Namespace(_sourceDir)
   if !IsObject(SourceFolder)
      throw "Source directory not found"
   
   if !InStr(FileExist(destDir), "D") {
      FileCreateDir, % destDir
      if ErrorLevel
         throw "Can't create destination folder"
   }
   for Item in SourceFolder.Items {
      if (Item.Path = filePath) {
         Shell.Namespace(destDir).CopyHere(Item)
         break
      }
   }
}
User avatar
Sabestian Caine
Posts: 467
Joined: 12 Apr 2015, 03:53

Re: How to retrieve full path of currently opened folder?

13 Aug 2020, 11:26

teadrinker wrote:
12 Aug 2020, 14:28
Try this:

Code: Select all

CopyFile("::{20D04FE0-3AEA-1069-A2D8-08002B30309D}\\\?\usb#vid_22d9&pid_2764#zlmnp74dnjoj8tfe#{6ac27878-a6fa-4155-ba85-f98f491d4f33}\SID-{10001,,53529321472}\{420EA89F-0000-0000-0000-000000000000}\{E41123DC-FFFF-FFFF-0000-000000000000}", "C:\My Folder")

CopyFile(filePath, destDir) {
   RegExMatch(filePath, "(?<sourceDir>.+)\\", _)
   
   Shell := ComObjCreate("Shell.Application")
   SourceFolder := Shell.Namespace(_sourceDir)
   if !IsObject(SourceFolder)
      throw "Source directory not found"
   
   if !InStr(FileExist(destDir), "D") {
      FileCreateDir, % destDir
      if ErrorLevel
         throw "Can't create destination folder"
   }
   for Item in SourceFolder.Items {
      if (Item.Path = filePath) {
         Shell.Namespace(destDir).CopyHere(Item)
         break
      }
   }
}
Thanks dear teadrinker... you are really awesome at coding... :clap:

Sir, in fact I was tying to make a script by that I can see the size of all folders and files in listview. And for this I made this script-

Code: Select all

  
   MsgBox % o:= Clipboard:= Explorer_GetActiveFolderPath()

  Gui, +resize
Gui,Font, s10, Arial
SetBatchLines, -1  ; Make the operation run at maximum speed.
Gui, Add, ListView, Grid Sort r30 w1000, Name|KB|MB|GB|
  
Loop, Files, %o%\*.*, D
{
	SplitPath, A_LoopFileFullPath, Folder
	FolderSize := 0
	Loop, Files, %A_LoopFileFullPath%\*.*, R
	    FolderSize += A_LoopFileSize
	LV_Add("", Folder, Round(FolderSize / 1024), Round(FolderSize / 1024 / 1024), Round(FolderSize / 1024 / 1024 / 1024)) 
}

Loop, %o%\*.*
{
    LV_Add("", A_LoopFileName, A_LoopFileSizeKB, A_LoopFileSizeMB, A_LoopFileSizeKB/1024/1024)

}
LV_ModifyCol()
LV_ModifyCol(2, "Integer")
LV_ModifyCol(2, "SortDesc")
Gui, Show
return

GuiClose:
gui, destroy

; codes by teadrinker working excellent to retrieve currently opened folder's path***************

Explorer_GetActiveFolderPath() {
   WinGetClass, winClass, % "ahk_id" . hWnd := WinExist("A")
   if !(winClass ~="Progman|WorkerW|(Cabinet|Explore)WClass")
      Return
   
   shellWindows := ComObjCreate("Shell.Application").Windows
   if (winClass ~= "Progman|WorkerW")  ; IShellWindows::Item:    https://goo.gl/ihW9Gm
                                       ; IShellFolderViewDual:   https://goo.gl/gnntq3
      shellFolderView := shellWindows.Item( ComObject(VT_UI4 := 0x13, SWC_DESKTOP := 0x8) ).Document
   else {
      for window in shellWindows       ; ShellFolderView object: https://goo.gl/MhcinH
         if (hWnd = window.HWND) && (shellFolderView := window.Document)
            break
   }
   folderPath := shellFolderView.Folder.Self.Path
   if RegExMatch(folderPath, "i)\.library-ms$") {
      for item in shellFolderView.Folder.Items {
         itemPath := item.Path
         break
      }
      SplitPath, itemPath,, dir
      (dir && folderPath := dir)
   }
   Return folderPath
}
It shows the size of all folders and files in descending order like this- (This is my drive D:\)
13_08_20 @10_04_35.PNG
13_08_20 @10_04_35.PNG (95.75 KiB) Viewed 669 times

This script is working quite fine. But, the problem is that When i attach my mobile to the computer and open mobile's folder to see the size of all files and folders then this script does not work. When i attach my mobile to computer then it shows like this-
ram5.gif
ram5.gif (115.96 KiB) Viewed 694 times


This is quite certain that there is the problem of path which is not in uniform format. I know there must be some other way to get the size of folders and files of my mobile in ListView. Could you please tell me any way to see all files and folders size of my mobile in ListView using some script?

Please help

Thans a lot..
I don't normally code as I don't code normally.
YOU do what YOU want, but happens what HE wants.
YOU do what HE wants, will happen what YOU want.
teadrinker
Posts: 1785
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to retrieve full path of currently opened folder?

19 Aug 2020, 12:49

Sabestian Caine wrote: Could you please tell me any way to see all files and folders size of my mobile
Sorry for late answer, haven't found a way.
malcev
Posts: 648
Joined: 12 Aug 2014, 12:37

Re: How to retrieve full path of currently opened folder?

19 Aug 2020, 23:11

You can count like this: (folders size You have to recurse by Yourself)

Code: Select all

for window in shellWindows       ; ShellFolderView object: https://goo.gl/MhcinH
         if (hWnd = window.HWND) && (shellFolderView := window.Document)
            break
   }

loop
{
   if (A_Index = 1)
   {
      folder := shellFolderView.Folder
      folderPath := folder.Title
   }
   folder := folder.parentfolder
   if (folder = "")
   {
      folderPath := RegexReplace(folderPath, "^.+?\\")
      break
   }
   folderPath := folder.Title "\" folderPath
}
msgbox % folderPath
for item in shellFolderView.Folder.Items
   msgbox % shellFolderView.Folder.GetDetailsOf(item, 0) "`n" shellFolderView.Folder.GetDetailsOf(item, 2)
teadrinker
Posts: 1785
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to retrieve full path of currently opened folder?

20 Aug 2020, 04:52

@malcev
GetDetailsOf(item, 2) works, but doesn't show size in bytes. Also, this behavior is undocumented:
Folder.GetDetailsOf wrote:(0)

Retrieves the name of the item.

(1)

Retrieves the size of the item.

(2)

Retrieves the type of the item.

Return to “Ask For Help”

Who is online

Users browsing this forum: mikeyww, PlumpSamurai and 49 guests