Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using FindFirstFile and FindNextFile
#1
Hi Gintaras:

I have a question, why this function shows only the first and second file of a directory.
Function procTree
Code:
Copy      Help
function int'hedit int'hwnd

WIN32_FIND_DATA b
int m=FindFirstFile("c:\*" &b)
rep
,if(m==0)
,,break
,str mf.format("%s",&b.cFileName)
,out mf
,m=FindNextFile(m &b)
,out "%u" GetLastError()

FindClose m


ret

this is the output of this code

$Recycle.Bin
0
09876a35419303c82a
18


Thank you
Regards
#2
Hi Gintaras i have found why

Here it is my code it works well now.
Function procTree
Code:
Copy      Help
function
lpstr lppath=calloc(20 sizeof(lpstr))
lppath="c:\*.dll"
WIN32_FIND_DATA b
int m=FindFirstFile(lppath &b)
rep
,str mf.format("%s",&b.cFileName)
,out mf
,if(FindNextFile(m &b)==0)
,,break
FindClose m


ret

Thank you.
Regards
#3
Hello Gintaras:

I am using the following code to calculate the size of a folder and its subfolders, but I have noticed the return value is different from mine

i am running this macro:
Macro
Code:
Copy      Help
long size1=get_folder_size("c:\windows")

out size1

long size2=GetFileOrFolderSize("c:\windows")

out size2
the output is this:

3501233014
16384405434

I am not sure if my own function is processing "all" the files inside the windows directory, or maybe there are problems with owners and privileges, my function is the following:

So the question is: is there another way to execute this thread, maybe as an administrator level or why this code does not process all files?. (I think there is a problem with special characters)

Function get_folder_size
Code:
Copy      Help
function str'folder
long fsize=0
str dotfolder
dotfolder.right(folder,1)
if(folder.len>0&&StrCompare(dotfolder,".")!=0);;test against .. and .
,str ffilter=folder
,ffilter+"\*.*";;set filter and path
,WIN32_FIND_DATA struFind
,int threadFind=FindFirstFile(ffilter, &struFind)
,rep
,,;; I used this instead of struFind.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY
,,;; because  i think it is a way of testing against many different types of directories like hidden,system, etc.
,,if((struFind.dwFileAttributes|FILE_ATTRIBUTE_DIRECTORY)==struFind.dwFileAttributes)
,,,str fname.format("%s",&struFind.cFileName);; ****I think the problem is here**
,,,;; but i don't know how to get special characters***
,,,;;set next child
,,,str childFolder=""
,,,childFolder+folder
,,,childFolder+"\"
,,,childFolder+fname
,,,out childFolder
,,,long f_bytes=get_folder_size(childFolder);;recursive call
,,,fsize=fsize+f_bytes
,,else
,,,fsize=fsize+struFind.nFileSizeLow
,,if(FindNextFile(threadFind &struFind)==0)
,,,long er=GetLastError()
,,,if (er!=18);; 18 is there aren't files or folders
,,,,out er;; i get 6 very often  i think it's becaus special characters
,,,break
,FindClose threadFind
ret fsize

I have another sample

Macro
Code:
Copy      Help
str directory="C:\software_test\lang_es\revisión1.2"

long size1=get_folder_size(directory)
out size1
long size2=GetFileOrFolderSize(directory)
out size2

Output:

0
221617

Thank you!!

Regards.
#4
These functions don't depend on security.

Insert some out to see what files are found and what are not. Then you will see why.

Also, use Unicode versions, eg FindFirstFileW.
#5
Thank you Gintaras

I will try FindFirstFileW.

Regards.
#6
Hello Gintaras,
As you said i have done some out calls to debug my code, but now I have two questions about Unicode( I think I am doing something wrong with the memory or have memory leaks, because sometimes my program run, and sometimes my program closes unexplectly)

1.- What is the right way to get the cFileName with WIN32_FIND_DATAW i am using the following code:
Function get_folder_size
Code:
Copy      Help
str s.ansi(&struFind.cFileName)
(Where struFind is an instance of WIN32_FIND_DATAW structure, at this time I have executed this code
Code:
Copy      Help
int threadFind=FindFirstFileW(trueFilter,&struFind)
at first, and trueFilter is now a BSTR) is it ok??

When I tried to add struFind.CFileName directly to a BSTR with "add" method it got a number, i guess a pointer or reference and appends it as a part of the string.


2.- a BSTR does not need to be initialized is it right? like this:
Function get_folder_size
Code:
Copy      Help
,,,str s.ansi(&struFind.cFileName)
,,,BSTR childFolder=""
,,,childFolder.add(folder)
,,,childFolder.add("\")
,,,childFolder.add(s)

I have tried to make a chidlFolder.alloc(800), but also my program closes.

Thank you.
Regards.
#7
str s.ansi(&struFind.cFileName) is OK.

int threadFind=FindFirstFileW(trueFilter,&struFind) is OK if trueFilter is BSTR, but easier to pass str or lpstr with @: int threadFind=FindFirstFileW(@trueFilter,&struFind).

Do all string manipulation with str. Convert to Unicode using @, and Unicode to str using ansi. BSTR is probably not needed in your code.

_____________

FYI: dir also uses FindFirstFileW etc. It stores find data in a special local variable _dir. Example:

dir ...
out "0x%X" _dir.fd.dwFileAttributes
#8
Thank you Gintaras Big Grin

It works perfectly Now!!!


Function get_folder_size
Code:
Copy      Help
function% str'folder
long dsize=0
str testdotfolder.right(folder,1)
if(folder.len>0&&StrCompare(testdotfolder,".")!=0)
,str ffilter.format("%s",folder)
,ffilter+"\*"
,WIN32_FIND_DATAW struFind
,long threadFind=FindFirstFileW(@ffilter,&struFind)
,rep
,,if(struFind.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
,,,str filename.ansi(&struFind.cFileName)
,,,str childFolder=""
,,,childFolder+folder
,,,childFolder+"\"
,,,childFolder+filename
,,,long f_bytes=get_folder_size(childFolder)
,,,dsize+f_bytes
,,else
,,,dsize+struFind.nFileSizeLow
,,if(FindNextFileW(threadFind,&struFind)==0)
,,,break
,FindClose threadFind
ret dsize

Regards


Forum Jump:


Users browsing this thread: 2 Guest(s)