script to count number of items in a folder view not working

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Gio710
Posts: 31
Joined: 01 Mar 2022, 06:34

script to count number of items in a folder view not working

Post by Gio710 » 01 Dec 2022, 06:48

Hi,

I'm trying to get the number of items in a folder : FolderPath

without a count loop. I poked around with this :

Code: Select all

numberitem := ComObjCreate("Shell.Application").NameSpace(FolderPath).Items.Count
it sents me a number but it's not the one I?m looking for. ANy idea How I could do that ?

User avatar
mikeyww
Posts: 26856
Joined: 09 Sep 2014, 18:38

Re: script to count number of items in a folder view not working

Post by mikeyww » 01 Dec 2022, 07:32

Code: Select all

#IfWinExist ahk_class CabinetWClass
F3::MsgBox, 64, Number of items, % nItems()
#IfWinExist

nItems(hwnd := 0) { ; Adapted from https://www.autohotkey.com/boards/viewtopic.php?p=387113#p387113
 If hWnd
  explorerHwnd := WinExist("ahk_class CabinetWClass ahk_id " hwnd)
 Else (!explorerHwnd := WinActive("ahk_class CabinetWClass"))
     && explorerHwnd := WinExist( "ahk_class CabinetWClass")
 If explorerHwnd
  For window in ComObjCreate("Shell.Application").Windows
   Try If (window && window.hwnd && window.hwnd = explorerHwnd)
    Return window.Document.Folder.Items.Count
 Return False
}
This did work here:

Code: Select all

MsgBox, % ComObjCreate("Shell.Application").NameSpace(A_Desktop).Items.Count
it's not the one I?m looking for
Since no one knows your folder or even what number you want, the answer to your question is undefined.

User avatar
mikeyww
Posts: 26856
Joined: 09 Sep 2014, 18:38

Re: script to count number of items in a folder view not working

Post by mikeyww » 01 Dec 2022, 07:53

My simple suggestions about posting on the forum are below.

1. Provide a script that the user can run to demonstrate the problem.

2. State what actually happens when you run the script.

3. State what should happen instead, in detail.

4. Include an example.

Gio710
Posts: 31
Joined: 01 Mar 2022, 06:34

Re: script to count number of items in a folder view not working

Post by Gio710 » 11 Dec 2022, 13:20

thank you very much, I'll try to be more specific next time I post

Post Reply

Return to “Ask for Help (v1)”