Problem with VBS Syntax


  1. Posts : 46
    Microsoft Windows 10 Pro 64-bit Multiprocessor
       #1

    Problem with VBS Syntax


    I have been experimenting with running Scheduled backups with Macrium Reflect using a generated VB Script which I have successfully modified. I now wish to enhance the VBS to perform the backups only if an external drive exists (i.e. it is switched on), and if there is sufficient space for the backup image. I give below some code which I have extracted from the Macrium Knowledgebase and modified prior to inserting it into the existing script which is working fine.

    Option Explicit
    Dim FSoObject
    Set FSoObject=WScript.CreateObject("Scripting.FileSystemObject")
    If FSoObject.DriveExists("D:") Then
    wscript.echo "D drive exists"

    If GetFreeSpaceGB("D:\OS Partn backups") < 50 Then
    wscript.echo "D:\OS Partn backups Space is < 50GB"
    wscript.echo "D:\OS Partn backups Free Space is " & GetFreeSpaceGB
    wscript.echo GetFreeSpace
    End If

    Else wscript.echo "D drive does not exist"
    End If

    Function GetFreeSpaceGB(ByVal drvPath)
    Dim objFSo, d
    Set objFSo = CreateObject("Scripting.FileSystemObject")
    Set d = objFSo.GetDrive(objFSo.GetDriveName (drvPath))
    GetFreeSpaceGB = d.FreeSpace/ (1024 * 1024 * 1024)
    End Function

    The use of "echo" is only temporary until I get the script to work. At present it displays the "D drive exists" and "Space is < 50GB" messages but has a runtime error on the next statement with :- Wrong number of arguments or invalid property assignment 'GetFreeSpaceGB' Code 800A01C2. 'OS Partn backups' is the name of the target folder on the external D: drive.

    I would appreciate any suggestions for making this code work.
      My Computer


  2. Posts : 238
    Win7-64
       #2

    The GetFreeSpaceGB function is just that - a function. It is not a variable. So you cannot use it in a statement without a function argument - the argument being the drive for which you want the freespace value.

    You tried to do this in the wscript.echo statement which is why you got the error message - VBS expects it to have a function argument.

    Fix this by assigning a variable the value returned by GetFreeSpaceGB("D:\OS Partn backups"):

    FreeSpace = GetFreeSpaceGB("D:\OS Partn backups")
    If FreeSpace < 50 then
    wscript.echo "D:\OS Partn backups Space is < 50GB"
    wscript.echo "D:\OS Partn backups Free Space is " & FreeSpace
    End If
      My Computer


  3. Posts : 46
    Microsoft Windows 10 Pro 64-bit Multiprocessor
    Thread Starter
       #3

    Many thanks bbinnard for your prompt reply, and having corrected my code I am pleased to say that it works as expected. I had previously tried something similar but obviously didn't get the coding quite right.

    I wonder if I might please pose another coding question to you? When manually deleting a file or folder a message window asking for confirmation of moving to the Recycle Bin appears (and if too large another message asking to confirm permanent deletion appears) and both require the Yes or No buttons to be pressed. Can you provide me with the sort of code necessary to equal manually pressing the Yes button and thus avoid the procedure 'hanging' waiting for that action?
      My Computer


  4. Posts : 238
    Win7-64
       #4

    Sorry, I can't help you with that one because I have never actually used VBS - just various versions of VB and VBA. However, I'd guess there is an optional parameter for the Delete function that lets you specify whether it is a hard (really deleted for good) or soft (deleted to Recycle bin) delete.

    If that's not the case then look for another version of the Delete function that says it will bypass the Recycle bin. There are lots of utility programs that let you do either kind of delete so there has to be a way VBS can as well.
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 17:57.
Find Us