#157618 - 2006-02-22 01:51 PM
Folders sorted by size
BunzyBuddy
Fresh Scripter
Registered: 2006-02-22
Posts: 40
Loc: Amsterdam, Holland
I want to scan the size of the users HomeDirs in our HomeDirs folder, sort all folders by size and write the 3 biggest folders' name size in a textfile. Example: Scan all subfolders of: \\Myserver\HomeDirs$ Sort them by size, biggest on top Write name and size of the 3 biggest folders to HomeSize.txt I've been trying some things with GetFolderSize but I'm having some trouble to make it all work. Any help with an axample script would be realy appreciated !
Top
#157619 - 2006-02-22 11:06 PM
Re: Folders sorted by size
Bryce
KiX Supporter
Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
here is something... using the UDF's DirPlus QuickSort Code: break on call 'quicksort.udf' call 'dirplus.udf' $root = dirplus('c:\','/ad') dim $folders[ubound($root)] for $i = 0 to ubound($root) $n = $root[$i].name $s = val($root[$i].size) $folders[$i] = $n,$s next $folders = quicksort($folders,1) for each $item in $folders ? $item[0] " " $item[1] next
Top
#157620 - 2006-02-23 04:48 PM
Re: Folders sorted by size
BunzyBuddy
Fresh Scripter
Registered: 2006-02-22
Posts: 40
Loc: Amsterdam, Holland
Thanx for the reply. I'm not familiair with UDF's so I'm not realy understanding how this works. Where do I put in the path to the HomeDirs and how do I write these results to a textfile ?
Top
#157621 - 2006-02-23 05:33 PM
Re: Folders sorted by size
Bryce
KiX Supporter
Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
in the FAQ forum , there is a sticky post "FAQ Contents Page" and in there you will find "How to use UDFs" That should get ya started
Top
#157622 - 2006-02-23 05:36 PM
Re: Folders sorted by size
Bryce
KiX Supporter
Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
the path to the home dirs's would eb changed in tis line hereCode: $root = dirplus('c:\','/ad') that bit of code is pointing at the "c:\" and you would change your path there. To write to a text file, look into the open() and writeline() commands.
Top
#157623 - 2006-02-24 11:52 AM
Re: Folders sorted by size
BunzyBuddy
Fresh Scripter
Registered: 2006-02-22
Posts: 40
Loc: Amsterdam, Holland
Thanx Bryce I'll give it a try ... and I'll read the "How to use UDF's" on the forum ...
Top
#157624 - 2006-02-24 12:00 PM
Re: Folders sorted by size
BunzyBuddy
Fresh Scripter
Registered: 2006-02-22
Posts: 40
Loc: Amsterdam, Holland
Sorry Bryce, I got an error on the script: ERROR : expected expression! Script: E:\Script test\HomeSize.kix Line : 5
Top
#157626 - 2006-02-24 12:12 PM
Re: Folders sorted by size
BunzyBuddy
Fresh Scripter
Registered: 2006-02-22
Posts: 40
Loc: Amsterdam, Holland
I copied the script you wrote to a HomeSize.kix file, changed the path to the homedirs share (\\server\homedirs$) and tried to run it. Am I forgetting something ?
Top
#157627 - 2006-02-24 12:24 PM
Re: Folders sorted by size
Richard H.
Administrator
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Not my script. Did you read the FAQ on using UDFs? UDFs are functions written by us (or you ) - you need to include them in your script either directly or by using CALL to load them. The simplest way is to use the links in Bryce's post above to open the UDF page, then cut-and-paste the code into your own script.
Top
#157628 - 2006-02-24 03:19 PM
Re: Folders sorted by size
BunzyBuddy
Fresh Scripter
Registered: 2006-02-22
Posts: 40
Loc: Amsterdam, Holland
I'm realy sorry, I'm so stupid ! Of course, I've to put in the UDF to make it all work.... Stupid stupid stupid !!!!
Top
#157629 - 2006-02-24 07:13 PM
Re: Folders sorted by size
NTDOC
Administrator
Registered: 2000-07-28
Posts: 11624
Loc: CA
Quote: cut-and-paste the code into your own script. Now now Richard there will be no cutting around here, copy maybe but no cutting. That's an old bad saying that somehow stuck in eveyone's brain.
Top
#157631 - 2006-02-27 09:22 AM
Re: Folders sorted by size
BunzyBuddy
Fresh Scripter
Registered: 2006-02-22
Posts: 40
Loc: Amsterdam, Holland
I inserted both UDF's in my script. Now, when I'm running it, I got an error on the QuickSort UDF in line $LowerItem = QuickSortItem( $A[$LowerIndex],$indexcol ) ERROR: Expected expression !
Top
#157633 - 2006-02-27 09:42 AM
Re: Folders sorted by size
BunzyBuddy
Fresh Scripter
Registered: 2006-02-22
Posts: 40
Loc: Amsterdam, Holland
That's exactly what I did: I copies the QuickSort UDF and Bryce's code into a new .kix document without changeing anything. Then I got this error. This line giving me that error, you'll find it in the QuickSoft UDF. Running Kix 4.51 I got error : Expexted ')'! in that same line....
Top
#157635 - 2006-02-27 09:47 AM
Re: Folders sorted by size
NTDOC
Administrator
Registered: 2000-07-28
Posts: 11624
Loc: CA
break on $root = dirplus ('C:\Program Files' ,'/ad' ) dim $folders [ubound ($root )] for $i = 0 to ubound ($root ) $n = $root [$i ].name $s = val ($root [$i ].size ) $folders [$i ] = $n ,$s next $folders = quicksort ($folders ,1 ) for each $item in $folders ? $item [0 ] " " $item [1 ]next Function DirPlus($path ,optional $Options , optional $f , optional $sfflag ) If not vartype ($f ) DIM $f EndIf If not vartype ($sfflag ) DIM $sfflag EndIf DIM $file , $i , $temp , $item , $ex1 , $mask ,$mask1 ,$maskArray ,$maskarray1 , $ex2 , $code , $CodeWeight , $targetWeight , $weight , $masktrue DIM $tarray [0 ] $ex1 = SetOption (Explicit ,on ) $ex2 = SetOption (NoVarsInStrings ,on ) $codeWeight = 0 If not Exist ($path ) $temp = SetOption (Explicit ,$ex1 ) $temp = SetOption (NoVarsInStrings ,$ex2 ) Exit @ERROR EndIf If not vartype ($f ) $f = CreateObject ("Scripting.FileSystemObject" ).getfolder ($path ) EndIf If @ERROR $temp = SetOption (Explicit ,$ex1 ) $temp = SetOption (NoVarsInStrings ,$ex2 ) Exit @ERROR EndIf For Each $temp In Split ($options ,"/" ) $temp =Trim ($temp ) Select Case left ($temp ,1 ) = "s" If not vartype ($sfflag ) If Val (right ($temp ,-1 )) = 0 $sfflag = -1 Else $sfflag = Val (right ($temp ,-1 )) EndIf EndIf Case Left ($temp ,1 ) = "a" Select Case Right ($temp ,-1 )="d" $codeWeight = $codeWeight + 1 $temp = "if $file.attributes & 16 " ;"if $file.type = 'File Folder' " Case Right ($temp ,-1 )="-d" $codeWeight = $codeWeight + 1 $temp = "if ($file.attributes & 16)=0 " ;"if $file.type < > 'File Folder' " Case Right ($temp ,-1 )="s" $codeWeight = $codeWeight + 1 $temp = "if $file.attributes & 4 " Case Right ($temp ,-1 )="-s" $codeWeight = $codeWeight + 1 $temp = "if ($file.attributes & 4)=0 " Case Right ($temp ,-1 )="h" $codeWeight = $codeWeight + 1 $temp = "if $file.attributes & 2 " Case Right ($temp ,-1 )="-h" $codeWeight = $codeWeight + 1 $temp = "if ($file.attributes & 2)=0 " Case Right ($temp ,-1 )="r" $codeWeight = $codeWeight + 1 $temp = "if $file.attributes & 1 " Case Right ($temp ,-1 )="-r" $codeWeight = $codeWeight + 1 $temp = "if ($file.attributes & 1)=0 " Case Right ($temp ,-1 )="a" $codeWeight = $codeWeight + 1 $temp = "if $file.attributes & 32 " Case Right ($temp ,-1 )="-a" $codeWeight = $codeWeight + 1 $temp = "if ($file.attributes & 32)=0 " EndSelect $code = $temp + "$weight=$weight+1 endif" +@CRLF + $code Case Left ($temp ,1 ) = "m" $maskarray = Split (Right ($temp ,-2 ),"|" ) $codeweight = $codeweight + 1 $code = "$masktrue=0 for Each $mask in $maskarray if instr($file.name,$mask) $masktrue=1 " + "EndIf Next If $masktrue $weight=$weight+1 endif" + @CRLF +$code Case Left ($temp ,1 ) = "f" $maskarray1 = Split (Right ($temp ,-2 )," " ) $codeweight = $codeweight + 1 $code = "$masktrue=0 for Each $mask1 in $maskarray1 if substr($file.name,Instrrev($file.name,'.')+1)" + "=$mask1 $masktrue=1 EndIf Next If $masktrue $weight=$weight+1 endif" + @CRLF +$code EndSelect Next $code = "$weight = 0 $targetWeight = " + $codeweight + @CRLF + $code $code = $code + "if $weight = $targetweight Exit 1 endif" For Each $file In $f.subfolders If Execute ($code ) $tarray [$i ] = $file $i = $i + 1 ReDIM preserve $tarray [$i ] EndIf If $sfflag $temp = dirplus ($file ,$options ,$file ,$sfflag -1 ) For Each $item In $temp $tarray [$i ] = $item $i = $i + 1 ReDIM preserve $tarray [$i ] Next EndIf Next For Each $file In $f.files If Execute ($code ) $tarray [$i ] = $file $i = $i + 1 ReDIM preserve $tarray [$i ] EndIf Next If $i ReDIM preserve $tarray [$i -1 ] $i =0 Else $tarray = 0 EndIf $dirplus = $tarray $temp = SetOption (Explicit ,$ex1 ) $temp = SetOption (NoVarsInStrings ,$ex2 ) Exit @ERROR EndFunction function quicksort($a , optional $indexcol ) ;$a array to be sorted ;$indexcol index of subitem to sort on ;$LowerIndexStack lower index stack ;$UpperIndexStack upper index stack ;$StackPointer stack pointer ;$LowerIndex lower index ;$UpperIndex upper index ;$Pivot value of pivot ;$TempSwappingValue temp variable for swapping elements ;$index stores either lower or upper index of unsorted segment of the array ;$i counter moving from lower index to the right ;$j counter moving from upper index to the left ;$LowerItem value to compare with dim $StackSize , $StackIncr $StackSize = 8 $StackIncr = $StackSize dim $LowerIndexStack [$StackSize ],$UpperIndexStack [$StackSize ] dim $StackPointer , $LowerIndex , $UpperIndex , $Pivot , $TempSwappingValue , $index , $i , $j , $LowerItem dim $ExitLoop if $indexcol $indexcol = CStr ($indexcol ) if indexcol < 0 $indexcol = 0 endif else $indexcol = 0 endif $LowerIndexStack [0 ]=0 $UpperIndexStack [0 ]=UBOUND ($a ) $StackPointer =0 While $StackPointer >= 0 $LowerIndex =$LowerIndexStack [$StackPointer ] $UpperIndex =$UpperIndexStack [$StackPointer ] While $LowerIndex < $UpperIndex $Pivot =$LowerIndex +($UpperIndex -$LowerIndex )/2 $TempSwappingValue =$a [$LowerIndex ] $A [$LowerIndex ]=$A [$Pivot ] $A [$Pivot ]=$TempSwappingValue $i =$LowerIndex +1 $j =$UpperIndex $ExitLoop =0 Do $LowerItem = QuickSortItem ( $A [$LowerIndex ],$indexcol ) While ($i <$j ) AND ( QuickSortItem ( $A [$i ],$indexcol ) < $LowerItem ) $i =$i +1 Loop While ($j >=$i ) AND ($LowerItem < QuickSortItem ( $A [$j ],$indexcol )) $j =$j -1 Loop if $i >=$j $ExitLoop =1 else $TempSwappingValue =$A [$i ] $A [$i ]=$A [$j ] $A [$j ]=$TempSwappingValue $j =$j -1 $i =$i +1 endif Until $ExitLoop =1 $TempSwappingValue =$a [$LowerIndex ] $a [$LowerIndex ]=$a [$j ] $a [$j ]=$TempSwappingValue $index =$j if $index - $LowerIndex < = $UpperIndex - $index if $index +1 < $UpperIndex if $StackPointer >$StackSize $StackSize = $StackSize + $StackIncr Redim preserve $LowerIndexStack [$StackSize ] Redim preserve $UpperIndexStack [$StackSize ] endif $LowerIndexStack [$StackPointer ]=$index +1 $UpperIndexStack [$StackPointer ]=$UpperIndex $StackPointer =$StackPointer +1 endif $UpperIndex =$index -1 else if $index -1 > $LowerIndex if $StackPointer >$StackSize $StackSize = $StackSize + $StackIncr Redim preserve $LowerIndexStack [$StackSize ] Redim preserve $UpperIndexStack [$StackSize ] endif $LowerIndexStack [$StackPointer ]=$LowerIndex $UpperIndexStack [$StackPointer ]=$index -1 $StackPointer =$StackPointer +1 endif $LowerIndex =$index +1 endif Loop $StackPointer =$StackPointer -1 Loop $quicksort =$a Endfunction Function QuickSortItem( $value , $index ) if vartype ($value ) & 8192 if ($index < = UBound ($value )) $QuickSortItem = $value [$index ] else $QuickSortItem = "" endif else $QuickSortItem = $value endif endfunction
Top
#157636 - 2006-02-27 09:54 AM
Re: Folders sorted by size
BunzyBuddy
Fresh Scripter
Registered: 2006-02-22
Posts: 40
Loc: Amsterdam, Holland
YES That's it !! Now can you tell me how to sort the other way ? Biggest folder on top ?
Top
#157637 - 2006-02-27 02:41 PM
Re: Folders sorted by size
Richard H.
Administrator
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Sure, just go through the array in the other direction. Replace:Code: for each $item in $folders ? $item[0] " " $item[1] next With:Code: For $i = UBound($folders) To 0 Step -1 $item=$folders[$i] $item[0]+" "+$item[1]+@CRLF Next
Top
Moderator: Jochen , Allen , Radimus , Glenn Barnas , ShaneEP , Ruud van Velsen , Arend_ , Mart
0 registered
and 918 anonymous users online.