Page 1 of 1 1
Topic Options
#108657 - 2003-11-21 08:24 PM Count subdirectorys not include files
@lejo @rias Offline
Starting to like KiXtart

Registered: 2003-09-02
Posts: 100
Loc: Medellin, Colombia
Hi, I need count a subdirectorys but not the files that contain a directory. for ex:
c:\progfil\tiv\mus\
c:\progfil\tiv\mas\
c:\progfil\tiv\mes\
c:\progfil\tiv\cfg.ini
c:\progfil\tiv\read.txt

count of subdirs = 3

I use this script

Code:
 
Function dirlist($dirname, optional $options)
Dim $filename, $counter, $filepath, $mask
Dim $list, $sublist, $subcounter

$counter=-1

$dirname=Trim($dirname)
If $dirname=''
$dirname=@CURDIR
EndIf
If Right($dirname,1)='\'
$dirname=Left($dirname,Len($dirname)-1)
EndIf
If GetFileAttr($dirname) & 16
$mask='*.*'
Else
$mask=SubStr($dirname,InStrRev($dirname,'\')+1)
$dirname=Left($dirname,Len($dirname)-Len($mask)-1)
EndIf

ReDim $list[10]
$filename=Dir($dirname+'\'+$mask)
While $filename<>'' AND @ERROR=0
If $filename<>'.' AND $filename<>'..'
Select
Case (GetFileAttr($dirname+'\'+$filename) & 16)
If $options & 1
$counter=$counter+1
If $options & 2
$list[$counter]=$dirname+'\'+$filename+'\'
Else
$list[$counter]=$filename+'\'
EndIf
EndIf
If ($options & 4)
$sublist=dirlist($dirname+'\'+$filename+'\'+$mask,4)
If Ubound($sublist)+1
ReDim preserve $list[Ubound($list)+Ubound($sublist)+1]
For $subcounter=0 to Ubound($sublist)
$counter=$counter+1
If $options & 2
$list[$counter]=$dirname+'\'+$filename+'\'+$sublist[$subcounter]
Else
$list[$counter]=$filename+'\'+$sublist[$subcounter]
EndIf
Next
EndIf
EndIf
Case ($options & 2)
$counter=$counter+1
$list[$counter]=$dirname+'\'+$filename
Case 1
$counter=$counter+1
$list[$counter]=$filename
EndSelect
If $counter mod 10
ReDim preserve $list[$counter+10]
EndIf
EndIf
$filename = Dir('')
Loop

If $counter+1
ReDim preserve $list[$counter]
Else
$list=''
EndIf

If $mask<>'*.*' AND ($options & 4)
$filename=Dir($dirname+'\*.*')
While $filename<>'' AND @ERROR=0
If $filename<>'.' AND $filename<>'..'
If (GetFileAttr($dirname+'\'+$filename) & 16)
$sublist=dirlist($dirname+'\'+$filename+'\'+$mask,4)
If Ubound($sublist)+1
ReDim preserve $list[Ubound($list)+Ubound($sublist)+1]
For $subcounter=0 to Ubound($sublist)
$counter=$counter+1
If $options & 2
$list[$counter]=$dirname+'\'+$filename+'\'+$sublist[$subcounter]
Else
$list[$counter]=$filename+'\'+$sublist[$subcounter]
EndIf
Next
EndIf
EndIf
EndIf
$filename = Dir('')
Loop
EndIf

If $counter+1
ReDim preserve $list[$counter]
Else
$list=''
EndIf

$dirlist=$list
EndFunction

$dirarr=DIRLIST("c:\progfil\tiv",1)

$cantelem=Ubound($dirarr)
?$cantelem




this work fine, but count the files, I need that count only mus, mas med dirs and retorn a value 3.

tks for u help.


_________________________
Look always 4 the best...

Top
#108658 - 2003-11-21 08:32 PM Re: Count subdirectorys not include files
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
There is a FAQ that explains this.

DIR() Primer
If (GetFileAttr($basedir+"\"+$name) & 16) will filter out files, showing only folders. Add a NOT to it and you filter out folders, showing only files.

_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#108659 - 2003-11-21 09:34 PM Re: Count subdirectorys not include files
@lejo @rias Offline
Starting to like KiXtart

Registered: 2003-09-02
Posts: 100
Loc: Medellin, Colombia
Hi les, I donīt know in where part of my script
must insert
If (GetFileAttr($basedir+"\"+$name) & 16)

pls can u tell me?



Edited by @lejo @rias (2003-11-21 09:36 PM)
_________________________
Look always 4 the best...

Top
#108660 - 2003-11-21 09:37 PM Re: Count subdirectorys not include files
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
Code:
 

for each $folder in FolderList('%ProgramFiles%')
? $folder
next


Function FolderList($folderName,optional $mask)
dim $objDir, $objFile, $t, $name
$objDir = CreateObject('Scripting.FileSystemObject').GetFolder($folderName).SubFolders
if @error exit 1 endif ; usually folder not found
For Each $objFile In $objDir
$name=$objFile.name
if ($mask and instr($name,$mask)) or not $mask
$t=$t+'|'+$name
endif
Next
$FolderList=split(substr($t,2),'|')
$objDir = 0
EndFunction


_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#108661 - 2003-11-21 09:48 PM Re: Count subdirectorys not include files
@lejo @rias Offline
Starting to like KiXtart

Registered: 2003-09-02
Posts: 100
Loc: Medellin, Colombia
Lonkero, how I count these number of folders that show your script?
_________________________
Look always 4 the best...

Top
#108662 - 2003-11-21 09:52 PM Re: Count subdirectorys not include files
@lejo @rias Offline
Starting to like KiXtart

Registered: 2003-09-02
Posts: 100
Loc: Medellin, Colombia
$c=0
For Each $folder In FolderList('%ProgramFiles%')
? $folder
$c=$c+1

TKS ALL 4 UR HLP
_________________________
Look always 4 the best...

Top
#108663 - 2003-11-21 09:53 PM Re: Count subdirectorys not include files
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
hmmm, I would have went with:

$count = UBound(FolderList('%ProgramFiles%'))

myself

-Shawn


Top
#108664 - 2003-11-21 09:53 PM Re: Count subdirectorys not include files
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2127
Loc: Tulsa, OK
Dont forget the 'Next' at the end of that...

Code:
 
$c=0
For Each $folder In FolderList('%ProgramFiles%')
? $folder
$c=$c+1
Next


Top
#108665 - 2003-11-21 10:01 PM Re: Count subdirectorys not include files
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
I was not suggesting you insert GetFileAttr()...
You already have it in three places.
I cannot make sense of that spaghetti code. (sorry )
Just trying to get you to understand that GetFileAttr() is what you need to filter on folders. Of course there is the method Radimus (not Lonkero) suggested as well.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#108666 - 2003-11-21 10:01 PM Re: Count subdirectorys not include files
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Never mind - using UBound returns count-1 ...
Top
#108667 - 2003-11-21 10:05 PM Re: Count subdirectorys not include files
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Rad, what happens when this UDF can't find any subfolders ? Does it return a single element array with a blank string ? Maybe you could set an @ERROR level just for that case ?
Top
#108668 - 2003-11-21 10:09 PM Re: Count subdirectorys not include files
Chris S. Offline
MM club member
*****

Registered: 2002-03-18
Posts: 2368
Loc: Earth
Not to belabor the point, but my fnGetFolderProp() can retrieve all of the subfolders of a given folder. Then it would be a matter of counting the subfolder objects.
Top
#108669 - 2003-11-21 10:25 PM Re: Count subdirectorys not include files
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
Here's my entry:
Code:
break ON
$ShellCMD = '%comspec% /c dir c:\data\scripts\ /b /a:d /s'
? "Running: $ShellCMD"
$dirs = WSHPIPE($ShellCMD, 1)
? "directory count = " + ubound($dirs)
Function WshPipe($ShellCMD, OPTIONAL $NoEcho)
Dim $oExec, $Output
$oExec = CreateObject("WScript.Shell").Exec($ShellCMD)
If Not VarType($oExec)=9 $WshPipe="WScript.Shell Exec Unsupported" Exit 10 EndIf
;While Not $oExec.Status Loop
$Output = $oExec.StdOut.ReadAll + $oExec.StdErr.ReadAll
If Not $NoEcho ? $Output Endif
$WshPipe=Split($Output,CHR(10))
Exit($oExec.ExitCode)
EndFunction

_________________________
Home page: http://www.kixhelp.com/hb/

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 1024 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.064 seconds in which 0.024 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org