Skip to content
⌘ NSIS Forum Archive

How can I determine how many files I am in a folder?

5 posts

Coby#

How can I determine how many files I am in a folder?

Hello!
I used the Call SearchFiles method but the result was not the one I wanted.
E.g:
When I searched files from two different folders,
The first folder resulted in a certain number of files and the second one continued the remaining count in the first folder.
How can I determine the exact number of files for each folder.

Name "Search File or Folder"
OutFile "SearchFile.exe"
Function SearchFile
  Pop $1
  Pop $0
  StrCpy $2 $1 1 -1
  StrCmp $2 \ 0 +2
  StrCpy $1 $1 -1
  StrCpy $2 $0 1 -1
  StrCmp $2 \ +2
  StrCpy $0 "$0\"
  FindFirst $2 $3 "$0$1"
FindNextFile:
  StrCmp $3 "" NoFiles
  StrCpy $5 "$5$0$3|"
  IntOp $4 $4 + 1
  DetailPrint "$4ş found file: $0$3"
  FindNext $2 $3
  Goto FindNextFile
  NoFiles:
  FindClose $2
  FindFirst $2 $3 "$0*."
  Push $6
  StrCpy $6 $0 "" 1
  StrCmp $6 ":\" +2
  FindNext $2 $3
  Pop $6
FindNextSubfolder:
  FindNext $2 $3
  StrCmp $3 "" NoSubfolders
  IfFileExists "$0$3\" FindNextSubfolder
  Push $0
  Push $1
  Push $2
  Push $3
  Push "$0$3\"
  Push $1
  Call SearchFile
  Pop $3
  Pop $2
  Pop $1
  Pop $0
  IntOp $4 $4 + $R0
  Goto FindNextSubfolder
NoSubfolders:
  FindClose $2
FunctionEnd
var /GLOBAL LOGDIR
Section SearchFile
  Strcpy $LOGDIR "$EXEDIR\Log"
  CreateDirectory "$LOGDIR"
  FileOpen $0 "$LOGDIR\Log.ini" w
  FileWrite $0 "[Log]"
  FileClose $0
  Push "c:\"
  Push "Temp\*.*"
  Call SearchFile
  Strcpy $LOGDIR "$EXEDIR\Log"
  WriteINIStr "$LOGDIR\Log.ini" "Log" "Temp_Files" "Total Files: $4 $5"
  Push "c:\"
  Push "Google\*.*"
  Call SearchFile
  Strcpy $LOGDIR "$EXEDIR\Log"
  WriteINIStr "$LOGDIR\Log.ini" "Log" "Google_Files" "Total Files: $4 $5"
SectionEnd 
Anders#
You call SearchFile two times in a section so it is unclear to me if you are talking about those two or just the recursion inside SearchFile.

If it is inside SearchFile that is the problem then you should code it like this
Function SearchFile
StrCpy $4 0
....
;Recurse:
Push $4
Call ${__FUNCTION__}
Pop $4
Coby#
Thanks Anders
The problem has been solved.
The Call SearchFile function is correct
You just need to set StrCpy $4 0 between the two searches