The MASM Forum

General => The Campus => Topic started by: cman on December 25, 2016, 07:19:04 AM

Title: Counting Files In A Directory
Post by: cman on December 25, 2016, 07:19:04 AM
What's the best way to count all the files in a certain directory? Thanks for any information and happy holidays! :biggrin:
Title: Re: Counting Files In A Directory
Post by: jj2007 on December 25, 2016, 09:10:34 AM
include \masm32\MasmBasic\MasmBasic.inc      ; download (http://masm32.com/board/index.php?topic=94.0)
  Init
  GfNoRecurse=1
  GetFiles (http://www.webalice.it/jj2006/MasmBasicQuickReference.htm#Mb1056) ExpandEnv$("%WinDir%\*")
  Inkey Str$("%i files found", eax)
EndOfCode


78 files found
Title: Re: Counting Files In A Directory
Post by: cman on December 26, 2016, 06:17:58 AM
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: ).
Title: Re: Counting Files In A Directory
Post by: hutch-- on December 26, 2016, 07:33:17 AM
You basically do a FindFirst, FindNext pair of loops and store the names in an array.
Title: Re: Counting Files In A Directory
Post by: cman on December 26, 2016, 09:30:45 AM
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:
Title: Re: Counting Files In A Directory
Post by: hutch-- on December 26, 2016, 10:13:55 AM
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.