Microsoft Windows Server are very great products to work with. And,... server administrators and operators do not like to do stupid work or repeating tasks. That's why I have made this category, to share my most favorite general scripts that I often use. This is not a scripting site, there are many more and better sites available for that. These scripts are just handy for common and daily use. So, the initially set up was for my own references, but now I share it with you. Enjoy it !! Due to the fact that a big percentage of visitors of this page are international based, this article is in the english language.


List foldersizes with a vbs script:
This script has been made to list foldersizes from a server. There a several tools available, that can do this with a beautiful GUI nowadays, for instance the popular program TreeSizeFree, but there are many more, often free available. Just search the internet for it. Before all those beautiful programs came available, I used a script to to sort of the same thing. And yes, I still use it, because its easy, do not require any installation etc. etc.. Now, I got this script from someone, but I can not reminder the name, it has been a long, long time ago. Feel free to copy this and use it. The script can take some time to run, but the link to an Excelsheet works great and I find it handy from time to time. Just copy/paste it, and save it with a name you want. can be. On top of the script, the locations are specified. Change this to list other directories, is rather selfexplaining. You can do this remotely, and I often do that, otherwise you need to have an Excel version on every server and you don't want that. Good luck with it.


'---------------------------------------------------------------------------------------
' Name:  Listfoldersizes.vbs
' Description: listfoldersizes.vbs calculates the size of all subfolders within
'   a folder and sorts this data in an excel workbook
'---------------------------------------------------------------------------------------

   rootfolder = Inputbox("Enter directory/foldername: " & _
                         chr(10) & chr(10) & "(i.e. C:\Program Files or " & _
                         "\\Servername\C$\Program Files)" & chr(10) & chr(10), _
                         "Getfoldersize", "C:\Program Files")

   outputfile = "c:\foldersize_" & Day(now) & Month(now) & Year(now) & ".xls"

   Set fso = CreateObject("scripting.filesystemobject")
   if fso.fileexists(outputfile) then fso.deletefile(outputfile)

'Create Excel workbook
   set objXL = CreateObject( "Excel.Application" )
   objXL.Visible = False
   objXL.WorkBooks.Add

'Counter 1 for writing in cell A1 within the excel workbook
   icount = 1

'Run checkfolder
   CheckFolder (FSO.getfolder(rootfolder))

Sub CheckFolder(objCurrentFolder)

       For Each objFolder In objCurrentFolder.SubFolders
         FolderSize = objFolder.Size
         Tmp = (FormatNumber(FolderSize, 0, , , 0)/1024)/1024
         ObjXL.ActiveSheet.Cells(icount,1).Value = objFolder.Path
         ObjXL.ActiveSheet.Cells(icount,2).Value = Tmp
         'Wscript.Echo Tmp & " " & objFolder.Path
  'raise counter with 1 for a new row in excel
         icount = icount + 1
       Next

       'Recurse through all of the folders
       For Each objNewFolder In objCurrentFolder.subFolders
               CheckFolder objNewFolder
       Next
      
End Sub


'sort data in excel
objXL.ActiveCell.CurrentRegion.Select
objXL.Selection.Sort objXL.Worksheets(1).Range("B1"), _
                   2, _
                   , _
                   , _
                   , _
                   , _
                   , _
                   0, _
                   1, _
                   False, _
                   1

'Lay out for Excel workbook
   objXL.Range("A1").Select
   objXL.Selection.EntireRow.Insert
   objXL.Selection.EntireRow.Insert
   objXL.Selection.EntireRow.Insert
   objXL.Selection.EntireRow.Insert
   objXL.Selection.EntireRow.Insert

   objXL.Columns(1).ColumnWidth = 60
   objXL.Columns(2).ColumnWidth = 15
   objXL.Columns(2).NumberFormat = "#,##0.0"
   objXL.Range("B1:B1").NumberFormat = "d-m-yyyy"
   objXL.Range("A1:B5").Select
   objXL.Selection.Font.Bold = True
   objXL.Range("A1:B3").Select
   objXL.Selection.Font.ColorIndex = 5
   objXL.Range("A1:A1").Select
   objXL.Selection.Font.Italic = True
   objXL.Selection.Font.Size = 16
   ObjXL.ActiveSheet.Cells(1,1).Value = "Survey FolderSize "
   ObjXL.ActiveSheet.Cells(1,2).Value = Day(now) & "-" & Month(now) & "-"& Year(now)
   ObjXL.ActiveSheet.Cells(3,1).Value = UCase(rootfolder)
   ObjXL.ActiveSheet.Cells(5,1).Value = "Folder"
   ObjXL.ActiveSheet.Cells(5,2).Value = "Total (MB)"


'Finally close the workbook
   ObjXL.ActiveWorkbook.SaveAs(outputfile)
   ObjXL.Application.Quit
   Set ObjXL = Nothing

'Message when finished the looping
   Set WshShell = CreateObject("WScript.Shell")
   Finished = Msgbox ("Script executed successfully, results can be found in " & Chr(10) _
                     & outputfile & "." & Chr(10) & Chr(10) _
                     & "Do you want to view the results now?", 65, "Script executed successfully!")
   if Finished = 1 then WshShell.Run "excel " & outputfile


Now the second script: This script has been make by Anand and does the same thing only you should save this with a ps1 extension, as it is a powershell script. This is much more common these days, and you can use this within and from other applications as well.

# Script: foldersize.ps1
# Usage: Change the working directory where you want to list get size of the folders
# Run the script as c:\<dir where this script exists>\foldersize.ps1
# If you want to create a file with the results of this code, run this script as
# C:\<dir where this script exists>\foldersize.ps1 > homefolders.txt
#
# Written by Anand 

#Get the directory listing and pass the resutls to ForEach
get-childitem | where {$_.PSIsContainer} | foreach {

#For every subfolder get the size
$size = (Get-ChildItem $_ -recurse | where {!$_.PSIsContainer} | Measure-Object -Sum Length).Sum

#add the sub folder path and size into table
$obj = new-object psobject
add-member -inp $obj noteproperty path $_.fullName
add-member -inp $obj noteproperty size $size

#display the table
$obj

}
#End of Script



I probally do not have to remind you, that you should always use scripts with care.
Therefore this posting is "as is" without express or implied warranty.


Printscreen: not available


-----------------------------------------------------------------------------------------------------

Ben OostdamBen Oostdam has been working with Windows systems since 1993. Worked for several companies as a system administrator, and is currently a Senior Support Engineer for Qurius Customer Care in the Netherlands, specialized in System Center Solutions.

Disclaimer: The information contained in this website/article is for general information purposes only. The information is provided as is, by Ben Oostdam and while we endeavour to keep the information up to date and correct, we make no representations or warranties of any kind, express or implied, about the completeness, accuracy, reliability, suitability or availability with respect to the website or the information, products, services, or related graphics contained on the website for any purpose. Any reliance you place on such information is therefore strictly at your own risk. In no event will we be liable for any loss or damage including without limitation, indirect or consequential loss or damage, or any loss or damage whatsoever arising from loss of data or profits arising out of, or in connection with, the use of this website. Through this website you are sometimes able to link to other websites which are not under my control. I have no control over the nature, content and availability of those sites. The inclusion of any links does not necessarily imply a recommendation or endorse the views expressed within them. Every effort is made to keep the website up and running smoothly. However, I take no responsibility for, and will not be liable for, the website being temporarily unavailable due to technical issues beyond our control. All entries in these articles, are my individual opinion, or from co-writers and they don't necessary reflect the opinion of my employer.



 

Wednesday the 14th, November 2018. All rights reserved.. // Oostdam WebDesign