Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

Command line utility for monitoring free disk space

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

  • PBCC Command line utility for monitoring free disk space

    Another command line utility for keeping an eye on drive free space. Desired free space can be specified in bytes or as percentage.

    DrvSpMon /Hide A:B: C:10% H:25% X:53,687,091,200
    Above will skip drives A:,B: and D: and free space limits of C: 10% free, H: 25% free and X: 50 Gig free
    Note: Commas are allowed but are meaningless to the program

    DrvSpMon /?
    Code:
    DrvSpMon.EXE   Version: 0.1.0 (Build: 1)
    
    Usage:
    DrvSpMon [option] [option] [...] C:[##.##%|###,###,###,###,###] [...]
    
    Options:
       /NO_TITLE     - No Title bars on output
       /NO_REMOVABLE - Don't list removable drives
       /NO_FIXED     - Don't list fixed drives
       /NO_REMOTE    - Don't list remote drives
       /NO_CD-ROM    - Don't list CD-ROM drives
       /NO_RAM-DISK  - Don't list RAM drives
       /HIDE X:Y:Z:  - Don't list "X:" "Y:" "Z:" drives
       /TRAY
    Test run with nothing in diskette or CD-ROM drives
    Code:
    -------------------------------------------------------------------------------
      |Type    |       Used        |       Free        |       Total       | % Free
    -------------------------------------------------------------------------------
    A: Removable                  0                   0                   0   0.00%
    C: Fixed        102,782,377,984      25,250,844,672     128,033,222,656  19.72%
    D: CD-ROM                     0                   0                   0   0.00%
    E: Fixed      3,925,983,670,272      74,766,827,520   4,000,750,497,792   1.87%
    F: Removable     31,583,797,248         398,802,944      31,982,600,192   1.25%
    R: Fixed         19,035,103,232     118,403,584,000     137,438,687,232  86.15%
    Z: Removable    390,641,598,464   3,610,110,734,336   4,000,752,332,800  90.24%
    ===============================================================================
    Test run with something in both drives
    Code:
    -------------------------------------------------------------------------------
      |Type    |       Used        |       Free        |       Total       | % Free
    -------------------------------------------------------------------------------
    A: Removable          1,115,648             342,016           1,457,664  23.46%
    C: Fixed        102,782,386,176      25,250,836,480     128,033,222,656  19.72%
    D: CD-ROM            20,209,664                   0          20,209,664   0.00%
    E: Fixed      3,925,983,670,272      74,766,827,520   4,000,750,497,792   1.87%
    F: Removable     31,583,797,248         398,802,944      31,982,600,192   1.25%
    R: Fixed         19,035,103,232     118,403,584,000     137,438,687,232  86.15%
    Z: Removable    390,641,598,464   3,610,110,734,336   4,000,752,332,800  90.24%
    ===============================================================================
    DrvSpMon.bas
    Code:
    ' Usage DrvSpMon /NO_TITLE /NO_REMOVABLE /NO_FIXED /NO_REMOTE /NO_CD-ROM /NO_RAM-DISK /TRAY /HIDE A:B:D: C:10% H:53,687,091,200
    ' Heavily inspired by: https://forum.powerbasic.com/forum/user-to-user-discussions/programming/775746-command-line-tool-for-checking-drive-space-using-pb-function-diskfree-and-disksize
    ' Other pieces of code and ideas from various posts on the PowerBASIC forums...Many Thanks to all who contribute!!!
    
    ' Version Scheme - Major,Minor,Revision,Build
    %APP_MAJOR            = 0
    %APP_MINOR            = 1
    %APP_REVISION         = 0
    %APP_BUILD            = 1
    $APP_VERSION          = "0.1.0 Build-00001"
    $APP_NAME             = "DrvSpMon"
    $APP_ORIGINALFILENAME = $APP_NAME + ".EXE"
    
    #BREAK         OFF
    #COMPILE       EXE
    #COMPILER      PBCC 6
    #CONSOLE       ON
    #DEBUG DISPLAY OFF
    #DEBUG ERROR   OFF
    #DIM           ALL
    #OPTIMIZE      SPEED
    #OPTION        VERSION5
    #REGISTER      NONE
    #RESOURCE      VERSIONINFO
    #RESOURCE      FILEFLAGS 0&
    #RESOURCE      FILEVERSION    %APP_MAJOR,%APP_MINOR,%APP_REVISION,%APP_BUILD
    #RESOURCE      PRODUCTVERSION %APP_MAJOR,%APP_MINOR,%APP_REVISION,%APP_BUILD
    #RESOURCE      STRINGINFO "0409", "04E4"
    #RESOURCE      VERSION$ "Author",           "Michael A. Rice"
    #RESOURCE      VERSION$ "Comments",         "Command line utility for monitoring drive free space"
    #RESOURCE      VERSION$ "CompanyName",      "None"
    #RESOURCE      VERSION$ "FileDescription",  "Main executable"
    #RESOURCE      VERSION$ "FileVersion",      $APP_VERSION
    #RESOURCE      VERSION$ "InternalName",     "Drive Space Monitor"
    #RESOURCE      VERSION$ "LegalCopyright",   "Copyright © None"
    #RESOURCE      VERSION$ "OriginalFilename", $APP_ORIGINALFILENAME
    #RESOURCE      VERSION$ "ProductName",      $APP_NAME
    #RESOURCE      VERSION$ "ProductVersion",   $APP_VERSION
    #RESOURCE      VERSION$ "Compiler",         "PBCC 6"
    #RESOURCE      MANIFEST,1,"Manifest.xml"    ' From José's includes
    #TOOLS         OFF
    
    %WIN32_LEAN_AND_MEAN = 1
    #INCLUDE ONCE "WIN32API.INC"                ' José Roca includes
    
    #IF NOT %DEF(%FALSE)
       %FALSE = 0&
    #ENDIF
    
    #IF NOT %DEF(%TRUE)
       %TRUE = NOT %FALSE
    #ENDIF
    
    #IF NOT %DEF(%NULL)
       %NULL = 0???
    #ENDIF
    
    TYPE Flag_Type
       NoTitle     AS BIT * 1 IN BYTE
       NoRemovable AS BIT * 1
       NoFixed     AS BIT * 1
       NoRemote    AS BIT * 1
       NoCDROM     AS BIT * 1
       NoRAMDISK   AS BIT * 1
       Hide        AS BIT * 1
       Tray        AS BIT * 1
    END TYPE
    
    FUNCTION PBMAIN()
       LOCAL dwDriveType         AS DWORD
       LOCAL sDriveType          AS STRING
       LOCAL sDrive              AS STRING
       LOCAL sDriveHide          AS STRING
       LOCAL sDriveReqSpace      AS STRING
       LOCAL qDriveReqSpace      AS QUAD
       LOCAL qDriveSize          AS QUAD
       LOCAL qDriveFree          AS QUAD
       LOCAL qDriveUsed          AS QUAD
       LOCAL cDrivePctFree       AS CURRENCY
       LOCAL sCommand            AS STRING
       LOCAL sMsg                AS STRING
       LOCAL dwWork              AS DWORD
       LOCAL sWork               AS STRING
       LOCAL dwInStrPos          AS DWORD
       LOCAL X                   AS LONG
       LOCAL sRegExpMask         AS STRING
       LOCAL dwRegExpLen         AS DWORD
       LOCAL dwRegExpPos         AS DWORD
       LOCAL Flag                AS Flag_Type
    
       IF INSTR(COMMAND$,"/?") THEN
          CON.STDOUT HelpString
          FUNCTION = 0
          EXIT FUNCTION
       END IF
    
       sCommand = UCASE$(COMMAND$)
    
       IF INSTR(sCommand,"/NO_TITLE") THEN
          Flag.NoTitle = %TRUE
          REPLACE "/NO_TITLE" WITH "" IN sCommand
       END IF
    
       IF INSTR(sCommand,"/NO_REMOVABLE") THEN
          Flag.NoRemovable = %TRUE
          REPLACE "/NO_REMOVABLE" WITH "" IN sCommand
       END IF
    
       IF INSTR(sCommand,"/NO_FIXED") THEN
          Flag.NoFixed = %TRUE
          REPLACE "/NO_FIXED" WITH "" IN sCommand
       END IF
    
       IF INSTR(sCommand,"/NO_REMOTE") THEN
          Flag.NoRemote = %TRUE
          REPLACE "/NO_REMOTE" WITH "" IN sCommand
       END IF
    
       IF INSTR(sCommand,"/NO_CD-ROM") THEN
          Flag.NoCDROM = %TRUE
          REPLACE "/NO_CD-ROM" WITH "" IN sCommand
       END IF
    
       IF INSTR(sCommand,"/NO_RAM-DISK") THEN
          Flag.NoRAMDISK = %TRUE
          REPLACE "/NO_RAM-DISK" WITH "" IN sCommand
       END IF
    
       dwInStrPos = INSTR(sCommand,"/HIDE ")
       IF dwInStrPos THEN
          Flag.Hide = %TRUE
    
          ' Verify syntax  ---  Allow 1 to 25 drives to be hidden
          ' Build regular expression mask
          sRegExpMask = "/HIDE[ ]+[A-Z]:"
          FOR X = 24 TO 1 STEP -1
             sRegExpMask += "(" + REPEAT$(X,"[A-Z]:") + ")|"
          NEXT X
          'sRegExpMask = CLIP$(RIGHT sRegExpMask,1) + "( |$)"
          sRegExpMask += "()( |$)"
    
          REGEXPR sRegExpMask IN sCommand AT dwInStrPos TO dwRegExpPos,dwRegExpLen
    
          IF dwRegExpPos THEN
             sWork = MID$(sCommand,dwRegExpPos,dwRegExpLen)
             dwWork = VERIFY(6,sWork," ")
             sDriveHide = MID$(sWork,dwWork,(LEN(sWork)-dwWork)+1)
             sCommand = CLIP$(MID sCommand,dwRegExpPos,dwRegExpLen)
          ELSE
             CON.STDOUT HelpString
             FUNCTION = 1
             EXIT FUNCTION
          END IF
       END IF
    
       IF INSTR(sCommand,"/TRAY") THEN
          Flag.Tray = %TRUE
          REPLACE "/TRAY" WITH "" IN sCommand
       END IF
    
       sCommand = REMOVE$(sCommand,ANY ",")
    
       ' Verify syntax  ---  Allow 1 to 26 drives to have free space low amount specified
       ' Build regular expression mask
       RESET sRegExpMask
       FOR X = 26 TO 1 STEP -1
          sRegExpMask += "(" + REPEAT$(X,"[A-Z]:[0-9]+[%]?[ ]*") + ")|"
       NEXT X
       sRegExpMask = CLIP$(RIGHT sRegExpMask,1)
    
       REGEXPR sRegExpMask IN sCommand TO dwRegExpPos,dwRegExpLen
    
       IF dwRegExpPos THEN
          sDriveReqSpace = REMOVE$(MID$(sCommand,dwRegExpPos,dwRegExpLen),ANY " ")
          sCommand = TRIM$(CLIP$(MID sCommand,dwRegExpPos,dwRegExpLen))
       ELSEIF TRIM$(sCommand) = "" THEN
          sCommand = ""
          EXIT IF      ' Nothing else was specified on the command line...nothing to do here!
       ELSE
          CON.STDOUT HelpString
          FUNCTION = 1
          EXIT FUNCTION
       END IF
    
       IF sCommand <> "" THEN
          CON.STDOUT "Error: unexpected input " + $DQ + sCommand + $DQ
          FUNCTION = 1
          EXIT FUNCTION
       END IF
    
       IF ISFALSE Flag.NoTitle THEN
          CON.STDOUT STRING$(79,"-")
          CON.STDOUT "  |Type    |       Used        |       Free        |       Total       | % Free"
          CON.STDOUT STRING$(79,"-")
       END IF
    
       FOR X = 65 TO 90     ' A --> Z
          sDrive = CHR$(X) + ":\" + $NUL
          dwDriveType = GetDriveTypeA(BYVAL STRPTR(sDrive))
          sDrive = LEFT$(sDrive,2)
    
          SELECT CASE CONST dwDriveType
             CASE %DRIVE_UNKNOWN
                sDriveType = "Unknown"
             CASE %DRIVE_NO_ROOT_DIR
                sDriveType = "NoRoot"
             CASE %DRIVE_REMOVABLE
                IF ISTRUE Flag.NoRemovable THEN ITERATE FOR
                sDriveType = "Removable"
             CASE %DRIVE_FIXED
                IF ISTRUE Flag.NoFixed THEN ITERATE FOR
                sDriveType = "Fixed"
             CASE %DRIVE_REMOTE
                IF ISTRUE Flag.NoRemote THEN ITERATE FOR
                sDriveType = "Remote"
             CASE %DRIVE_CDROM
                IF ISTRUE Flag.NoCDROM THEN ITERATE FOR
                sDriveType = "CD-ROM"
             CASE %DRIVE_RAMDISK
                IF ISTRUE Flag.NoRAMDISK THEN ITERATE FOR
                sDriveType = "RAM"
             CASE ELSE
                sDriveType = "Error"
          END SELECT
    
          IF INSTR(sDriveHide,sDrive) THEN ITERATE FOR
    
          IF dwDriveType <> %DRIVE_NO_ROOT_DIR THEN
             qDriveSize    = DISKSIZE(sDrive)
             qDriveFree    = DISKFREE(sDrive)
             qDriveUsed    = qDriveSize - qDriveFree
             cDrivePctFree = (qDriveFree/qDriveSize)*100
             IF cDrivePctFree < 0 THEN cDrivePctFree = 0
             sMsg = LEFT$(sDrive,2) + " " + _
                    LSET$(sDriveType,9) + _
                    RSET$(FORMAT$(qDriveUsed,"#,"),19) + " " + _
                    RSET$(FORMAT$(qDriveFree,"#,"),19) + " " + _
                    RSET$(FORMAT$(qDriveSize,"#,"),19) + " " + _
                    RSET$(FORMAT$(cDrivePctFree,"##0.00"),6) + "%"
             CON.STDOUT sMsg
             ' Check drive free space for drives specified
             dwInStrPos = INSTR(sDriveReqSpace,sDrive)
             IF dwInStrPos THEN
                sRegExpMask = "[0-9]+"
                REGEXPR sRegExpMask IN sDriveReqSpace AT (dwInStrPos + 2) TO dwRegExpPos,dwRegExpLen
                qDriveReqSpace = VAL(MID$(sDriveReqSpace,dwRegExpPos,dwRegExpLen))
                IF MID$(sDriveReqSpace,dwRegExpPos+dwRegExpLen,1) = "%" THEN
                   IF cDrivePctFree < qDriveReqSpace THEN
                      FUNCTION = 1    ' Drive is under free space
                   END IF
                ELSE
                   IF qDriveFree < qDriveReqSpace THEN
                      FUNCTION = 1    ' Drive is under free space
                   END IF
                END IF
             END IF
          END IF
       NEXT X
    
       IF ISFALSE Flag.NoTitle THEN
          CON.STDOUT STRING$(79,"=")
       END IF
    END FUNCTION
    
    FUNCTION HelpString() AS STRING
       FUNCTION = $APP_ORIGINALFILENAME + "   Version: " + _
                  FORMAT$(%APP_MAJOR) + "." + _
                  FORMAT$(%APP_MINOR) + "." + _
                  FORMAT$(%APP_REVISION) + " " + _
                  "(Build: " + FORMAT$(%APP_BUILD) + ")" + $CRLF + $CRLF + _
                  "Usage:" + $CRLF + _
                  "DrvSpMon [option] [option] [...] C:[##.##%|###,###,###,###,###] [...]" + $CRLF + $CRLF + _
                  "Options: " + $CRLF + _
                  "   /NO_TITLE     - No Title bars on output" + $CRLF + _
                  "   /NO_REMOVABLE - Don't list removable drives" + $CRLF + _
                  "   /NO_FIXED     - Don't list fixed drives" + $CRLF + _
                  "   /NO_REMOTE    - Don't list remote drives" + $CRLF + _
                  "   /NO_CD-ROM    - Don't list CD-ROM drives" + $CRLF + _
                  "   /NO_RAM-DISK  - Don't list RAM drives" + $CRLF + _
                  "   /HIDE X:Y:Z:  - Don't list " + $DQ + "X:" + $DQ + " " + $DQ + "Y:" + $DQ + " " + $DQ + "Z:" + $DQ + " drives" + $CRLF + _
                  "   /TRAY"
    END FUNCTION
    DrvSpMon.zip

  • #2
    Fixed bug that didn't return an error to batch file if a drive was listed to be monitored but wasn't checked... i.e. Drive not present

    Code:
    ' Usage DrvSpMon /NO_TITLE /NO_REMOVABLE /NO_FIXED /NO_REMOTE /NO_CD-ROM /NO_RAM-DISK /TRAY /HIDE A:B:D: C:10% H:53,687,091,200
    ' Heavily inspired by: https://forum.powerbasic.com/forum/user-to-user-discussions/programming/775746-command-line-tool-for-checking-drive-space-using-pb-function-diskfree-and-disksize
    ' Other pieces of code and ideas from various posts on the PowerBASIC forums...Many Thanks to all who contribute!!!
    
    ' Version Scheme - Major,Minor,Revision,Build
    %APP_MAJOR            = 0
    %APP_MINOR            = 1
    %APP_REVISION         = 1
    %APP_BUILD            = 2
    $APP_VERSION          = "0.1.1 Build-00002"
    $APP_NAME             = "DrvSpMon"
    $APP_ORIGINALFILENAME = $APP_NAME + ".EXE"
    
    #BREAK         OFF
    #COMPILE       EXE
    #COMPILER      PBCC 6
    #CONSOLE       ON
    #DEBUG DISPLAY OFF
    #DEBUG ERROR   OFF
    #DIM           ALL
    #OPTIMIZE      SPEED
    #OPTION        VERSION5
    #REGISTER      NONE
    #RESOURCE      VERSIONINFO
    #RESOURCE      FILEFLAGS 0&
    #RESOURCE      FILEVERSION    %APP_MAJOR,%APP_MINOR,%APP_REVISION,%APP_BUILD
    #RESOURCE      PRODUCTVERSION %APP_MAJOR,%APP_MINOR,%APP_REVISION,%APP_BUILD
    #RESOURCE      STRINGINFO "0409", "04E4"
    #RESOURCE      VERSION$ "Author",           "Michael A. Rice"
    #RESOURCE      VERSION$ "Comments",         "Command line utility for monitoring drive free space"
    #RESOURCE      VERSION$ "CompanyName",      "None"
    #RESOURCE      VERSION$ "FileDescription",  "Main executable"
    #RESOURCE      VERSION$ "FileVersion",      $APP_VERSION
    #RESOURCE      VERSION$ "InternalName",     "Drive Space Monitor"
    #RESOURCE      VERSION$ "LegalCopyright",   "Copyright © None"
    #RESOURCE      VERSION$ "OriginalFilename", $APP_ORIGINALFILENAME
    #RESOURCE      VERSION$ "ProductName",      $APP_NAME
    #RESOURCE      VERSION$ "ProductVersion",   $APP_VERSION
    #RESOURCE      VERSION$ "Compiler",         "PBCC 6"
    #RESOURCE      MANIFEST,1,"Manifest.xml"    ' From José's includes
    #TOOLS         OFF
    
    %WIN32_LEAN_AND_MEAN = 1
    #INCLUDE ONCE "WIN32API.INC"                ' José Roca includes
    
    #IF NOT %DEF(%FALSE)
       %FALSE = 0&
    #ENDIF
    
    #IF NOT %DEF(%TRUE)
       %TRUE = NOT %FALSE
    #ENDIF
    
    #IF NOT %DEF(%NULL)
       %NULL = 0???
    #ENDIF
    
    TYPE Flag_Type
       NoTitle     AS BIT * 1 IN BYTE
       NoRemovable AS BIT * 1
       NoFixed     AS BIT * 1
       NoRemote    AS BIT * 1
       NoCDROM     AS BIT * 1
       NoRAMDISK   AS BIT * 1
       Hide        AS BIT * 1
       Tray        AS BIT * 1
    END TYPE
    
    FUNCTION PBMAIN()
       LOCAL dwDriveType         AS DWORD
       LOCAL sDriveType          AS STRING
       LOCAL sDrive              AS STRING
       LOCAL sDrivesProcessed    AS STRING
       LOCAL sDriveHide          AS STRING
       LOCAL sDriveReqSpace      AS STRING
       LOCAL qDriveReqSpace      AS QUAD
       LOCAL qDriveSize          AS QUAD
       LOCAL qDriveFree          AS QUAD
       LOCAL qDriveUsed          AS QUAD
       LOCAL cDrivePctFree       AS CURRENCY
       LOCAL sCommand            AS STRING
       LOCAL sMsg                AS STRING
       LOCAL dwWork              AS DWORD
       LOCAL sWork               AS STRING
       LOCAL dwInStrPos          AS DWORD
       LOCAL X                   AS LONG
       LOCAL sRegExpMask         AS STRING
       LOCAL dwRegExpLen         AS DWORD
       LOCAL dwRegExpPos         AS DWORD
       LOCAL Flag                AS Flag_Type
    
       IF INSTR(COMMAND$,"/?") THEN
          CON.STDOUT HelpString
          FUNCTION = 0
          EXIT FUNCTION
       END IF
    
       sCommand = UCASE$(COMMAND$)
    
       IF INSTR(sCommand,"/NO_TITLE") THEN
          Flag.NoTitle = %TRUE
          REPLACE "/NO_TITLE" WITH "" IN sCommand
       END IF
    
       IF INSTR(sCommand,"/NO_REMOVABLE") THEN
          Flag.NoRemovable = %TRUE
          REPLACE "/NO_REMOVABLE" WITH "" IN sCommand
       END IF
    
       IF INSTR(sCommand,"/NO_FIXED") THEN
          Flag.NoFixed = %TRUE
          REPLACE "/NO_FIXED" WITH "" IN sCommand
       END IF
    
       IF INSTR(sCommand,"/NO_REMOTE") THEN
          Flag.NoRemote = %TRUE
          REPLACE "/NO_REMOTE" WITH "" IN sCommand
       END IF
    
       IF INSTR(sCommand,"/NO_CD-ROM") THEN
          Flag.NoCDROM = %TRUE
          REPLACE "/NO_CD-ROM" WITH "" IN sCommand
       END IF
    
       IF INSTR(sCommand,"/NO_RAM-DISK") THEN
          Flag.NoRAMDISK = %TRUE
          REPLACE "/NO_RAM-DISK" WITH "" IN sCommand
       END IF
    
       dwInStrPos = INSTR(sCommand,"/HIDE ")
       IF dwInStrPos THEN
          Flag.Hide = %TRUE
    
          ' Verify syntax  ---  Allow 1 to 25 drives to be hidden
          ' Build regular expression mask
          sRegExpMask = "/HIDE[ ]+[A-Z]:"
          FOR X = 24 TO 1 STEP -1
             sRegExpMask += "(" + REPEAT$(X,"[A-Z]:") + ")|"
          NEXT X
          sRegExpMask += "()( |$)"
    
          REGEXPR sRegExpMask IN sCommand AT dwInStrPos TO dwRegExpPos,dwRegExpLen
    
          IF dwRegExpPos THEN
             sWork = MID$(sCommand,dwRegExpPos,dwRegExpLen)
             dwWork = VERIFY(6,sWork," ")
             sDriveHide = MID$(sWork,dwWork,(LEN(sWork)-dwWork)+1)
             sCommand = CLIP$(MID sCommand,dwRegExpPos,dwRegExpLen)
          ELSE
             CON.STDOUT HelpString
             FUNCTION = 1
             EXIT FUNCTION
          END IF
       END IF
    
       IF INSTR(sCommand,"/TRAY") THEN
          Flag.Tray = %TRUE
          REPLACE "/TRAY" WITH "" IN sCommand
       END IF
    
       sCommand = REMOVE$(sCommand,ANY ",")
    
       ' Verify syntax  ---  Allow 1 to 26 drives to have free space low amount specified
       ' Build regular expression mask
       RESET sRegExpMask
       FOR X = 26 TO 1 STEP -1
          sRegExpMask += "(" + REPEAT$(X,"[A-Z]:[0-9]+[%]?[ ]*") + ")|"
       NEXT X
       sRegExpMask = CLIP$(RIGHT sRegExpMask,1)
    
       REGEXPR sRegExpMask IN sCommand TO dwRegExpPos,dwRegExpLen
    
       IF dwRegExpPos THEN
          sDriveReqSpace = REMOVE$(MID$(sCommand,dwRegExpPos,dwRegExpLen),ANY " ")
          sCommand = TRIM$(CLIP$(MID sCommand,dwRegExpPos,dwRegExpLen))
       ELSEIF TRIM$(sCommand) = "" THEN
          sCommand = ""
          EXIT IF      ' Nothing else was specified on the command line...nothing to do here!
       ELSE
          CON.STDOUT HelpString
          FUNCTION = 1
          EXIT FUNCTION
       END IF
    
       IF sCommand <> "" THEN
          CON.STDOUT "Error: unexpected input " + $DQ + sCommand + $DQ
          FUNCTION = 1
          EXIT FUNCTION
       END IF
    
       IF ISFALSE Flag.NoTitle THEN
          CON.STDOUT STRING$(79,"-")
          CON.STDOUT "  |Type    |       Used        |       Free        |       Total       | % Free"
          CON.STDOUT STRING$(79,"-")
       END IF
    
       FOR X = 65 TO 90     ' A --> Z
          sDrive = CHR$(X) + ":\" + $NUL
          dwDriveType = GetDriveTypeA(BYVAL STRPTR(sDrive))
          sDrive = LEFT$(sDrive,2)
    
          SELECT CASE CONST dwDriveType
             CASE %DRIVE_UNKNOWN
                sDriveType = "Unknown"
             CASE %DRIVE_NO_ROOT_DIR
                sDriveType = "NoRoot"
             CASE %DRIVE_REMOVABLE
                IF ISTRUE Flag.NoRemovable THEN ITERATE FOR
                sDriveType = "Removable"
             CASE %DRIVE_FIXED
                IF ISTRUE Flag.NoFixed THEN ITERATE FOR
                sDriveType = "Fixed"
             CASE %DRIVE_REMOTE
                IF ISTRUE Flag.NoRemote THEN ITERATE FOR
                sDriveType = "Remote"
             CASE %DRIVE_CDROM
                IF ISTRUE Flag.NoCDROM THEN ITERATE FOR
                sDriveType = "CD-ROM"
             CASE %DRIVE_RAMDISK
                IF ISTRUE Flag.NoRAMDISK THEN ITERATE FOR
                sDriveType = "RAM"
             CASE ELSE
                sDriveType = "Error"
          END SELECT
    
          IF INSTR(sDriveHide,sDrive) THEN ITERATE FOR
    
          IF dwDriveType <> %DRIVE_NO_ROOT_DIR THEN
             sDrivesProcessed += sDrive
             qDriveSize        = DISKSIZE(sDrive)
             qDriveFree        = DISKFREE(sDrive)
             qDriveUsed        = qDriveSize - qDriveFree
             cDrivePctFree     = (qDriveFree/qDriveSize)*100
             IF cDrivePctFree < 0 THEN cDrivePctFree = 0
             sMsg = LEFT$(sDrive,2) + " " + _
                    LSET$(sDriveType,9) + _
                    RSET$(FORMAT$(qDriveUsed,"#,"),19) + " " + _
                    RSET$(FORMAT$(qDriveFree,"#,"),19) + " " + _
                    RSET$(FORMAT$(qDriveSize,"#,"),19) + " " + _
                    RSET$(FORMAT$(cDrivePctFree,"##0.00"),6) + "%"
             CON.STDOUT sMsg
             ' Check drive free space for drives specified
             dwInStrPos = INSTR(sDriveReqSpace,sDrive)
             IF dwInStrPos THEN
                sRegExpMask = "[0-9]+"
                REGEXPR sRegExpMask IN sDriveReqSpace AT (dwInStrPos + 2) TO dwRegExpPos,dwRegExpLen
                qDriveReqSpace = VAL(MID$(sDriveReqSpace,dwRegExpPos,dwRegExpLen))
                IF MID$(sDriveReqSpace,dwRegExpPos+dwRegExpLen,1) = "%" THEN
                   IF cDrivePctFree < qDriveReqSpace THEN
                      FUNCTION = 1    ' Drive is under free space
                   END IF
                ELSE
                   IF qDriveFree < qDriveReqSpace THEN
                      FUNCTION = 1    ' Drive is under free space
                   END IF
                END IF
             END IF
          END IF
       NEXT X
    
       ' Make sure all requested drives were checked...return error if NOT
       IF LEN(REMOVE$(RETAIN$(sDriveReqSpace,ANY CHR$(65 TO 90)),ANY sDrivesProcessed)) THEN
          FUNCTION = 1
       END IF
    
       IF ISFALSE Flag.NoTitle THEN
          CON.STDOUT STRING$(79,"=")
       END IF
    END FUNCTION
    
    FUNCTION HelpString() AS STRING
       FUNCTION = $APP_ORIGINALFILENAME + "   Version: " + _
                  FORMAT$(%APP_MAJOR) + "." + _
                  FORMAT$(%APP_MINOR) + "." + _
                  FORMAT$(%APP_REVISION) + " " + _
                  "(Build: " + FORMAT$(%APP_BUILD) + ")" + $CRLF + $CRLF + _
                  "Usage:" + $CRLF + _
                  "DrvSpMon [option] [option] [...] C:[##.##%|###,###,###,###,###] [...]" + $CRLF + $CRLF + _
                  "Options: " + $CRLF + _
                  "   /NO_TITLE     - No Title bars on output" + $CRLF + _
                  "   /NO_REMOVABLE - Don't list removable drives" + $CRLF + _
                  "   /NO_FIXED     - Don't list fixed drives" + $CRLF + _
                  "   /NO_REMOTE    - Don't list remote drives" + $CRLF + _
                  "   /NO_CD-ROM    - Don't list CD-ROM drives" + $CRLF + _
                  "   /NO_RAM-DISK  - Don't list RAM drives" + $CRLF + _
                  "   /HIDE X:Y:Z:  - Don't list " + $DQ + "X:" + $DQ + " " + $DQ + "Y:" + $DQ + " " + $DQ + "Z:" + $DQ + " drives" + $CRLF + _
                  "   /TRAY"  + $CRLF + $CRLF + _
                  "Usage note: Don't hide a drive that appears in the list of drives to monitor"
    END FUNCTION
    DrvSpMon.zip

    Comment


    • #3
      New version. Added some new command line features...like specifying monitoring sizes in K,M,G,T and /Hide can be abbreviated as /H now

      DrvSpMon /?
      DrvSpMon.EXE Version: 1.0.0 (Build: 3)

      Usage:
      DrvSpMon [option] [option] [...] C:[##.##%|###,###,###,###,###][K|M|G|T] [...]

      Options:
      /NO_TITLE - No Title bars on output
      /NO_REMOVABLE - Don't list removable drives
      /NO_FIXED - Don't list fixed drives
      /NO_REMOTE - Don't list remote drives
      /NO_CD-ROM - Don't list CD-ROM drives
      /NO_RAM-DISK - Don't list RAM drives
      /H[IDE] X:Y:Z: - Don't list "X:" "Y:" "Z:" drives
      /TRAY

      Example:
      DrvSpMon /HIDE A:B: /NO_CD-ROM /NO_RAM-DISK C:10G

      Usage note: Don't hide a drive that appears in the list of drives to monitor

      Test.cmd
      Code:
      DrvSpMon /H A:B: /NO_CD-ROM C:20G
      @IF ERRORLEVEL 1 @ECHO WARNING: Error returned!!  Check drives
      
      DrvSpMon /H A:B: /NO_CD-ROM C:22G
      @IF ERRORLEVEL 1 @ECHO WARNING: Error returned!!  Check drives
      
      DrvSpMon /H A:B: /NO_CD-ROM C:17.51%%
      @IF ERRORLEVEL 1 @ECHO WARNING: Error returned!!  Check drives
      
      DrvSpMon /H A:B: /NO_CD-ROM C:17.52%%
      @IF ERRORLEVEL 1 @ECHO WARNING: Error returned!!  Check drives
      
      DrvSpMon /H A:B: /NO_CD-ROM C:17.53%%
      @IF ERRORLEVEL 1 @ECHO WARNING: Error returned!!  Check drives
      
      DrvSpMon /H A:B:C: /NO_CD-ROM C:1%%
      @IF ERRORLEVEL 1 @ECHO WARNING: Error returned!!  Check drives
      Results:
      Code:
      DrvSpMon /H A:B: /NO_CD-ROM C:20G
      -------------------------------------------------------------------------------
        |Type    |       Used        |       Free        |       Total       | % Free
      -------------------------------------------------------------------------------
      C: Fixed        105,598,107,648      22,435,115,008     128,033,222,656  17.52%
      ===============================================================================
      
      :: Note 22G = 23,622,320,128 Bytes
      DrvSpMon /H A:B: /NO_CD-ROM C:22G
      -------------------------------------------------------------------------------
        |Type    |       Used        |       Free        |       Total       | % Free
      -------------------------------------------------------------------------------
      C: Fixed        105,598,111,744      22,435,110,912     128,033,222,656  17.52%
      ===============================================================================
      WARNING: Error returned!!  Check drives
      
      DrvSpMon /H A:B: /NO_CD-ROM C:17.51%
      -------------------------------------------------------------------------------
        |Type    |       Used        |       Free        |       Total       | % Free
      -------------------------------------------------------------------------------
      C: Fixed        105,598,111,744      22,435,110,912     128,033,222,656  17.52%
      ===============================================================================
      
      DrvSpMon /H A:B: /NO_CD-ROM C:17.52%
      -------------------------------------------------------------------------------
        |Type    |       Used        |       Free        |       Total       | % Free
      -------------------------------------------------------------------------------
      C: Fixed        105,598,111,744      22,435,110,912     128,033,222,656  17.52%
      ===============================================================================
      
      DrvSpMon /H A:B: /NO_CD-ROM C:17.53%
      -------------------------------------------------------------------------------
        |Type    |       Used        |       Free        |       Total       | % Free
      -------------------------------------------------------------------------------
      C: Fixed        105,598,111,744      22,435,110,912     128,033,222,656  17.52%
      ===============================================================================
      WARNING: Error returned!!  Check drives
      
      DrvSpMon /H A:B:C: /NO_CD-ROM C:1%
      -------------------------------------------------------------------------------
        |Type    |       Used        |       Free        |       Total       | % Free
      -------------------------------------------------------------------------------
      ===============================================================================
      WARNING: Error returned!!  Check drives
      DrvSpMon.bas
      Code:
      ' Usage DrvSpMon /NO_TITLE /NO_REMOVABLE /NO_FIXED /NO_REMOTE /NO_CD-ROM /NO_RAM-DISK /TRAY /HIDE A:B:D: C:10% H:53,687,091,200
      ' Heavily inspired by: https://forum.powerbasic.com/forum/user-to-user-discussions/programming/775746-command-line-tool-for-checking-drive-space-using-pb-function-diskfree-and-disksize
      ' Other pieces of code and ideas from various posts on the PowerBASIC forums...Many Thanks to all who contribute!!!
      
      ' Version Scheme - Major,Minor,Revision,Build
      %APP_MAJOR            = 1
      %APP_MINOR            = 0
      %APP_REVISION         = 0
      %APP_BUILD            = 3
      $APP_VERSION          = "1.0.0 Build-00003"
      $APP_NAME             = "DrvSpMon"
      $APP_ORIGINALFILENAME = $APP_NAME + ".EXE"
      
      #BREAK         OFF
      #COMPILE       EXE
      #COMPILER      PBCC 6
      #CONSOLE       ON
      #DEBUG DISPLAY OFF
      #DEBUG ERROR   OFF
      #DIM           ALL
      #OPTIMIZE      SPEED
      #OPTION        VERSION5
      #REGISTER      NONE
      #RESOURCE      VERSIONINFO
      #RESOURCE      FILEFLAGS 0&amp;amp;
      #RESOURCE      FILEVERSION    %APP_MAJOR,%APP_MINOR,%APP_REVISION,%APP_BUILD
      #RESOURCE      PRODUCTVERSION %APP_MAJOR,%APP_MINOR,%APP_REVISION,%APP_BUILD
      #RESOURCE      STRINGINFO "0409", "04E4"
      #RESOURCE      VERSION$ "Author",           "Michael A. Rice"
      #RESOURCE      VERSION$ "Comments",         "Command line utility for monitoring drive free space"
      #RESOURCE      VERSION$ "CompanyName",      "None"
      #RESOURCE      VERSION$ "FileDescription",  "Main executable"
      #RESOURCE      VERSION$ "FileVersion",      $APP_VERSION
      #RESOURCE      VERSION$ "InternalName",     "Drive Space Monitor"
      #RESOURCE      VERSION$ "LegalCopyright",   "Copyright © None"
      #RESOURCE      VERSION$ "OriginalFilename", $APP_ORIGINALFILENAME
      #RESOURCE      VERSION$ "ProductName",      $APP_NAME
      #RESOURCE      VERSION$ "ProductVersion",   $APP_VERSION
      #RESOURCE      VERSION$ "Compiler",         "PBCC 6"
      #RESOURCE      MANIFEST,1,"Manifest.xml"    ' From José's includes
      #TOOLS         OFF
      
      %WIN32_LEAN_AND_MEAN = 1
      #INCLUDE ONCE "WIN32API.INC"                ' José Roca includes
      
      $SizeValidSuffix = "(K)|(M)|(G)|(T)|( )|$"
      
      %KB =      1024
      %MB = %KB * %KB
      %GB = %MB * %KB
      %TB = %GB * %KB
      
      #IF NOT %DEF(%FALSE)
      %FALSE = 0&amp;amp;
      #ENDIF
      
      #IF NOT %DEF(%TRUE)
      %TRUE = NOT %FALSE
      #ENDIF
      
      #IF NOT %DEF(%NULL)
      %NULL = 0???
      #ENDIF
      
      TYPE Flag_Type
         NoTitle     AS BIT * 1 IN BYTE
      NoRemovable AS BIT * 1
         NoFixed     AS BIT * 1
         NoRemote    AS BIT * 1
         NoCDROM     AS BIT * 1
         NoRAMDISK   AS BIT * 1
         Hide        AS BIT * 1
         Tray        AS BIT * 1
      END TYPE
      
      FUNCTION PBMAIN()
      LOCAL dwDriveType         AS DWORD
         LOCAL sDriveType          AS STRING
         LOCAL sDrive              AS STRING
         LOCAL sDrivesProcessed    AS STRING
         LOCAL sDriveHide          AS STRING
         LOCAL sDriveReqSpace      AS STRING
         LOCAL cDriveReqSpace      AS CURRENCY
         LOCAL qDriveSize          AS QUAD
         LOCAL qDriveFree          AS QUAD
         LOCAL qDriveUsed          AS QUAD
         LOCAL cDrivePctFree       AS CURRENCY
         LOCAL sCommand            AS STRING
         LOCAL sMsg                AS STRING
         LOCAL dwWork              AS DWORD
         LOCAL sWork               AS STRING
         LOCAL dwInStrPos          AS DWORD
         LOCAL X                   AS LONG
         LOCAL sRegExpMask         AS STRING
         LOCAL dwRegExpLen         AS DWORD
         LOCAL dwRegExpPos         AS DWORD
         LOCAL Flag                AS Flag_Type
      
      IF INSTR(COMMAND$,"/?") THEN
            CON.STDOUT HelpString
      FUNCTION = 0
      EXIT FUNCTION
         END IF
      
      sCommand = UCASE$(COMMAND$)
      
      IF INSTR(sCommand,"/NO_TITLE") THEN
      Flag.NoTitle = %TRUE
      REPLACE "/NO_TITLE" WITH "" IN sCommand
      END IF
      
         IF INSTR(sCommand,"/NO_REMOVABLE") THEN
      Flag.NoRemovable = %TRUE
      REPLACE "/NO_REMOVABLE" WITH "" IN sCommand
      END IF
      
         IF INSTR(sCommand,"/NO_FIXED") THEN
      Flag.NoFixed = %TRUE
      REPLACE "/NO_FIXED" WITH "" IN sCommand
      END IF
      
         IF INSTR(sCommand,"/NO_REMOTE") THEN
      Flag.NoRemote = %TRUE
      REPLACE "/NO_REMOTE" WITH "" IN sCommand
      END IF
      
         IF INSTR(sCommand,"/NO_CD-ROM") THEN
      Flag.NoCDROM = %TRUE
      REPLACE "/NO_CD-ROM" WITH "" IN sCommand
      END IF
      
         IF INSTR(sCommand,"/NO_RAM-DISK") THEN
      Flag.NoRAMDISK = %TRUE
      REPLACE "/NO_RAM-DISK" WITH "" IN sCommand
      END IF
      
      dwInStrPos = MAX(INSTR(sCommand,"/HIDE "),INSTR(sCommand,"/H "))
      IF dwInStrPos THEN
      Flag.Hide = %TRUE
      REPLACE "/HIDE" WITH "" IN sCommand
      REPLACE "/H"    WITH "" IN sCommand
      
      ' Verify syntax  ---  Allow 1 to 25 drives to be hidden
            ' Build regular expression mask
      sRegExpMask = "[ ]+[A-Z]:"
      FOR X = 24 TO 1 STEP -1
               sRegExpMask += "(" + REPEAT$(X,"[A-Z]:") + ")|"
      NEXT X
            sRegExpMask += "()( |$)"
      
      REGEXPR sRegExpMask IN sCommand AT dwInStrPos TO dwRegExpPos,dwRegExpLen
      
      IF dwRegExpPos THEN
      sWork = MID$(sCommand,dwRegExpPos,dwRegExpLen)
      dwWork = VERIFY(sWork," ")
      sDriveHide = MID$(sWork,dwWork,(LEN(sWork)-dwWork)+1)
      sCommand = CLIP$(MID sCommand,dwRegExpPos,dwRegExpLen)
      ELSE
               CON.STDOUT HelpString
      FUNCTION = 1
      EXIT FUNCTION
            END IF
         END IF
      
         IF INSTR(sCommand,"/TRAY") THEN
      Flag.Tray = %TRUE
      REPLACE "/TRAY" WITH "" IN sCommand
      END IF
      
      sCommand = REMOVE$(sCommand,ANY ",")
      
      ' Verify syntax  ---  Allow up to 26 drives to have free space low amount specified
      sRegExpMask = "([A-Z]:[0-9]+" + $SizeValidSuffix + ")|([A-Z]:[0-9.]+%)"
      RESET sWork
      ' Search for drives and store them in sWork if any found
      FOR X = 1 TO 26
      REGEXPR sRegExpMask IN sCommand TO dwRegExpPos,dwRegExpLen
      IF dwRegExpPos THEN
      sWork += MID$(sCommand,dwRegExpPos,dwRegExpLen) + ","       ' Put "," between each
      sCommand = CLIP$(MID sCommand,dwRegExpPos,dwRegExpLen)
      ELSE
               EXIT FOR
            END IF
         NEXT X
      
      IF LEN(sWORK) THEN
      sWork = CLIP$(RIGHT sWork,1)                                   ' Delete the extra trailing ","
      sDriveReqSpace = REMOVE$(sWork,ANY " ")
      sCommand = TRIM$(sCommand)
      ELSEIF TRIM$(sCommand) = "" THEN
      sCommand = ""
      EXIT IF      ' Nothing else was specified on the command line...nothing to do here!
      ELSE
            CON.STDOUT HelpString
      FUNCTION = 1
      EXIT FUNCTION
         END IF
      
         IF sCommand &amp;lt;&amp;gt; "" THEN
            CON.STDOUT "Error: unexpected input " + $DQ + sCommand + $DQ
            FUNCTION = 1
      EXIT FUNCTION
         END IF
      
         IF ISFALSE Flag.NoTitle THEN
            CON.STDOUT STRING$(79,"-")
      CON.STDOUT "  |Type    |       Used        |       Free        |       Total       | % Free"
      CON.STDOUT STRING$(79,"-")
      END IF
      
         FOR X = 65 TO 90     ' A --&amp;gt; Z
      sDrive = CHR$(X) + ":\" + $NUL
      dwDriveType = GetDriveTypeA(BYVAL STRPTR(sDrive))
      sDrive = LEFT$(sDrive,2)
      
      SELECT CASE CONST dwDriveType
      CASE %DRIVE_UNKNOWN
                  sDriveType = "Unknown"
      CASE %DRIVE_NO_ROOT_DIR
                  sDriveType = "NoRoot"
      CASE %DRIVE_REMOVABLE
      IF ISTRUE Flag.NoRemovable THEN ITERATE FOR
      sDriveType = "Removable"
      CASE %DRIVE_FIXED
      IF ISTRUE Flag.NoFixed THEN ITERATE FOR
      sDriveType = "Fixed"
      CASE %DRIVE_REMOTE
      IF ISTRUE Flag.NoRemote THEN ITERATE FOR
      sDriveType = "Remote"
      CASE %DRIVE_CDROM
      IF ISTRUE Flag.NoCDROM THEN ITERATE FOR
      sDriveType = "CD-ROM"
      CASE %DRIVE_RAMDISK
      IF ISTRUE Flag.NoRAMDISK THEN ITERATE FOR
      sDriveType = "RAM"
      CASE ELSE
      sDriveType = "Error"
      END SELECT
      
            IF INSTR(sDriveHide,sDrive) THEN ITERATE FOR
      
            IF dwDriveType &amp;lt;&amp;gt; %DRIVE_NO_ROOT_DIR THEN
      sDrivesProcessed += sDrive
               qDriveSize        = DISKSIZE(sDrive)
      qDriveFree        = DISKFREE(sDrive)
      qDriveUsed        = qDriveSize - qDriveFree
               cDrivePctFree     = (qDriveFree/qDriveSize)*100
      IF cDrivePctFree &amp;lt; 0 THEN cDrivePctFree = 0
               sMsg = LEFT$(sDrive,2) + " " + _
                      LSET$(sDriveType,9) + _
                      RSET$(FORMAT$(qDriveUsed,"#,"),19) + " " + _
                      RSET$(FORMAT$(qDriveFree,"#,"),19) + " " + _
                      RSET$(FORMAT$(qDriveSize,"#,"),19) + " " + _
                      RSET$(FORMAT$(cDrivePctFree,"##0.00"),6) + "%"
      CON.STDOUT sMsg
      ' Check drive free space for drives specified
      dwInStrPos = INSTR(sDriveReqSpace,sDrive)
      IF dwInStrPos THEN
      sRegExpMask = "[0-9.]+"
      REGEXPR sRegExpMask IN sDriveReqSpace AT (dwInStrPos + 2) TO dwRegExpPos,dwRegExpLen
                  cDriveReqSpace = VAL(MID$(sDriveReqSpace,dwRegExpPos,dwRegExpLen))
      IF MID$(sDriveReqSpace,dwRegExpPos+dwRegExpLen,1) = "%" THEN
                     IF cDrivePctFree &amp;lt; cDriveReqSpace THEN
                        FUNCTION = 1    ' Drive is under free space
      END IF
                  ELSE
      sWork = MID$(sDriveReqSpace,dwRegExpPos+dwRegExpLen,1)
      IF sWork = "K" THEN cDriveReqSpace *= %KB
      IF sWork = "M" THEN cDriveReqSpace *= %MB
      IF sWork = "G" THEN cDriveReqSpace *= %GB
      IF sWork = "T" THEN cDriveReqSpace *= %TB
      IF VERIFY(sWork,"KMGT") = 0 THEN sDriveReqSpace = CLIP$(MID sDriveReqSpace,dwRegExpPos+dwRegExpLen,1)
      IF qDriveFree &amp;lt; cDriveReqSpace THEN
                        FUNCTION = 1    ' Drive is under free space
      END IF
                  END IF
               END IF
            END IF
         NEXT X
      
      ' Make sure all requested drives were checked...return error if NOT
      IF LEN(REMOVE$(RETAIN$(sDriveReqSpace,ANY CHR$(65 TO 90)),ANY sDrivesProcessed)) THEN
            FUNCTION = 1
      END IF
      
         IF ISFALSE Flag.NoTitle THEN
            CON.STDOUT STRING$(79,"=")
      END IF
      END FUNCTION
      
      FUNCTION HelpString() AS STRING
         FUNCTION = $APP_ORIGINALFILENAME + "   Version: " + _
                    FORMAT$(%APP_MAJOR) + "." + _
                    FORMAT$(%APP_MINOR) + "." + _
                    FORMAT$(%APP_REVISION) + " " + _
      "(Build: " + FORMAT$(%APP_BUILD) + ")" + $CRLF + $CRLF + _
      "Usage:" + $CRLF + _
      "DrvSpMon [option] [option] [...] C:[##.##%|###,###,###,###,###][K|M|G|T] [...]" + $CRLF + $CRLF + _
      "Options: " + $CRLF + _
      "   /NO_TITLE      - No Title bars on output" + $CRLF + _
      "   /NO_REMOVABLE  - Don't list removable drives" + $CRLF + _
      "   /NO_FIXED      - Don't list fixed drives" + $CRLF + _
      "   /NO_REMOTE     - Don't list remote drives" + $CRLF + _
      "   /NO_CD-ROM     - Don't list CD-ROM drives" + $CRLF + _
      "   /NO_RAM-DISK   - Don't list RAM drives" + $CRLF + _
      "   /H[IDE] X:Y:Z: - Don't list " + $DQ + "X:" + $DQ + " " + $DQ + "Y:" + $DQ + " " + $DQ + "Z:" + $DQ + " drives" + $CRLF + _
      "   /TRAY"  + $CRLF + $CRLF + _
      "Example:" + $CRLF + _
      "   DrvSpMon /HIDE A:B: /NO_CD-ROM /NO_RAM-DISK C:10G" + $CRLF + $CRLF + _
      "Usage note: Don't hide a drive that appears in the list of drives to monitor"
      END FUNCTION
      DrvSpMon.zip

      Comment


      • #4
        Quick change to NOT display a dialog box if removable media drive doesn't have media inserted. i.e. no error for empty floppy drive or empty media reader.
        Code:
        ' Usage DrvSpMon /NO_TITLE /NO_REMOVABLE /NO_FIXED /NO_REMOTE /NO_CD-ROM /NO_RAM-DISK /TRAY /HIDE A:B:D: C:10% H:53,687,091,200
        ' Heavily inspired by: https://forum.powerbasic.com/forum/user-to-user-discussions/programming/775746-command-line-tool-for-checking-drive-space-using-pb-function-diskfree-and-disksize
        ' Other pieces of code and ideas from various posts on the PowerBASIC forums...Many Thanks to all who contribute!!!
        
        ' Version Scheme - Major,Minor,Revision,Build
        %APP_MAJOR            = 1
        %APP_MINOR            = 0
        %APP_REVISION         = 1
        %APP_BUILD            = 4
        $APP_VERSION          = "1.0.1 Build-00004"
        $APP_NAME             = "DrvSpMon"
        $APP_ORIGINALFILENAME = $APP_NAME + ".EXE"
        
        #BREAK         OFF
        #COMPILE       EXE
        #COMPILER      PBCC 6
        #CONSOLE       ON
        #DEBUG DISPLAY OFF
        #DEBUG ERROR   OFF
        #DIM           ALL
        #OPTIMIZE      SPEED
        #OPTION        VERSION5
        #REGISTER      NONE
        #RESOURCE      VERSIONINFO
        #RESOURCE      FILEFLAGS 0&
        #RESOURCE      FILEVERSION    %APP_MAJOR,%APP_MINOR,%APP_REVISION,%APP_BUILD
        #RESOURCE      PRODUCTVERSION %APP_MAJOR,%APP_MINOR,%APP_REVISION,%APP_BUILD
        #RESOURCE      STRINGINFO "0409", "04E4"
        #RESOURCE      VERSION$ "Author",           "Michael A. Rice"
        #RESOURCE      VERSION$ "Comments",         "Command line utility for monitoring drive free space"
        #RESOURCE      VERSION$ "CompanyName",      "None"
        #RESOURCE      VERSION$ "FileDescription",  "Main executable"
        #RESOURCE      VERSION$ "FileVersion",      $APP_VERSION
        #RESOURCE      VERSION$ "InternalName",     "Drive Space Monitor"
        #RESOURCE      VERSION$ "LegalCopyright",   "Copyright © None"
        #RESOURCE      VERSION$ "OriginalFilename", $APP_ORIGINALFILENAME
        #RESOURCE      VERSION$ "ProductName",      $APP_NAME
        #RESOURCE      VERSION$ "ProductVersion",   $APP_VERSION
        #RESOURCE      VERSION$ "Compiler",         "PBCC 6"
        #RESOURCE      MANIFEST,1,"Manifest.xml"    ' From José's includes
        #TOOLS         OFF
        
        %WIN32_LEAN_AND_MEAN = 1
        #INCLUDE ONCE "WIN32API.INC"                ' José Roca includes
        
        $SizeValidSuffix = "(K)|(M)|(G)|(T)|( )|$"
        
        %KB =      1024
        %MB = %KB * %KB
        %GB = %MB * %KB
        %TB = %GB * %KB
        
        #IF NOT %DEF(%FALSE)
           %FALSE = 0&
        #ENDIF
        
        #IF NOT %DEF(%TRUE)
           %TRUE = NOT %FALSE
        #ENDIF
        
        #IF NOT %DEF(%NULL)
           %NULL = 0???
        #ENDIF
        
        TYPE Flag_Type
           NoTitle     AS BIT * 1 IN BYTE
           NoRemovable AS BIT * 1
           NoFixed     AS BIT * 1
           NoRemote    AS BIT * 1
           NoCDROM     AS BIT * 1
           NoRAMDISK   AS BIT * 1
           Hide        AS BIT * 1
           Tray        AS BIT * 1
        END TYPE
        
        FUNCTION PBMAIN()
           LOCAL dwDriveType         AS DWORD
           LOCAL sDriveType          AS STRING
           LOCAL sDrive              AS STRING
           LOCAL sDrivesProcessed    AS STRING
           LOCAL sDriveHide          AS STRING
           LOCAL sDriveReqSpace      AS STRING
           LOCAL cDriveReqSpace      AS CURRENCY
           LOCAL qDriveSize          AS QUAD
           LOCAL qDriveFree          AS QUAD
           LOCAL qDriveUsed          AS QUAD
           LOCAL cDrivePctFree       AS CURRENCY
           LOCAL dwPrevValErrorMode  AS DWORD
           LOCAL sCommand            AS STRING
           LOCAL sMsg                AS STRING
           LOCAL dwWork              AS DWORD
           LOCAL sWork               AS STRING
           LOCAL dwInStrPos          AS DWORD
           LOCAL X                   AS LONG
           LOCAL sRegExpMask         AS STRING
           LOCAL dwRegExpLen         AS DWORD
           LOCAL dwRegExpPos         AS DWORD
           LOCAL Flag                AS Flag_Type
        
           IF INSTR(COMMAND$,"/?") THEN
              CON.STDOUT HelpString
              FUNCTION = 0
              EXIT FUNCTION
           END IF
        
           sCommand = UCASE$(COMMAND$)
        
           IF INSTR(sCommand,"/NO_TITLE") THEN
              Flag.NoTitle = %TRUE
              REPLACE "/NO_TITLE" WITH "" IN sCommand
           END IF
        
           IF INSTR(sCommand,"/NO_REMOVABLE") THEN
              Flag.NoRemovable = %TRUE
              REPLACE "/NO_REMOVABLE" WITH "" IN sCommand
           END IF
        
           IF INSTR(sCommand,"/NO_FIXED") THEN
              Flag.NoFixed = %TRUE
              REPLACE "/NO_FIXED" WITH "" IN sCommand
           END IF
        
           IF INSTR(sCommand,"/NO_REMOTE") THEN
              Flag.NoRemote = %TRUE
              REPLACE "/NO_REMOTE" WITH "" IN sCommand
           END IF
        
           IF INSTR(sCommand,"/NO_CD-ROM") THEN
              Flag.NoCDROM = %TRUE
              REPLACE "/NO_CD-ROM" WITH "" IN sCommand
           END IF
        
           IF INSTR(sCommand,"/NO_RAM-DISK") THEN
              Flag.NoRAMDISK = %TRUE
              REPLACE "/NO_RAM-DISK" WITH "" IN sCommand
           END IF
        
           dwInStrPos = MAX(INSTR(sCommand,"/HIDE "),INSTR(sCommand,"/H "))
           IF dwInStrPos THEN
              Flag.Hide = %TRUE
              REPLACE "/HIDE" WITH "" IN sCommand
              REPLACE "/H"    WITH "" IN sCommand
        
              ' Verify syntax  ---  Allow 1 to 25 drives to be hidden
              ' Build regular expression mask
              sRegExpMask = "[ ]+[A-Z]:"
              FOR X = 24 TO 1 STEP -1
                 sRegExpMask += "(" + REPEAT$(X,"[A-Z]:") + ")|"
              NEXT X
              sRegExpMask += "()( |$)"
        
              REGEXPR sRegExpMask IN sCommand AT dwInStrPos TO dwRegExpPos,dwRegExpLen
        
              IF dwRegExpPos THEN
                 sWork = MID$(sCommand,dwRegExpPos,dwRegExpLen)
                 dwWork = VERIFY(sWork," ")
                 sDriveHide = MID$(sWork,dwWork,(LEN(sWork)-dwWork)+1)
                 sCommand = CLIP$(MID sCommand,dwRegExpPos,dwRegExpLen)
              ELSE
                 CON.STDOUT HelpString
                 FUNCTION = 1
                 EXIT FUNCTION
              END IF
           END IF
        
           IF INSTR(sCommand,"/TRAY") THEN
              Flag.Tray = %TRUE
              REPLACE "/TRAY" WITH "" IN sCommand
           END IF
        
           sCommand = REMOVE$(sCommand,ANY ",")
        
           ' Verify syntax  ---  Allow up to 26 drives to have free space low amount specified
           sRegExpMask = "([A-Z]:[0-9]+" + $SizeValidSuffix + ")|([A-Z]:[0-9.]+%)"
           RESET sWork
           ' Search for drives and store them in sWork if any found
           FOR X = 1 TO 26
              REGEXPR sRegExpMask IN sCommand TO dwRegExpPos,dwRegExpLen
              IF dwRegExpPos THEN
                 sWork += MID$(sCommand,dwRegExpPos,dwRegExpLen) + ","       ' Put "," between each
                 sCommand = CLIP$(MID sCommand,dwRegExpPos,dwRegExpLen)
              ELSE
                 EXIT FOR
              END IF
           NEXT X
        
           IF LEN(sWORK) THEN
              sWork = CLIP$(RIGHT sWork,1)                                   ' Delete the extra trailing ","
              sDriveReqSpace = REMOVE$(sWork,ANY " ")
              sCommand = TRIM$(sCommand)
           ELSEIF TRIM$(sCommand) = "" THEN
              sCommand = ""
              EXIT IF      ' Nothing else was specified on the command line...nothing to do here!
           ELSE
              CON.STDOUT HelpString
              FUNCTION = 1
              EXIT FUNCTION
           END IF
        
           IF sCommand <> "" THEN
              CON.STDOUT "Error: unexpected input " + $DQ + sCommand + $DQ
              FUNCTION = 1
              EXIT FUNCTION
           END IF
        
           IF ISFALSE Flag.NoTitle THEN
              CON.STDOUT STRING$(79,"-")
              CON.STDOUT "  |Type    |       Used        |       Free        |       Total       | % Free"
              CON.STDOUT STRING$(79,"-")
           END IF
        
           ' Stop windows from displaying error for empty removable drives
           dwPrevValErrorMode = SetErrorMode(%SEM_FAILCRITICALERRORS)
        
           FOR X = 65 TO 90     ' A --> Z
              sDrive = CHR$(X) + ":\" + $NUL
              dwDriveType = GetDriveTypeA(BYVAL STRPTR(sDrive))
              sDrive = LEFT$(sDrive,2)
        
              SELECT CASE CONST dwDriveType
                 CASE %DRIVE_UNKNOWN
                    sDriveType = "Unknown"
                 CASE %DRIVE_NO_ROOT_DIR
                    sDriveType = "NoRoot"
                 CASE %DRIVE_REMOVABLE
                    IF ISTRUE Flag.NoRemovable THEN ITERATE FOR
                    sDriveType = "Removable"
                 CASE %DRIVE_FIXED
                    IF ISTRUE Flag.NoFixed THEN ITERATE FOR
                    sDriveType = "Fixed"
                 CASE %DRIVE_REMOTE
                    IF ISTRUE Flag.NoRemote THEN ITERATE FOR
                    sDriveType = "Remote"
                 CASE %DRIVE_CDROM
                    IF ISTRUE Flag.NoCDROM THEN ITERATE FOR
                    sDriveType = "CD-ROM"
                 CASE %DRIVE_RAMDISK
                    IF ISTRUE Flag.NoRAMDISK THEN ITERATE FOR
                    sDriveType = "RAM"
                 CASE ELSE
                    sDriveType = "Error"
              END SELECT
        
              IF INSTR(sDriveHide,sDrive) THEN ITERATE FOR
        
              IF dwDriveType <> %DRIVE_NO_ROOT_DIR THEN
                 sDrivesProcessed += sDrive
                 qDriveSize        = DISKSIZE(sDrive)
                 qDriveFree        = DISKFREE(sDrive)
                 qDriveUsed        = qDriveSize - qDriveFree
                 cDrivePctFree     = (qDriveFree/qDriveSize)*100
                 IF cDrivePctFree < 0 THEN cDrivePctFree = 0
                 sMsg = LEFT$(sDrive,2) + " " + _
                        LSET$(sDriveType,9) + _
                        RSET$(FORMAT$(qDriveUsed,"#,"),19) + " " + _
                        RSET$(FORMAT$(qDriveFree,"#,"),19) + " " + _
                        RSET$(FORMAT$(qDriveSize,"#,"),19) + " " + _
                        RSET$(FORMAT$(cDrivePctFree,"##0.00"),6) + "%"
                 CON.STDOUT sMsg
                 ' Check drive free space for drives specified
                 dwInStrPos = INSTR(sDriveReqSpace,sDrive)
                 IF dwInStrPos THEN
                    sRegExpMask = "[0-9.]+"
                    REGEXPR sRegExpMask IN sDriveReqSpace AT (dwInStrPos + 2) TO dwRegExpPos,dwRegExpLen
                    cDriveReqSpace = VAL(MID$(sDriveReqSpace,dwRegExpPos,dwRegExpLen))
                    IF MID$(sDriveReqSpace,dwRegExpPos+dwRegExpLen,1) = "%" THEN
                       IF cDrivePctFree < cDriveReqSpace THEN
                          FUNCTION = 1    ' Drive is under free space
                       END IF
                    ELSE
                       sWork = MID$(sDriveReqSpace,dwRegExpPos+dwRegExpLen,1)
                       IF sWork = "K" THEN cDriveReqSpace *= %KB
                       IF sWork = "M" THEN cDriveReqSpace *= %MB
                       IF sWork = "G" THEN cDriveReqSpace *= %GB
                       IF sWork = "T" THEN cDriveReqSpace *= %TB
                       IF VERIFY(sWork,"KMGT") = 0 THEN sDriveReqSpace = CLIP$(MID sDriveReqSpace,dwRegExpPos+dwRegExpLen,1)
                       IF qDriveFree < cDriveReqSpace THEN
                          FUNCTION = 1    ' Drive is under free space
                       END IF
                    END IF
                 END IF
              END IF
           NEXT X
        
           ' Reset previous mode
           SetErrorMode(dwPrevValErrorMode)
        
           ' Make sure all requested drives were checked...return error if NOT
           IF LEN(REMOVE$(RETAIN$(sDriveReqSpace,ANY CHR$(65 TO 90)),ANY sDrivesProcessed)) THEN
              FUNCTION = 1
           END IF
        
           IF ISFALSE Flag.NoTitle THEN
              CON.STDOUT STRING$(79,"=")
           END IF
        END FUNCTION
        
        FUNCTION HelpString() AS STRING
           FUNCTION = $APP_ORIGINALFILENAME + "   Version: " + _
                      FORMAT$(%APP_MAJOR) + "." + _
                      FORMAT$(%APP_MINOR) + "." + _
                      FORMAT$(%APP_REVISION) + " " + _
                      "(Build: " + FORMAT$(%APP_BUILD) + ")" + $CRLF + $CRLF + _
                      "Usage:" + $CRLF + _
                      "DrvSpMon [option] [option] [...] C:[##.##%|###,###,###,###,###][K|M|G|T] [...]" + $CRLF + $CRLF + _
                      "Options: " + $CRLF + _
                      "   /NO_TITLE      - No Title bars on output" + $CRLF + _
                      "   /NO_REMOVABLE  - Don't list removable drives" + $CRLF + _
                      "   /NO_FIXED      - Don't list fixed drives" + $CRLF + _
                      "   /NO_REMOTE     - Don't list remote drives" + $CRLF + _
                      "   /NO_CD-ROM     - Don't list CD-ROM drives" + $CRLF + _
                      "   /NO_RAM-DISK   - Don't list RAM drives" + $CRLF + _
                      "   /H[IDE] X:Y:Z: - Don't list " + $DQ + "X:" + $DQ + " " + $DQ + "Y:" + $DQ + " " + $DQ + "Z:" + $DQ + " drives" + $CRLF + _
                      "   /TRAY"  + $CRLF + $CRLF + _
                      "Example:" + $CRLF + _
                      "   DrvSpMon /HIDE A:B: /NO_CD-ROM /NO_RAM-DISK C:10G" + $CRLF + $CRLF + _
                      "Usage note: Don't hide a drive that appears in the list of drives to monitor"
        END FUNCTION
        DrvSpMon.zip

        Comment

        Working...
        X