Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @echo off
- rem Version: 1.6
- rem Author: Branislav Vartik
- setlocal enableextensions enabledelayedexpansion
- rem Prepare CR+LF characters (LF-LF-CR)
- rem https://stackoverflow.com/questions/67607068/remove-last-new-line-character-of-text-file-in-batch
- For /F %%G In ('Copy /Z "%~f0" NUL') Do Set "cr=%%G" & (Set lf=% 0x0A %)
- rem Detect broken (EOL LF-LF-CR) and/or old (honors 8.3 path instead of volumeid) fsutil
- set fsutilbroken=0
- set fsutilold=1
- for /f "tokens=*" %%g in ('fsutil ^| findstr /b /c:"-" /c:"transaction"') do (
- set line=%%g
- if [!line:~-1!] == [!cr!] set fsutilbroken=1
- echo !line! | findstr /b /c:"transaction" >nul && set fsutilold=0
- )
- rem echo DEBUG: fsutilbroken=!fsutilbroken! fsutilold=!fsutilold!
- echo Filesystem 1K-blocks Used Available Capacity Mounted on
- rem Enumerate volume IDs and mountpoints
- for /f "tokens=*" %%a in ('mountvol ^| find "\"') do (
- rem echo DEBUG: %%a
- set line=%%a
- rem Is it volume ID?
- if "!line:~0,2!"=="\\" (
- rem echo DEBUG: volume id !line!
- set volumeid=!line!
- ) else (
- rem Is it mounpoint?
- echo !line! | find "\" >nul && (
- rem echo DEBUG: mountpoint !line!
- set mountpoint=!line!
- set total=
- set used=
- set avail=
- set volumename=
- set fsname=
- rem Get diskfree - broken fsutil works only with mountpoint, non-broken fsutil works better with volumeid
- set volume=!volumeid!
- if [!fsutilold!] == [1] for %%b in ("!mountpoint!") do set volume=%%~sb
- rem echo DEBUG: volumeid=!volume! mountpoint=!mountpoint! volume=!volume!
- for /f "tokens=1,2 delims=:" %%b in ('fsutil volume diskfree !volume! ^& fsutil fsinfo volumeinfo !volume!') do (
- rem echo DEBUG: %%b:%%c
- set name=%%b
- set value=%%c
- if [!fsutilbroken!] == [1] (
- if not [!value!] == [] (
- set value=!value:~0,-1!
- ) else (
- if not [!name!] == [] set name=!name:~0,-1!
- )
- )
- rem echo DEBUG: !name!:!value!
- if not "!value!"=="" (
- rem Space off (thousand separator)
- echo !value! | find " " >nul && set name=!name: =!
- rem Comma off (alternative thousand separator)
- set value=!value: =!
- echo !value! | find "," >nul && set value=!value:,=!
- rem NBSP off
- echo !value! | find "˙" >nul && set value=!value:˙=!
- echo !value! | find "(" >nul && call :removebracketinfo "!value!"
- if "!name!" == "Totalbytes" set total=!value:~,-3!
- if "!name!" == "Totalquotafreebytes" set avail=!value:~,-3!
- if "!name!" == "Total#ofbytes" set total=!value:~,-3!
- if "!name!" == "Total#ofavailfreebytes" set avail=!value:~,-3!
- if "!name!" == "Usedbytes" set used=!value:~,-3!
- if "!name!" == "VolumeName" set volumename=!value!
- if "!name!" == "FileSystemName" set fsname=!value!
- )
- )
- if "!avail!"=="" set avail=0
- rem echo DEBUG: total:!total! avail:!avail! volumename:!volumename! fsname:!fsname!
- rem If media is inserted, calculate and print
- if not "!total!"=="" (
- if [!volumename!] == [] set volumename=!fsname!
- rem Check if total is 2TB+
- set totalmb=!total:~,-3!
- if !totalmb! geq 2147483 (
- rem Calculate with MB if over 2TB
- if [!used!] == [] (
- rem If used does not exist, calculate availmb and used(mb)
- set availmb=!avail:~,-3!
- set /a usedmb="!totalmb!-!availmb!"
- set used=!usedmb!000
- ) else (
- rem If used exists, calculate usedmb
- set usedmb=!used:~,-3!
- rem Recalculate avail(mb) if negative
- if !avail! lss 0 (
- set /a availmb="!totalmb!-!usedmb!"
- set avail=!avail!000
- )
- )
- set /a usedpercent="!usedmb!/(!totalmb!/100)"
- ) else (
- rem Calculate with KB
- if [!used!] == [] (
- set /a used="!total!-!avail!"
- rem Recalculate avail if negative and used defined
- ) else if !avail! lss 0 set /a avail="!total!-!used!
- set /a usedpercent="!used!/(!total!/100)"
- )
- set msg=!volumename! !total! !used! !avail! !usedpercent!%% !mountpoint!
- echo !msg!
- rem If parameter is given, send to syslog server
- if not [%1] == [] (
- rem set msg=!msg:\=\x5b!
- rem %~dp0SyslogGen.exe -t:%1 -f:1 -s:6 -q -m:"logger: !msg!"
- %~dp0logger.exe >nul -l %1 -p user.info -t logger "!msg!
- rem https://www.adiscon.com/adiscon-tools/an-unix-like-logger-for-windows/
- )
- )
- )
- )
- )
- goto :eof
- rem https://stackoverflow.com/questions/49003738/in-cmd-substitution-how-to-remove-substring-which-starts-from-specific-characte
- :removebracketinfo
- set input=%~1
- set value=%input:(=&:%
- goto :eof
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement