Page 1 of 1 1
Topic Options
#210249 - 15/05/11 09:13 AM Folder Size
Tchaymutukikaari Offline
Just in Town

Registered: 15/05/11
Posts: 3
Loc: Germany
I got the Task to do a Kixtart-Program which shows All Folders and they Sizes,
I was searching in this Forum and found a Code for the Folder Size, it works but I want to know what is happening.

 Code:
$Directory = "%windir%"
$DirSize = fnGetFolderProp($Directory,Size)
$DirSizeKB = $DirSize / 1024
$DirSizeMB = $DirSizeKB / 1024
$DirSizeGB = $DirSizeMB / 1024

$msg = MessageBox('The ' + $Directory + ' directory is ' + $DirSizeMB + 'MB','Notice',64)

Function fnGetFolderProp($sFldr,$sProp)
    Dim $objFSO, $objFldr, $nul
    $objFSO = CreateObject("Scripting.FileSystemObject")
    If Not VarType($objFSO)=9 Exit 1 EndIf
    $objFldr = $objFSO.GetFolder($sFldr)
    If Not VarType($objFldr)=9 Exit 3 EndIf
    $nul=Execute("$$fnGetFolderProp = $$objFldr."+$sProp)
    If VarType($fnGetFolderProp)=0 Exit 87 EndIf
EndFunction"


Code is from:
http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Number=161567

Top
#210250 - 15/05/11 12:40 PM Re: Folder Size [Re: Tchaymutukikaari]
Allen Moderator Offline
KiX Supporter
*****

Registered: 03/04/19
Posts: 4241
Loc: USA
Beware that fso and .size has returned incorrect values on certain directories, since the release of vista(and up).

http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Number=190300#Post190300

Top
#210251 - 15/05/11 04:32 PM Re: Folder Size [Re: Tchaymutukikaari]
Mart Moderator Offline
KiX Supporter
*****

Registered: 02/03/27
Posts: 4603
Loc: The Netherlands
 Originally Posted By: Tchaymutukikaari

....
it works but I want to know what is happening.
....


Basically what it does is get the size of the folder specified by $directory = %windir%. It retrieves the total size of that folder including all items and sub folders and divide it by 1024 three times to get the size in GB. To get the size of the folder it uses FSO (FileSysemObject).

%windir% can be changed to the folder you need. For example if you have folder called TestFolder on your D drive it could do the same for that folder by changing $Directory = %windir% to $Directory = "D:\TestFolder".
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#210254 - 15/05/12 08:25 AM Re: Folder Size [Re: Mart]
Tchaymutukikaari Offline
Just in Town

Registered: 15/05/11
Posts: 3
Loc: Germany
Thanks for the Answers, is there an IDE what makes it easier to use Kixtart?
And is objFSO an Varaiblename which is spezified?


Edited by Tchaymutukikaari (15/05/12 08:54 AM)

Top
#210255 - 15/05/12 09:26 AM Re: Folder Size [Re: Tchaymutukikaari]
Mart Moderator Offline
KiX Supporter
*****

Registered: 02/03/27
Posts: 4603
Loc: The Netherlands
I have been using AdminScript Editor for years but it is no longer developed and eventually will become too outdated. Editors like EditPlus, NotePad++, Editpad, etc... may have some kind of intellisense so it uses code completion and code coloring to make it easier to read. In some cases it is also possible to create the intellisense for the editor. I also use Notepad++ version 6.0 it is free to download and install and it has intellisense for kix.

