Script to find out size of specific folders from a list of servers

We need to find the size of two folders from a list of file servers. The problem is they may me mounted on any of the volume on the servers. So no consistency as such.
Only thing that is consistent is the names of two folders.
Example- on all 100 file servers  we need to list size of A and B which are sub folders under shared drive  C  and C is on  a volume (volume name may be anything).

Powershell or VBscript anything can work for me.
Who is Participating?
RobSampsonConnect With a Mentor Commented:
Hi, the third line you changed to
strFolderShare = "NDrive$"

should reflect the "Share Name" that is given to the folder on each server.  So, you should be able to manually browse to

on each server.  Similary, if you have the fourth line as
arrSubFolders = Array("Temp", "Users")

then you must be able to browse to

on each server.  If the folder is multiple levels deep from the shared folder, you can use
arrSubFolders = Array("Temp\Folder1", "Users\MyAccount")

to find

on each server.

I hope that helps.


sydneyithelpdeskAuthor Commented:
Making this bit easy - C is a shared folder and accessible from any computer so we can forget about which volume it resides on.
Hi, give this script a shot.



strInputFile = "servers.txt"
strOutputFile = "foldersize.csv"
strFolderShare = "C$"
arrSubFolders = Array("Temp", "Users")

Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objInput = objFSO.OpenTextFile(strInputFile, ForReading, True)
Set objOutput = objFSO.CreateTextFile(strOutputFile, True)
objOutput.WriteLine """Server"",""" & Join(arrSubFolders, """,""") & """"
While Not objInput.AtEndOfStream
	strComputer = Trim(objInput.ReadLine)
	If strComputer <> "" Then
		If Ping(strComputer) = True Then
			objOutput.Write """" & strComputer & """"
			For Each strSubFolder In arrSubFolders
				strFolderPath = "\\" & strComputer & "\" & strFolderShare & "\" & strSubFolder
				If objFSO.FolderExists(strFolderPath) = False Then
					objOutput.Write ",""<NOT FOUND>"""
					On Error Resume Next
					strSize = 0
					Set objFolder = Nothing
					Set objFolder = objFSO.GetFolder(strFolderPath)
					If Err.Number = 0 Then
						strSize = objFolder.Size/1024/1024
						If Err.Number = 0 Then
							objOutput.Write ",""" & strSize & """"
							objOutput.Write ",""" & Err.Number & ": " & Err.Description & """"
						End If
						objOutput.Write ",""" & Err.Number & ": " & Err.Description & """"
					End If
					On Error Goto 0
				End If
			objOutput.WriteLine """" & strComputer & """<OFFLINE>"",""<OFFLINE>"""
		End If
	End If

WScript.Echo "Done"

Function Ping(strComputer)
	Dim objShell, boolCode
	Set objShell = CreateObject("WScript.Shell")
	boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
	If boolCode = 0 Then
		Ping = True
		Ping = False
	End If
End Function

Open in new window

sydneyithelpdeskAuthor Commented:
Thanks Rob! I was already following one of your article

Here If i try to run this script Result says "Not found"

I changed third line to "Ndrive$" however as it is a shared folder on every server does this require $ with it.
SubsunConnect With a Mentor Commented:
If you are interested in PowerShell script..
C:\server.txt is the input file..
The script will search for directories subsun and PerfLogs in server's C:\ drive (Check line number 6 and 7 to change it)..
[Array]$Result = $null
GC C:\server.txt | % {
$Server = $_
If (Test-Connection $Server -Count 2 -ErrorAction SilentlyContinue) {
 If (Get-PSDrive z -ErrorAction SilentlyContinue) {Remove-PSDrive -Name z}
  If (New-PSDrive -Name z -PSProvider FileSystem -Root \\$Server\C$ -ErrorAction SilentlyContinue) { 
    $Folders = Get-ChildItem z:\ -Recurse | ? {$_.PSIsContainer -eq $true -and $_.Name -eq "Subsun" -or $_.Name -eq "PerfLogs"}
	If ($Folders){
	   foreach ($Folder in $Folders) { 
	    $Result += $Folder | Get-ChildItem -Recurse | Measure-Object -Sum Length | Select-Object `
	    @{N="Size";E={"{0:N2}" -f ($_.sum / 1MB) + " MB"}}
    Else {$Result += New-Object PsObject -Property @{Server=$Server;Path="No folder";Files="NA";Size="NA"}}
  Else {$Result += New-Object PsObject -Property @{Server=$Server;Path="No folder";Files="NA";Size="NA"}}
  Remove-PSDrive -Name z -ErrorAction SilentlyContinue
 Else {$Result += New-Object PsObject -Property @{Server=$Server;Path="Not pinging";Files="NA";Size="NA"}}
$Result | Select Server,Path,Files,Size | Export-Csv c:\report.csv -NoTypeInformation

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.