Number of files in a folder?

Windows specific forum
Seymour Clufley
Addict
Addict
Posts: 1266
Joined: Wed Feb 28, 2007 9:13 am
Location: London

Number of files in a folder?

Post by Seymour Clufley »

This is a ridiculously basic question and I'm sorry to ask it.

Is there a fast way to get the number of files in a directory, on Windows?

I had a look in PureArea and found this but that simply loops through all the files with NextDirectoryEntry. Surely there's an API call or something which can get the number more quickly than that?

It doesn't need to check sub-directories.
JACK WEBB: "Coding in C is like sculpting a statue using only sandpaper. You can do it, but the result wouldn't be any better. So why bother? Just use the right tools and get the job done."
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Re: Number of files in a folder?

Post by Shield »

There is no other way, at least not as far as I know.
You have to iterate through the directory to count the files.

The main reason for that being the file system's access management.
In that folder there might be some files you're not allowed to see, so the operating system
has to check your access rights for every file.
Image
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds
buddymatkona
Enthusiast
Enthusiast
Posts: 252
Joined: Mon Aug 16, 2010 4:29 am

Re: Number of files in a folder?

Post by buddymatkona »

For this kind of thing I usually use a script but you can still drive the action from PB.
http://www.purebasic.fr/english/viewtop ... fa185f6029
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Re: Number of files in a folder?

Post by MachineCode »

Seymour Clufley wrote:I had a look in PureArea and found this but that simply loops through all the files with NextDirectoryEntry
Actually, that's pretty fast (once you delete the recursion into subfolders of it).

Or maybe populate an (invisible) ExplorerListGadget() with your path and count its files:

Code: Select all

If OpenWindow(0,0,0,400,400,"",#PB_Window_SystemMenu)
  ExplorerListGadget(0,0,0,400,400,"C:\Windows\",#PB_Explorer_NoFolders)
  SetWindowTitle(0,Str(CountGadgetItems(0)-1))
  Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow
EndIf
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
Post Reply