Mastaxx
Mastaxx

Reputation: 125

AutoHotKey: How to Count number of files in active folder and copy to clipboard?

I have a long list folders that i need to automatically spreadsheet the following two pieces of information into our existing excel document:

  1. Folder Name
  2. File count within that folder

I've written the script up to the point where i now need AHK to get the file count of the active explorer window, copy to clipboard and ALT+TAB/Paste it into the spreadsheet. I plan to loop this script using 'goto' and just monitoring it until it reaches the last folder and use ESC to end the script by eye.

Whats the easiest way to get the numerical value file count of the active window and have it copied to clipboard?

My code so far which is basically using F2 to 'rename' thus copy folder name, alt+tab to spreadsheet, paste it in, move to the file count cell on the spreadsheet, alt+tab back to active explorer window, step into said folder -- and now i'm stuck (where i need to get file count onto clipboard). It's worth noting that i would want the file count to ignore system files like .DS_Store if they're present.

`::

{

Send, {F2}
Sleep, 200
Send, {Ctrl Down}
Sleep, 50
Send, c
sleep, 50
Send, {Ctrl Up}
Sleep, 100
Send, {Alt Down}
Sleep, 50
Send, {Tab}
Sleep, 50
Send, {Alt Up}
Sleep, 100
Send, {Ctrl Down}
Sleep, 50
Send, v
sleep, 50
Send, {Ctrl Up}
Sleep, 100
Send, {Right}
Sleep, 50
Send, {Right}
Sleep, 50
Send, {Right}
Sleep, 100
Send, {Alt Down}
Sleep, 50
Send, {Tab}
Sleep, 50
Send, {Alt Up}
Sleep, 100
Send, {Enter}

^^^^^^^^^^^^^ Need my file count / copy to clipboard here


Esc::ExitApp

}

Upvotes: 1

Views: 1872

Answers (1)

PGilm
PGilm

Reputation: 2312

Maybe have a look at something like this (and follow along in the comments):

; Calculate the number of files in a folder and its subfolders:
SetBatchLines, -1  ; Make the operation run at maximum speed.
FileNum = 0
; FileSelectFolder, WhichFolder  ; Ask the user to pick a folder.
WhichFolder := Clipboard  ;  assumes full path to folder is in clipboard
Loop, Files, %WhichFolder%\*.*, R
{
    if A_LoopFileAttrib contains H,R,S  ; Skip Hidden, Read-only, or System files
        continue  ; Skip this file and move on to the next one
    FileNum += 1
}
Clipboard := FileNum
ClipWait  ; Wait for the clipboard to contain text.
MsgBox %WhichFolder% has %FileNum% files in it (incl. subfolders).

Then, have a look at the following which explains how to read and loop through directories and files: https://autohotkey.com/docs/commands/LoopFile.htm.

Hth, let us know how you make out . . .

Upvotes: 2

Related Questions