Advertisement
vartik

df.bat

Oct 10th, 2020 (edited)
941
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @echo off
  2. rem Version: 1.6
  3. rem Author: Branislav Vartik
  4.  
  5. setlocal enableextensions enabledelayedexpansion
  6.  
  7. rem Prepare CR+LF characters (LF-LF-CR)
  8. rem https://stackoverflow.com/questions/67607068/remove-last-new-line-character-of-text-file-in-batch
  9. For /F %%G In ('Copy /Z "%~f0" NUL') Do Set "cr=%%G" & (Set lf=% 0x0A %)
  10.  
  11. rem Detect broken (EOL LF-LF-CR) and/or old (honors 8.3 path instead of volumeid) fsutil
  12. set fsutilbroken=0
  13. set fsutilold=1
  14. for /f "tokens=*" %%g in ('fsutil ^| findstr /b /c:"-" /c:"transaction"') do (
  15.     set line=%%g
  16.     if [!line:~-1!] == [!cr!] set fsutilbroken=1
  17.     echo !line! | findstr /b /c:"transaction" >nul && set fsutilold=0
  18. )
  19. rem echo DEBUG: fsutilbroken=!fsutilbroken! fsutilold=!fsutilold!
  20.  
  21. echo Filesystem 1K-blocks   Used    Available   Capacity    Mounted on
  22.  
  23. rem Enumerate volume IDs and mountpoints
  24. for /f "tokens=*" %%a in ('mountvol ^| find "\"') do (
  25. rem echo DEBUG: %%a
  26.     set line=%%a
  27.     rem Is it volume ID?
  28.     if "!line:~0,2!"=="\\" (
  29. rem     echo DEBUG: volume id !line!
  30.         set volumeid=!line!
  31.     ) else (
  32.         rem Is it mounpoint?
  33.         echo !line! | find "\" >nul && (
  34. rem         echo DEBUG: mountpoint !line!
  35.             set mountpoint=!line!
  36.             set total=
  37.             set used=
  38.             set avail=
  39.             set volumename=
  40.             set fsname=
  41.             rem Get diskfree - broken fsutil works only with mountpoint, non-broken fsutil works better with volumeid
  42.             set volume=!volumeid!
  43.             if [!fsutilold!] == [1] for %%b in ("!mountpoint!") do set volume=%%~sb
  44. rem             echo DEBUG: volumeid=!volume! mountpoint=!mountpoint! volume=!volume!
  45.             for /f "tokens=1,2 delims=:" %%b in ('fsutil volume diskfree !volume! ^& fsutil fsinfo volumeinfo !volume!') do (
  46. rem             echo DEBUG: %%b:%%c
  47.                 set name=%%b
  48.                 set value=%%c
  49.                 if [!fsutilbroken!] == [1] (
  50.                     if not [!value!] == [] (
  51.                         set value=!value:~0,-1!
  52.                     ) else (
  53.                         if not [!name!] == [] set name=!name:~0,-1!
  54.                     )
  55.                 )
  56. rem             echo DEBUG: !name!:!value!
  57.                 if not "!value!"=="" (
  58.                     rem Space off (thousand separator)
  59.                     echo !value! | find " " >nul && set name=!name: =!
  60.                     rem Comma off (alternative thousand separator)
  61.                     set value=!value: =!
  62.                     echo !value! | find "," >nul && set value=!value:,=!
  63.                     rem NBSP off
  64.                     echo !value! | find "˙" >nul && set value=!value:˙=!
  65.                     echo !value! | find "(" >nul && call :removebracketinfo "!value!"
  66.                     if "!name!" == "Totalbytes" set total=!value:~,-3!
  67.                     if "!name!" == "Totalquotafreebytes" set avail=!value:~,-3!
  68.                     if "!name!" == "Total#ofbytes" set total=!value:~,-3!
  69.                     if "!name!" == "Total#ofavailfreebytes" set avail=!value:~,-3!
  70.                     if "!name!" == "Usedbytes" set used=!value:~,-3!
  71.                     if "!name!" == "VolumeName" set volumename=!value!
  72.                     if "!name!" == "FileSystemName" set fsname=!value!
  73.                 )
  74.             )
  75.             if "!avail!"=="" set avail=0
  76. rem         echo DEBUG: total:!total! avail:!avail! volumename:!volumename! fsname:!fsname!
  77.             rem If media is inserted, calculate and print
  78.             if not "!total!"=="" (
  79.                 if [!volumename!] == [] set volumename=!fsname!
  80.                 rem Check if total is 2TB+
  81.                 set totalmb=!total:~,-3!
  82.                 if !totalmb! geq 2147483 (
  83.                     rem Calculate with MB if over 2TB
  84.                     if [!used!] == [] (
  85.                         rem If used does not exist, calculate availmb and used(mb)
  86.                         set availmb=!avail:~,-3!
  87.                         set /a usedmb="!totalmb!-!availmb!"
  88.                         set used=!usedmb!000
  89.                     ) else (
  90.                         rem If used exists, calculate usedmb
  91.                         set usedmb=!used:~,-3!
  92.                         rem Recalculate avail(mb) if negative
  93.                         if !avail! lss 0 (
  94.                             set /a availmb="!totalmb!-!usedmb!"
  95.                             set avail=!avail!000
  96.                         )
  97.                     )
  98.                     set /a usedpercent="!usedmb!/(!totalmb!/100)"
  99.                 ) else (
  100.                     rem Calculate with KB
  101.                     if [!used!] == [] (
  102.                         set /a used="!total!-!avail!"
  103.                     rem Recalculate avail if negative and used defined
  104.                     ) else if !avail! lss 0 set /a avail="!total!-!used!
  105.                     set /a usedpercent="!used!/(!total!/100)"
  106.                 )
  107.                 set msg=!volumename!    !total! !used!  !avail! !usedpercent!%% !mountpoint!
  108.                 echo !msg!
  109.                 rem If parameter is given, send to syslog server
  110.                 if not [%1] == [] (
  111. rem                 set msg=!msg:\=\x5b!
  112. rem                 %~dp0SyslogGen.exe -t:%1 -f:1 -s:6 -q -m:"logger: !msg!"
  113.                     %~dp0logger.exe >nul -l %1 -p user.info -t logger "!msg!
  114.                     rem https://www.adiscon.com/adiscon-tools/an-unix-like-logger-for-windows/
  115.                 )
  116.             )
  117.         )
  118.     )
  119. )
  120. goto :eof
  121.  
  122. rem https://stackoverflow.com/questions/49003738/in-cmd-substitution-how-to-remove-substring-which-starts-from-specific-characte
  123. :removebracketinfo
  124. set input=%~1
  125. set value=%input:(=&:%
  126. goto :eof
  127.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement