Check Directory size using VBScript

Is there a way to check a directory's size in a VBScript? I need to check the size of a remote directory, then check the available space on a local hard disk. I then want to compare the two and if there's not enough space on the destination, the script I have for moving files will exit instead of run.
LVL 10
cjones_mcseAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

codeconquerorCommented:
Use the scripting runtime:

<CODE SNIPPET>
Dim fso As New FileSystemObject
Dim fold As Folder
Set fold = fso.GetFolder("C:\Temp")
Debug.Print "Size on C: " & fso.Drives("C:").FreeSpace & vbCrLf & "Size of Temp: " & fold.Size
Set fold = Nothing
Set fso = Nothing
</CODE SNIPPET>
codeconquerorCommented:
Or for VBScript:

<CODE SNIPPET>
Dim fso As Object
Dim fold As Folder
Set fso = CreateObject("Scripting.FileSystemObject")
Set fold = fso.GetFolder("C:\Temp")
Debug.Print "Size on C: " & fso.Drives("C:").FreeSpace & vbCrLf & "Size of Temp: " & fold.Size
Set fold = Nothing
Set fso = Nothing
</CODE SNIPPET>
codeconquerorCommented:
Here it is with the comparison:

<CODE SNIPPET>
Dim fso As Object
Dim fold As Folder
Set fso = CreateObject("Scripting.FileSystemObject")
Set fold = fso.GetFolder("C:\Temp")
If fold.Size > fso.Drives("C:").FreeSpace Then
     MsgBox("Drive C: does not have enough free space to copy the file.  Please try another folder or another drive.", vbOkOnly + vbExclamation, "Insufficient Disk Space")
End If
Set fold = Nothing
Set fso = Nothing
</CODE SNIPPET>
Your Guide to Achieving IT Business Success

The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

cjones_mcseAuthor Commented:
Should this run as is? I've modified it a little, but I keep getting errors. Here's what I have so far to test it:

Dim fso, fold, Msg, Title
Msg = ""
Title = ""
Set fso = CreateObject("Scripting.FileSystemObject")
Set fold = fso.GetFolder("\\Server_Name\sharepoint\backup")
If fold.Size > fso.Drives("D:").FreeSpace Then
     Msg = "Drive C: does not have enough free space to copy the file.  Please try another folder or another drive."
     Title = "Insufficient Disk Space"
     MsgBox Msg, vbOkOnly + vbExclamation, Title
Else if fold.size <  fso.drives("C:").FreeSpace Then
     Msg = "Drive C: has the necessary free space"
     Title = "Sufficient Disk Space"
     MsgBox Msg, vbokonly + vbExclamation, Title
End If
Set fold = Nothing
Set fso = Nothing

I took out the "As Object", etc, because I kept getting an error about an expected end of statement on line 1. I took out the parenthesis on the MsgBox calls because I kept getting a "Cannot use parenthesis in procedure call" error. Now I get an "Expected End" error. Sorry...not a programmer, and not very familiar with scripting other than what I've found and been able to modify with my limited VB experience.
JesterTooCommented:
replace this line:   Else if fold.size <  fso.drives("C:").FreeSpace Then
with just this:       Else

also, you're checking drive D: but your messages specify drive C:

codeconquerorCommented:
Try this:

<CODE SNIPPET>
Dim fso
Dim fold
Dim Msg
Dim Title
Dim Drive
Drive = "D:"
Msg = vbNullString
Title = vbNullString
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FolderExists("\\Server_Name\sharepoint\backup") Then
     Set fold = fso.GetFolder("\\Server_Name\sharepoint\backup")
     If fold.Size > fso.Drives(Drive).FreeSpace Then
          Msg = "Drive " & Drive & " does not have enough free space to copy the file.  Please try another folder or another drive."
          Title = "Insufficient Disk Space"
          MsgBox Msg
     Else
          Msg = "Drive C: has the necessary free space"
          Title = "Sufficient Disk Space"
          MsgBox Msg
     End If
End If
Set fold = Nothing
Set fso = Nothing
</CODE SNIPPET>
codeconquerorCommented:
Woops, mistake in there, here you go:

<CODE SNIPPET>
Dim oFSObject
Dim oFSFolder
Dim sMsg
Dim sDrive
Dim sFolderPath
sFolderPath = "\\Server_Name\sharepoint\backup"
sDrive = "D:"
Set oFSObject = CreateObject("Scripting.FileSystemObject") '**** Create the instance of the file system object
If oFSObject.FolderExists(sFolderPath) Then '**** If the folder exists
     Set oFSFolder = oFSObject.GetFolder(sFolderPath) '**** Get the folder object
     If Not oFSFolder Is Nothing Then '**** If we successfully got the folder
          If oFSFolder.Size > oFSObject .Drives(sDrive).FreeSpace Then '**** See if the folder is bigger than the drive's free space
               sMsg = "Drive " & sDrive & " does not have enough free space to copy the file.  Please try another folder or another drive."
          Else
               '**** The folder was found and size is okay

               '*************************************************************
               '**** Now put the code in here that will perform the file copy or move procedures  *
               '*************************************************************

               sMsg = "File(s) successfully copied/moved from '" & sFolderPath & "' to the '" & sDrive & "' drive."
          End If
     Else
          '**** Something went wrong trying to get the folder, perhaps permissions aren't set properly, check the rights
          sMsg = "The folder " & sFolderPath & " could not be retrieved.  Please make sure you have the appropriate network permissions and that the folder is properly shared."
     End If
