News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Counting Files In A Directory

Started by cman, December 25, 2016, 07:19:04 AM

Previous topic - Next topic

cman

What's the best way to count all the files in a certain directory? Thanks for any information and happy holidays! :biggrin:

jj2007

include \masm32\MasmBasic\MasmBasic.inc      ; download
  Init
  GfNoRecurse=1
  GetFiles ExpandEnv$("%WinDir%\*")
  Inkey Str$("%i files found", eax)
EndOfCode


78 files found

cman

Thank you for your reply! :t  I was looking for a "low  level" approach using just the Windows api ( your approach is elegant , but I don't like using libraries for everything - I know that's a bad characteristic to have as a programmer - reinventing the wheel and all that  :biggrin: ).

hutch--

You basically do a FindFirst, FindNext pair of loops and store the names in an array.

cman

Ok , thanks! So you basically use the same algorithm to count the files that you would if you wanted to inspect each file in a directory. I thought there might be a way to get the number ahead of time so I could set up space in a data structure to hold information for each file , but I guess not. I suppose you could loop through each file twice , but that would be inefficient. I'll have to use a dynamic data structure and loop through each file once , I suppose. Thanks for the information! :biggrin:

hutch--

If you are only after the file count OR are not separating file extensions and directories, its only a single loop and its plenty fast enough.