FSO is part of Windows and can be called upon by scripts. There is a line that says DIM $objFSO (and more) this is where the variable is declared and $objFSO = CreateObject(... is where it is first used by the script and this also also where the FSO object is created inside the script so it can be used later in the script.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#210256 - 15/05/12 10:46 AM Re: Folder Size [Re: Mart]
Tchaymutukikaari Offline
Just in Town

Registered: 15/05/11
Posts: 3
Loc: Germany
I'm sorry to ask new Questions every Time ,but why I cant get the File-Information.

 Code:
$Directory = "TestDaten"
$FolderList = GetFolders($Directory, SubFolders)

FOR EACH $X in $FolderList

$FolderName = GetFolders($X, Name)
$FolderSize = GetFolders($X, Size)
"" +$FolderName "		" +$FolderSize " Byte" +Chr(10)

$SubFolderList = GetFolders($X, SubFolders)
FOR EACH $Y in $SubFolderList
$FolderName = GetFolders($Y, Name)
$FolderSize = GetFolders($Y, Size)
" -" +$FolderName "		" +$FolderSize " Byte" +Chr(10)
NEXT

$FileList = GetFolders($X, Files)
FOR EACH $Z in FileList
$FileName = GetFiles($Z, Name)
" --" +$FileName +Chr(10)
NEXT

NEXT

GET $X

;---Functions---;
FUNCTION GetFolders($Directory, $Property)
DIM $objFSO, $ObjectDirectory, $Ausgabe

$objFSO = CreateObject("Scripting.FileSystemObject")
If Not VarType($objFSO)=9 
Exit 1 
EndIf

$ObjectDirectory = $objFSO.GetFolder($Directory)
If Not VarType($ObjectDirectory)=9 
Exit 3 
EndIf

$Ausgabe = Execute("$$GetFolders = $$ObjectDirectory." + $Property)
If VarType($GetFolders)=0 
Exit 87 
EndIf

ENDFUNCTION

FUNCTION GetFiles($Directory, $Property)
DIM $objFSO, $ObjectDirectory, $Ausgabe

$objFSO = CreateObject("Scripting.FileSystemObject")
$ObjectDirectory = $objFSO.GetFile($Directory)

$Ausgabe = Execute("$$GetFiles = $$ObjectDirectory." + $Property)
ENDFUNCTION



Edited by Tchaymutukikaari (15/05/12 10:52 AM)

Top
#210257 - 15/05/12 11:37 AM Re: Folder Size [Re: Tchaymutukikaari]
Mart Moderator Offline
KiX Supporter
*****

Registered: 02/03/27
Posts: 4603
Loc: The Netherlands
The first thing that I see is $Directory = "TestDaten". This will never work as you need to specify where this folder is like D:\testfolder for example. To get all files and folders and their properties I would suggest taking a look at the DirPlus() UDF. Everything you need is combined in this one UDF.

A small example:
 Code:
Break on

$folder = "D:\TestFolder"

$list = DirPlus($folder, "/s")

For Each $item in $list
	? $item.name
	? $item.size
	? $item.type
Next

Sleep 5

;====The code below is a UDF. This comes ready for use and does not need to be changed====
;
;Function		DIRPlus()
;
;Author		Bryce Lindsay bryce@isorg.net
;
;Action		Returns an array containing directory files and folders
;
;Syntax		DIRPLUS("PATH","OPTIONS")
;
;Version		2.34
;
;Date Revised	2-10-05
;			2005.09.20 2.34 Filed the file/folder option "d" to be non language dependent, thanks Jochen
;
;Parameters	Path
;		  Full path To To a folder that you want To Return information on.
;		  "c:\program files"
;
;		  OPTIONS
;		  /S          Displays files In specified directory and all subdirectories.
;                             Use a /S# where # is equal to the subfolder depth that you want to recurse.
;
;		  /A          Displays files with specified attributes.
;		  attributes   D  Directories                R  Read-only files
;		               H  Hidden files               A  Files ready For archiving
;		               S  System files               -  Prefix meaning not
;
;		  /M          Apply mask string To filter based on InSTR(), separate Each search string witha a |
;
;		  /F          Return a given File extension like exe log or txt Seperate each extension type with a space
;
;
;Remarks	Finaly fixed this UDF For To handle multiple recursions,
;		also should have a faster responce time since it is using the FSO
;
;		***Please note that the syntax For version 2.0 of this UDF has changed.***
;
;		made some tweeks using feedback from Les! thanks!  Also NTDOC!
;
;Returns	Returns and array of FSO objects that are equal the file and folder objects of
;		the given path.  Also returns a @ERROR code For event handling.
;
;Dependencies 	FSO
;
;KiXtart Ver	4.22
;
;Example(s)	$Dir = dirplus("c:\program files") ;returns all files and folders In the "c:\program files" folder
;		$Dir = dirplus("c:\program files","/s") ;all fiels and folders including subfolders
;		$Dir = dirplus("c:\","/a-d") ;returns only a list of files In the c:\
;		$Dir = dirplus("c:\","/ad") ;returns only a list of folders In the c:\
;		$Dir = dirplus("c:\program files","/ad /s") ;returns only the folders including all subfolders.
;
;		$Dir = dirplus("g:\kix\udf","/s /ad /m dir") ; recursive subfolder search, folders only, using a mask string of "dir"
;
;		$desktop = dirplus("%userprofile%\desktop","/a-d")
;		For Each $file In $desktop
;			? $file
;			? $file.size
;		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
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#210261 - 15/05/13 07:19 AM Re: Folder Size [Re: Mart]
ShaneEP Moderator Offline
MM club member
*****

Registered: 02/11/29
Posts: 1985
Loc: Tulsa, OK
Also, in the script you posted, there is a line that is missing a $.

FOR EACH $Z in FileList

should be...

FOR EACH $Z in $FileList

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Chris S., Glenn Barnas, ShaneEP, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 100 anonymous users online.
Newest Members
johnnyutah, pmk31, ArserHsieh, dumenil, jpw41
17410 Registered Users

Generated in 0.04 seconds in which 0.014 seconds were spent on a total of 13 queries. Zlib compression enabled.

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