Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Ffolder size on a remote computer 1

Status
Not open for further replies.

glmrenard

Technical User
Oct 29, 2002
52
FR
Hello,

The following code give me 'null' like result, please help.
I just want to know the size of a remote folder...

strComputer = "servor"
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colFolders = objWMIService.ExecQuery _
("Select * from CIM_Directory Where name = 'M:\\folder1\\folder2\\g-renard'")

i=1
For Each objFolder in colFolders
wscript.echo i,objFolder.name,"---",objFolder.filetype,"---",objFolder.FileSize,objFolder.LastAccessed,objFolder.Status
i=i+1
Next

So I haven't the global size and it is what I need...


If you have any idea... i really need help.

thanks in advance.
 
Is M the local drive? Looks to me like you would want to change the path to be a UNC.



I hope you find this post helpful. Please let me know if it was.

Regards,

Mark
 
Hello,

M is the local disk of the remote computer but I haven't any access via UNC.

Another idea ?
 
I just did some checking and near as I can tell you cant get the folder size that way. Take a look at your code. You are checking the folders but trying to display FileSize.

You will probably want to use the FileSystemObject instead.

Something like this:

Code:
'==========================================================================
'
' NAME: getFolderSizes2XL.vbs
'
' AUTHOR: Mark D. MacLachlan , The Spider's Parlor
' URL: [URL unfurl="true"]http://www.thespidersparlor.com[/URL]
' DATE  : 11/21/2004
'
' COMMENT: <comment>
'
'==========================================================================
Dim oFS, oFolder
Dim objexcel, r, lnameArray, lname, nameLength
set oFS = WScript.CreateObject("Scripting.FileSystemObject")
set oFolder = oFS.GetFolder("C:\Documents and Settings")

Set objExcel = createobject("Excel.application")   
objexcel.Workbooks.add
objexcel.Cells(1, 1).Value = "Folder Name"
objexcel.Cells(1, 2).Value = "Size (MB)"
objexcel.Cells(1, 3).Value = "# Files"
objexcel.Cells(1, 4).Value = "# Sub Folders"
objexcel.Visible = True
Wscript.Sleep 300
r=2


ShowFolderDetails oFolder, r

	
	
'	objexcel.ActiveWorkbook.SaveAs("LogonReport.xls")
'	objexcel.Quit
MsgBox "Done"	
	
Function ShowFolderDetails(oF,r)
	Dim F
    objexcel.Cells(r, 1).Value = oF.Name
    objexcel.Cells(r, 2).Value = oF.Size /1024\1024
    objexcel.Cells(r, 3).Value =  oF.Files.Count
    objexcel.Cells(r, 4).Value =  oF.Subfolders.count
    r = r+1
    for each F in oF.Subfolders
        ShowFolderDetails F, r
    next
End Function

I hope you find this post helpful. Please let me know if it was.

Regards,

Mark
 
Thanks again but I need to get the size of a remote folder so I can't use oFS.GetFolder("C:\Documents and Settings") which works on the local computer.
I cannot mount the remote folder to size it.
 
Hello glmrenard,

You can do it by [1] establishing a directory tree info from the directory you want to get the size, [2] using the info of [1] bind to each and every directory (drive and path) via cim_datafile and sum over the filesize.

As to establishing the directory tree, you can take a look of a simple solution using assocquery on win32_directory and win32_subdirectory at:

In fact, you can as well only use cim_directory no problem, conceptually simpler, with the need to use more efficient assocquery.

regards - tsuji
 
glmrenard,

This is how thing glue together.
Code:
dim afolder()    'must declared dynamic

'directory unescaped string format---must be a valid path
'no validation is performed hereafter
sroot="d:\test\abc"

redim afolder(0) : afolder(0)=sroot

snode="a0b1c2"
set svc=getobject("winmgmts:\\" & snode & "\root\cimv2")
build_foldertree sroot,svc,afolder

