Free disk space vbscript help

Home Forums Scripting General Scripting Free disk space vbscript help

This topic contains 5 replies, has 3 voices, and was last updated by Avatar igor7 9 years, 4 months ago.

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • Avatar
    Jerzy
    Member
    #148620

    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 & “rootcimv2”)
    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..

    Rems
    Rems
    Moderator
    #227704

    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 ^& “rootcimv2”)
    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

    Avatar
    Jerzy
    Member
    #225380

    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..

    Rems
    Rems
    Moderator
    #227705

    Re: Free disk space vbscript help

    Jerzy;201713 wrote:
    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
    echoBEEP…
    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

    Avatar
    Jerzy
    Member
    #225381

    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..

    Avatar
    igor7
    Member
    #295062

    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…

    strComputer = “.”
    Set objWMIService = GetObject(“winmgmts:” _
    & “{impersonationLevel=impersonate}!\” & strComputer & “rootcimv2”)
    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[/CODE]

    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[/CODE][CODE]
    strComputer = “.”
    Set objWMIService = GetObject(“winmgmts:” _
    & “{impersonationLevel=impersonate}!\” & strComputer & “rootcimv2”)
    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[/CODE]

    And another one:

    ::== 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[/CODE][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[/CODE]

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.