VBScript – Get a Folder/File Size or Verify That a Folder/File Exists

Here are some snippets of VBScript I pieced together from various sources out on the web to be able to find the size of a folder or file or check that a folder or file exists. This is pretty handy because it also works for UNC path names or files and folders out on network shares, as long as your user has permission to access that path.

Get the size of a folder in megabytes (MB) rounded to two decimal places:

Dim fso:Set fso = WScript.CreateObject("Scripting.FileSystemObject")
Dim oFolder:Set oFolder = fso.GetFolder( "\\serverName\networkShare\folderName")
Wscript.Echo oFolder.Name & " : " & round(oFolder.Size/1024/1024,2)

Get the size of a file in megabytes (MB) rounded to two decimal places:

Dim fso:Set fso = WScript.CreateObject("Scripting.FileSystemObject")
Dim oFile:Set oFile = fso.GetFile( "\\serverName\networkShare\folderName\fileName")
Wscript.Echo oFile.Name & " : " & round(oFile.Size/1024/1024,2)

Building off of that, if you want to verify that the folder exists, we can add these lines:

If fso.FolderExists(oFolder) Then
	Wscript.Echo "The folder does exist."
Else
	Wscript.Echo "The folder does not exist."
End If

Same as above, if you want to verify that file exists, simply change from folder to file:

If fso.FileExists(oFile) Then
	Wscript.Echo "The file does exist."
Else
	Wscript.Echo "The file does not exist."
End If

Leave a Reply