foldersize=0
for i=0 to ubound(afolder)
	sdrive=split(afolder(i),"\")(0)
	spath=split(afolder(i),":")(1) & "\"
	spath=replace(spath,"\","\\")
	squery="select filesize from cim_datafile where drive='" & sdrive & "' and path='" & spath & "'"
	set cfile=svc.execquery(squery)
	for each ofile in cfile
		foldersize=foldersize+ofile.filesize
	next
next
set cfile=nothing : set svc=nothing

wscript.echo "Folder : " & sroot & vbcrlf & "Folder size : " & foldersize & " (bytes)"

'core subroutine
sub build_foldertree(sroot, wmisvc, afolder)
    'here you can device sanity test (spared here)
    '[1] afolder is dynamic array
    '[2] afolder is nonempty, at least containing (0) equal to sroot if exists only one component
    '[3] sroot is in the unescaped string format

    sQuery="Associators of {win32_directory.name='" & sroot & "'} " & _
        "where AssocClass=win32_subdirectory " & _
        "ResultRole=PartComponent"

    set cfolder=wmisvc.execquery(sQuery)
    for each ofolder in cfolder    
    i=ubound(afolder)+1
    redim preserve afolder(i)
    afolder(i)=ofolder.name    'consistently in unescaped string format
    next

    for each ofolder in cfolder
        build_foldertree ofolder.name, wmisvc, afolder
    next
end sub
And you can verify that the data so derived agree with the properties gui shown.

- tsuji
 
Hello Tsuji,

Thanks very much :))

It is exactly whjat I need !

Thanks again !
 
Hello, Uh....
Now, I want to know if a folder exist to check its size.
I have a liste a folder (a user in fact) and I want to know if the subfolder
user1/toto
user2/toto
exists

Thanks in advance !

 
Hello, Uh....
Now, I want to know if a folder exist to check its size.
I have a liste a folder (a user in fact) and I want to know if the subfolder
user1/toto
user2/toto
exists

Thanks in advance !

 
glmrenard,

This small revision will take into account of allowing inexisting root directory, in format or in real with acceptable format.
Code:
'directory unescaped string format
'[blue]illegal in format or inexisting directory is allowed
'in this revision[/blue]
sroot="d:\test\abc"    'user input [1]
snode="a0b1c2"         'user input [2]

dim afolder()    'must declared dynamic
redim afolder(0) : afolder(0)=sroot

set svc=getobject("winmgmts:\\" & snode & "\root\cimv2")
iret=build_foldertree(sroot,svc,afolder)

foldersize=0

if iret<>0 then
	wscript.echo "Root folder " & sroot & " does not exist."
else
	for i=0 to ubound(afolder)
		sdrive=split(afolder(i),"\")(0)
		spath=split(afolder(i),":")(1) & "\"
		spath=replace(spath,"\","\\")
		squery="select filesize from cim_datafile where drive='" & sdrive & "' and path='" & spath & "'"
		set cfile=svc.execquery(squery)
		for each ofile in cfile
			foldersize=foldersize+ofile.filesize
		next
	next
	wscript.echo "Folder : " & sroot & vbcrlf & "Folder size : " & foldersize & " (bytes)"
end if
set cfile=nothing : set svc=nothing

'core subroutine
function build_foldertree(sroot, wmisvc, afolder)
    'here you can device sanity test (spared here)
    '[1] afolder is dynamic array
    '[2] afolder is nonempty, at least containing (0) equal to sroot if exists only one component
    '[3] sroot is in the unescaped string format

    sQuery="Associators of {win32_directory.name='" & sroot & "'} " & _
        "where AssocClass=win32_subdirectory " & _
        "ResultRole=PartComponent"

    set cfolder=wmisvc.execquery(sQuery)

	'add this section
	on error resume next
	dummy=cfolder.count
    if err.number<>0 then build_foldertree=err.number : err.clear : exit function
	on error goto 0

	for each ofolder in cfolder
		i=ubound(afolder)+1
		redim preserve afolder(i)
		afolder(i)=ofolder.name    'consistently in unescaped string format
	next
	for each ofolder in cfolder
		call build_foldertree(ofolder.name, wmisvc, afolder)
    next
end function
- tsuji
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top