Announcement

Collapse
No announcement yet.

Free disk space vbscript help

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Free disk space vbscript help

    Hi all.. I have been working on getting a vbscript built/changed to test for an amount of free disk space and if its less than the criteria, i need the script to exit with a failure code of 5001. Here is what I have so far..

    *****************
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set colDisks = objWMIService.ExecQuery _
    ("Select * from Win32_LogicalDisk Where DeviceID='C:'")
    For Each objDisk in colDisks
    strDiskSize = (objDisk.FreeSpace / ( 1024 * 1024 * 1024 ) )

    If strDiskSize <= 3 then
    WScript.Quit (5001);
    End If

    Next
    ******************

    If i insert Wscript.Echo strDiskSize into the script, it is checking so im happy about that..just can't get my exit to work properly.. also if anyone could help with creating a batch file to call the vbscript.. I am using this to verify a machine has enough disk space available for Vista SP2 and if it does i want it to continue w/ the executable, if not, it needs to exit and possibly relay a message to contact system support.. Thanks in advance guys and although i generally don't post here, its been a great read for as long as I have been a member..

  • #2
    Re: Free disk space vbscript help

    You can launch a vbs script from a bach like,
    wscript.exe "vbscript.vbs"
    or,
    cscript.exe /nologo "vbscript.vbs"

    In the batch use %errorlevel% to expand the exit code.

    When you remove the ";" from WScript.Quit (5001); from your vbscript it should work.


    It is also possible to create and remote the vbs all from within the batch,
    Code:
    @echo off
    call:CREATEVBS > "%temp%.\somevbscript"
    
    
    :: Execute the vbscript from within this batch
    cscript.exe /nologo /e:vbscript "%temp%.\somevbscript"
    
    echo The vbscript returned %errorlevel%
    
    if errorlevel 1 goto :error
    
    echo Available Disksize of C: is sufficient
    goto :end
    
    :error
    echo\
    echo Available Disksize of C: is below 4
    
    :end
    Del "%temp%.\somevbscript"
    
    pause
    
    
    goto:eof  - vbscript -
    :CREATEVBS
    echo/ strComputer = "."
    echo/ 
    echo/ Set objWMIService = GetObject("winmgmts:" _
    echo/    ^& "{impersonationLevel=impersonate}!\\" _
    echo/    ^& strComputer ^& "\root\cimv2")
    echo/ 
    echo/ On Error Resume next
    echo/ Set colDisks = objWMIService.ExecQuery _
    echo/    ("Select * from Win32_LogicalDisk Where DeviceID='C:'")
    echo/ 
    echo/ For Each objDisk in colDisks
    echo/    strDiskSize = (objDisk.FreeSpace / ( 1024^^^3 ) )
    echo/ 
    echo/    If strDiskSize ^<= 3 then
    echo/      Err.Raise 5001
    echo/    Else
    echo/      Err.Clear
    echo/    End If
    echo/ 
    echo/ Next
    echo/ 
    echo/ WScript.Quit Err.Number
    exit/b 0
    \Rems

    This posting is provided "AS IS" with no warranties, and confers no rights.

    __________________

    ** Remember to give credit where credit's due **
    and leave Reputation Points for meaningful posts

    Comment


    • #3
      Re: Free disk space vbscript help

      Thanks so much for the help.. ill give it a shot and let you know how it turns out..

      Comment


      • #4
        Re: Free disk space vbscript help

        Originally posted by Jerzy View Post
        Thanks so much for the help.. ill give it a shot and let you know how it turns out..
        additionally,
        Instead of capturing the exit code, it is also possible to bring a string over from the vbscript to the batch.

        Code:
        @echo off
        call:DISKSIZE C: >"%temp%.\somevbscript"
        
        cscript.exe /nologo /e:vbscript "%temp%.\somevbscript" > "%temp%.\$.cmd"
        
        call "%temp%.\$.cmd"
        
        If defined DiskSize If /i "%DiskSize%" LEQ "3" goto :error
        echo Disksize: %DiskSize% GB & goto :end
        
        :error
        echo\BEEP...
        echo Available Disksize is below 4 GB
        
        
        :end
        Del "%temp%.\somevbscript" & Del "%temp%.\$.cmd"
        
        pause
        
        
        goto:eof  - vbscript -
        :DISKSIZE
        echo/ Set objDisk = GetObject( _
        echo/    "WinMgmts:{impersonationLevel=impersonate}" _
        echo/    ^& "!root/cimv2:Win32_LogicalDisk='%~1'")
        echo/ DiskSize = (objDisk.FreeSpace / ( 1024^^^3 ) )
        echo/ WScript.Echo( "@set ""DiskSize=" ^& Int(DiskSize) ^& """" )
        exit/b 0
        \Rems
        Last edited by Rems; 24th April 2010, 01:16.

        This posting is provided "AS IS" with no warranties, and confers no rights.

        __________________

        ** Remember to give credit where credit's due **
        and leave Reputation Points for meaningful posts

        Comment


        • #5
          Re: Free disk space vbscript help

          Rems.. not sure if i ever got back to you to say thanks for all your help on this.. its up and runnign prefectly. Thanks again for your time and help..

          Comment


          • #6
            Re: Free disk space vbscript help

            I'm also looking for pretty same scripting solution. My goal is to check amount of free space on 640MB ZIP disk and return to user message saying how mach free space on the drive. Already found solution...

            Code:
             
            strComputer = "."
            Set objWMIService = GetObject("winmgmts:" _
            & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
            Set colDisks = objWMIService.ExecQuery _
                ("Select * from Win32_LogicalDisk Where DeviceID = 'E:'")
            For Each objDisk in colDisks
            strDiskSize = (objDisk.FreeSpace / ( 1024 * 1024 ) )
                If strDiskSize <= 100 then
            Wscript.Echo "Not enough free space on MO disk to perfom Audit Backup"& VbCr & _
                         "Please use another MO DISK."
            WScript.Quit
            End If
            Next
            Dim objWMIService, objItem, colDisks, strComputer
            On Error Resume Next
            For Each objItem in colDisks
            Wscript.Echo "Computer: " & objItem.SystemName & VbCr & _
            "" & vbCr & _
            " ==================================" & VbCr & _ 
            "Total Size: " & Int(objItem.Size /1048576) & " MB" & vbCr & _
            " ==================================" & VbCr & _  
            "Free Space: " & Int(objItem.FreeSpace /1048576) & " MB" & vbCr & _
            " ==================================" & VbCr & _ 
            "Integrity check has been successfully copleted!!"
            WSCript.Quit
            Next
            And another one:

            Code:
             
            ::== Space.bat
            @echo off
            setLocal EnableDelayedExpansion
            
            for /f "tokens=3 delims= " %%A in ('dir /s/-c \ ^|find "File(s)"') do (
            set /a fileTOT=%%A/1048576
            )
            for /f "tokens=3 delims= " %%A in ('dir /s/-c ^|find "bytes free"') do (
            set /a FREE=%%A/1048576
            )
            
            echo used: !fileTOT!MB
            echo free: !FREE!MB
            set /a TOTAL=!fileTOT!+!FREE!
            echo total disk space: !TOTAL!MB
            set /a pcUSED=(!fileTOT!*100)/!TOTAL!
            echo percent used: !pcUSED!
            ::==========================
            pause
            Last edited by igor7; 3rd December 2010, 18:03. Reason: My mistake... Found solution already.

            Comment

            Working...
            X