Hi, I'm writing a utility that is to find all files in a floder and store their full paths to be used later. I know I can use FindFirstFile to get that information, however I don't want a huge hardcoded ammount of space to be allocated, rather an ammount that would best fit the file information... Anyone know how I could do this? Thanks
Posted on 2004-01-15 16:23:20 by FearHQ
One method would be to make two passes of FindFirstFile/FindNextFile/FindClose, but since they cannot be atomic
(ie. another process may delete/create/change a file in that folder between those two passes or even while you are scanning it), it is not a good solution. So, you will have to allocate memory on demand in, say, 8KB chunks. That means after FindFirstFile you allocate first 8KB and store the first filename, then do FindNextFile. Check if the next filename fits into the allocated memory and if it does not, allocate another 8KB and store the filename. Then do FindNextFile again and again...
Posted on 2004-01-16 04:53:22 by Morris
if your application runs on NT you can use http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/base/readdirectorychangesw.asp

to track wether any file was changed during your findfist/findnext run :)
Posted on 2004-01-16 05:48:08 by Hiroshimator
Morris's method of on-demand memory allocation seems perfect, but I'm uncertain how to manage that... Do you have any code or mabe pseudo-code? I know how to allocate a static pool of memory, but this concept is new to me... Thanks for any help
Posted on 2004-01-16 12:36:50 by FearHQ
First, do:
invoke LocalAlloc,LHND,size_in_bytes

mov hMemory,eax

Then, to access allocated memory you have to
invoke LocalLock,hMemory

When you want to increase the size of the memory block, do
invoke LocalUnlock,hMemory

invoke LocalReAlloc,hMemory,new_size,LMEM_MOVEABLE or LMEM_ZEROINIT
mov hMemory,eax

LocalLock/LocalUnlock must be paired (you have to unlock the hMemory as many times as you locked it).

Posted on 2004-01-17 15:02:11 by Morris
have fun
Posted on 2004-01-17 15:16:06 by zabnik
Thanks guys, I used FindFirstFile/FindNextFile to get a listing, setup notifications on filename change/new file/delete file (has problems with WaitForMultipleObjects? If anyone would want to help, it's in the main forum) and am relisting. I picked GlobalAlloc/ReAlloc, but I guess I'll change to LocalAlloc/ReAlloc... Thanks all
Posted on 2004-01-17 17:12:02 by FearHQ