Else
     '**** The folder doesn't exist, it's not shared or there is network communication trouble
     sMsg = "The folder " & sFolderPath & " could not be retrieved.  Please check the folder's path (" & sFolderPath & ") and the local area network connection."
End if
If Len(sMsg) > 0 Then
     MsgBox sMsg
End If
Set oFSFolder = Nothing
Set oFSObject = Nothing
</CODE SNIPPET>
codeconquerorCommented:
Just to clarify, the problem in the original code was:

Else if fold.size <  fso.drives("C:").FreeSpace Then

Should be:

Elseif fold.size <  fso.drives("C:").FreeSpace Then

You had an extra space after the Else.  ;)
Or it could be changed to:

Else

as suggested by JesterToo.
cjones_mcseAuthor Commented:
Tried to run this and get an invalid procedure call or argument error at line 12 (If oFSFolder.Size > oFSObject.Drives(sDrive).FreeSpace Then):
Dim oFSObject
Dim oFSFolder
Dim sMsg
Dim sDrive
Dim sFolderPath
sFolderPath = "\\server_name\sharepoint\backup"
sDrive = "D:"
Set oFSObject = CreateObject("Scripting.FileSystemObject") '**** Create the instance of the file system object
If oFSObject.FolderExists(sFolderPath) Then '**** If the folder exists
     Set oFSFolder = oFSObject.GetFolder(sFolderPath) '**** Get the folder object
     If Not oFSFolder Is Nothing Then '**** If we successfully got the folder
          If oFSFolder.Size > oFSObject.Drives(sDrive).FreeSpace Then '**** See if the folder is bigger than the drive's free space
               sMsg = "Drive " & sDrive & " does not have enough free space to transfer the backup.  Please move existing backups to tape and try again."
          Else
               '**** The folder was found and size is okay

               '*************************************************************
               '**** Now put the code in here that will perform the file copy or move procedures  *
               '*************************************************************

               sMsg = "File(s) successfully copied/moved from '" & sFolderPath & "' to the '" & sDrive & "' drive."
          End If
     Else
          '**** Something went wrong trying to get the folder, perhaps permissions aren't set properly, check the rights
          sMsg = "The folder " & sFolderPath & " could not be retrieved.  Please make sure you have the appropriate network permissions and that the folder is properly shared."
     End If
Else
     '**** The folder doesn't exist, it's not shared or there is network communication trouble
     sMsg = "The folder " & sFolderPath & " could not be retrieved.  Please check the folder's path (" & sFolderPath & ") and the local area network connection."
End if
If Len(sMsg) > 0 Then
     MsgBox sMsg
End If
Set oFSFolder = Nothing
Set oFSObject = Nothing
cjones_mcseAuthor Commented:
A little confused because I got it working in the original code when I changed the Else If to ElseIf. Gone over this about ten times and can't see an error.
codeconquerorCommented:
Found the problem.  Apparently you can't store the drive letter in a variable for the FreeSpace call (If oFSFolder.Size > oFSObject.Drives("D:").FreeSpace Then).  Here is the working code (at least it works on my machine):

<CODE SNIPPET>
Dim oFSObject
Dim oFSFolder
Dim sMsg
Dim sDrive
Dim sFolderPath
sFolderPath = "\\server_name\sharepoint\backup"
sDrive = "D:"
Set oFSObject = CreateObject("Scripting.FileSystemObject") '**** Create the instance of the file system object
If oFSObject.FolderExists(sFolderPath) Then '**** If the folder exists
     Set oFSFolder = oFSObject.GetFolder(sFolderPath) '**** Get the folder object
     If Not oFSFolder Is Nothing Then '**** If we successfully got the folder
          If oFSFolder.Size > oFSObject.Drives("D:").FreeSpace Then '**** See if the folder is bigger than the drive's free space
               sMsg = "Drive " & sDrive & " does not have enough free space to transfer the backup.  Please move existing backups to tape and try again."
          Else
               '**** The folder was found and size is okay

               '*************************************************************
               '**** Now put the code in here that will perform the file copy or move procedures  *
               '*************************************************************

               sMsg = "File(s) successfully copied/moved from '" & sFolderPath & "' to the '" & sDrive & "' drive."
          End If
     Else
          '**** Something went wrong trying to get the folder, perhaps permissions aren't set properly, check the rights
          sMsg = "The folder " & sFolderPath & " could not be retrieved.  Please make sure you have the appropriate network permissions and that the folder is properly shared."
     End If
Else
     '**** The folder doesn't exist, it's not shared or there is network communication trouble
     sMsg = "The folder " & sFolderPath & " could not be retrieved.  Please check the folder's path (" & sFolderPath & ") and the local area network connection."
End if
If Len(sMsg) > 0 Then
     Response.Write(sMsg)
End If
Set oFSFolder = Nothing
Set oFSObject = Nothing
</CODE SNIPPET>

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
codeconquerorCommented:
That was the ASP version, if not running from IIS, then change the bottom 5 lines to:

<CODE SNIPPET>
If Len(sMsg) > 0 Then
     MsgBox sMsg
End If
Set oFSFolder = Nothing
Set oFSObject = Nothing
</CODE SNIPPET>
cjones_mcseAuthor Commented:
Perfect! Works great! Since I can't give a grade higher than an A, I'm going to up the points to 500! Thanks for the help!
cjones_mcseAuthor Commented:
Perfect! Works great! Since I can't give a grade higher than an A, I'm going to up the points to 500! Thanks for the help!
codeconquerorCommented:
:)  No problem, glad to hear it worked.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.