cjones_mcse
asked on
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.
Or for VBScript:
<CODE SNIPPET>
Dim fso As Object
Dim fold As Folder
Set fso = CreateObject("Scripting.Fi leSystemOb ject")
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>
<CODE SNIPPET>
Dim fso As Object
Dim fold As Folder
Set fso = CreateObject("Scripting.Fi
Set fold = fso.GetFolder("C:\Temp")
Debug.Print "Size on C: " & fso.Drives("C:").FreeSpace
Set fold = Nothing
Set fso = Nothing
</CODE SNIPPET>
Here it is with the comparison:
<CODE SNIPPET>
Dim fso As Object
Dim fold As Folder
Set fso = CreateObject("Scripting.Fi leSystemOb ject")
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>
<CODE SNIPPET>
Dim fso As Object
Dim fold As Folder
Set fso = CreateObject("Scripting.Fi
Set fold = fso.GetFolder("C:\Temp")
If fold.Size > fso.Drives("C:").FreeSpace
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>
ASKER
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.Fi leSystemOb ject")
Set fold = fso.GetFolder("\\Server_Na me\sharepo int\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.
Dim fso, fold, Msg, Title
Msg = ""
Title = ""
Set fso = CreateObject("Scripting.Fi
Set fold = fso.GetFolder("\\Server_Na
If fold.Size > fso.Drives("D:").FreeSpace
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
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.
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:
with just this: Else
also, you're checking drive D: but your messages specify drive C:
Try this:
<CODE SNIPPET>
Dim fso
Dim fold
Dim Msg
Dim Title
Dim Drive
Drive = "D:"
Msg = vbNullString
Title = vbNullString
Set fso = CreateObject("Scripting.Fi leSystemOb ject")
If fso.FolderExists("\\Server _Name\shar epoint\bac kup") Then
Set fold = fso.GetFolder("\\Server_Na me\sharepo int\backup ")
If fold.Size > fso.Drives(Drive).FreeSpac e 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>
<CODE SNIPPET>
Dim fso
Dim fold
Dim Msg
Dim Title
Dim Drive
Drive = "D:"
Msg = vbNullString
Title = vbNullString
Set fso = CreateObject("Scripting.Fi
If fso.FolderExists("\\Server
Set fold = fso.GetFolder("\\Server_Na
If fold.Size > fso.Drives(Drive).FreeSpac
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>
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.Fi leSystemOb ject") '**** Create the instance of the file system object
If oFSObject.FolderExists(sFo lderPath) Then '**** If the folder exists
Set oFSFolder = oFSObject.GetFolder(sFolde rPath) '**** 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>
<CODE SNIPPET>
Dim oFSObject
Dim oFSFolder
Dim sMsg
Dim sDrive
Dim sFolderPath
sFolderPath = "\\Server_Name\sharepoint\
sDrive = "D:"
Set oFSObject = CreateObject("Scripting.Fi
If oFSObject.FolderExists(sFo
Set oFSFolder = oFSObject.GetFolder(sFolde
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>
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.
Else if fold.size < fso.drives("C:").FreeSpace
Should be:
Elseif fold.size < fso.drives("C:").FreeSpace
You had an extra space after the Else. ;)
Or it could be changed to:
Else
as suggested by JesterToo.
ASKER
Tried to run this and get an invalid procedure call or argument error at line 12 (If oFSFolder.Size > oFSObject.Drives(sDrive).F reeSpace Then):
Dim oFSObject
Dim oFSFolder
Dim sMsg
Dim sDrive
Dim sFolderPath
sFolderPath = "\\server_name\sharepoint\ backup"
sDrive = "D:"
Set oFSObject = CreateObject("Scripting.Fi leSystemOb ject") '**** Create the instance of the file system object
If oFSObject.FolderExists(sFo lderPath) Then '**** If the folder exists
Set oFSFolder = oFSObject.GetFolder(sFolde rPath) '**** Get the folder object
If Not oFSFolder Is Nothing Then '**** If we successfully got the folder
If oFSFolder.Size > oFSObject.Drives(sDrive).F reeSpace 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
Dim oFSObject
Dim oFSFolder
Dim sMsg
Dim sDrive
Dim sFolderPath
sFolderPath = "\\server_name\sharepoint\
sDrive = "D:"
Set oFSObject = CreateObject("Scripting.Fi
If oFSObject.FolderExists(sFo
Set oFSFolder = oFSObject.GetFolder(sFolde
If Not oFSFolder Is Nothing Then '**** If we successfully got the folder
If oFSFolder.Size > oFSObject.Drives(sDrive).F
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
ASKER
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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>
<CODE SNIPPET>
If Len(sMsg) > 0 Then
MsgBox sMsg
End If
Set oFSFolder = Nothing
Set oFSObject = Nothing
</CODE SNIPPET>
ASKER
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!
ASKER
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!
:) No problem, glad to hear it worked.
<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
Set fold = Nothing
Set fso = Nothing
</CODE SNIPPET>