Old 28th March 2011, 09:48   #1
reachb4
Junior Member
 
Join Date: May 2009
Posts: 14
Get the count of total number of files under a folder.

Hi,

What is the script to get the count of files under a folder?


Please advice.


Thanks in advance,

Regards,
John.
reachb4 is offline   Reply With Quote
Old 28th March 2011, 10:17   #2
Highcoder
Member
 
Join Date: Jan 2011
Posts: 69
:-)

As a famous philosoph said a long time ago: "RTFM!"
Joke...

But exactly there is the answer. Keyword: "File Functions Header"

code:
!include "FileFunc.nsh"

Section "GetFileCount"
${GetSize} "C:\YourFolder" "/G=0" $0 $1 $2 ;file count in YourFolder or...
${GetSize} "C:\YourFolder" "/G=1" $0 $1 $2 ;file count in YourFolder include Subdirectorys!
; $0 = contains the size normaly but the option S=nothing means "don´t get size, faster"
; $1 = filecount
; $2 = directorycount
SectionEnd



have fun

cheers
Highcoder is offline   Reply With Quote
Old 28th March 2011, 12:21   #3
jiake
Senior Member
 
jiake's Avatar
 
Join Date: Oct 2007
Location: Shanghai, China
Posts: 207
http://nsis.sourceforge.net/Locate_plugin

Contact me: 137729898@qq.com
jiake is offline   Reply With Quote
Old 28th March 2011, 12:46   #4
pengyou
Major Dude
 
Join Date: Mar 2003
Posts: 571
The NSIS User Manual has some appendices describing lots of useful functions together with sample code showing how to use them.

Appendix E.1 (File Functions Header) includes the GetSize function which can be used to count the number of files in a folder or in a folder and its sub-folders:
http://nsis.sourceforge.net/Docs/AppendixE.html#E.1.3
pengyou is offline   Reply With Quote
Old 28th March 2011, 15:55   #5
MSG
Major Dude
 
Join Date: Oct 2006
Posts: 1,892
Or if you don't need recursive searches, you can just use something simple like this:

PHP Code:
StrCpy $dirs 0
StrCpy $files 0
FindFirst 
$$"C:\YourPath\*.*"
loop:
  
StrCmp $"" done
  
${If} ${FileExists"$1\*.*"
    
IntOp $dirs $dirs 1
  
${Else}
    
IntOp $files $files 1
  
${EndIf}
  
FindNext $$1
  
Goto loop
done
:
FindClose $
The manual is your friend. http://nsis.sourceforge.net/Docs/Chapter4.html
MSG is offline   Reply With Quote
Old 28th March 2011, 17:09   #6
Afrow UK
Moderator
 
Afrow UK's Avatar
 
Join Date: Nov 2002
Location: Surrey, England
Posts: 8,434
You need to ignore $1 if it is . or ..:
code:
ClearErrors
FindFirst $0 $1 "C:\YourPath\*.*"
${DoUntil} ${Errors}
${If} $1 != .
${AndIf} $1 != ..
${If} ${FileExists} "C:\YourPath\$1\*.*"
IntOp $dirs $dirs + 1
${Else}
IntOp $files $files + 1
${EndIf}
${EndIf}
FindNext $0 $1
${Loop}
FindClose $0

Stu
Afrow UK is offline   Reply With Quote
Old 29th March 2011, 05:24   #7
MSG
Major Dude
 
Join Date: Oct 2006
Posts: 1,892
Quote:
Originally Posted by Afrow UK View Post
You need to ignore $1 if it is .. otherwise it will count the files and folders in the parent folder as well!
I don't see how it would count the files from parent. It would simply add two counts to $dirs. FindFirst/Next isn't recursive, either down or up the tree. You can solve the two extra counts by using StrCpy $dirs -2 at the beginning, instead of 0. Saves a bunch of Ifs (but probably breaks if the search directory doesn't exist).
MSG is offline   Reply With Quote
Old 29th March 2011, 09:26   #8
Afrow UK
Moderator
 
Afrow UK's Avatar
 
Join Date: Nov 2002
Location: Surrey, England
Posts: 8,434
Sorry you are right of course. Fixed the error with the FileExist check ($1 is only the file name, not the complete path).

Stu
Afrow UK is offline   Reply With Quote
Reply
Go Back   Winamp & Shoutcast Forums > Developer Center > NSIS Discussion

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump