有没有人知道如何只显示小数点后的2个字符。
实施例: 19.123948493114变成19.12
不能使用%变量:〜5%因为小数点前的数字有所改变所以有时它会是19次,所以它将是123451213.12345123
两者都需要只显示小数点后的2个字符
- =编辑= - 这是我当前的脚本。
@ECHO OFF
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION
CLS
FOR /F "tokens=1*" %%A IN ('fsutil fsinfo drives') DO SET "Drives=%%B"
FOR %%A IN (%Drives%) DO fsutil fsinfo statistics %%A |FINDSTR /I /C:"The FSUTIL utility requires a local NTFS volume." >nul || CALL:GetDriveInfo %%A
EXIT /B
:GetDriveInfo
SET "CurrentDrive=%~1"
SET "CurrentDrive=%CurrentDrive:\=%
FOR /F "tokens=7 delims= " %%A IN ('fsutil volume diskfree %~1 ^|FINDSTR /C:"Total # of free bytes"') DO Call:GetDriveInfo-Convert "CurrentDrive-FreeSpace" "%%A"
FOR /F "tokens=6 delims= " %%A IN ('fsutil volume diskfree %~1 ^|FINDSTR /C:"Total # of bytes"') DO CALL:GetDriveInfo-Convert "CurrentDrive-TotalSpace" "%%A"
FOR /F %%A IN ('powershell %CurrentDrive-TotalSpace% - %CurrentDrive-FreeSpace%') DO Set "CurrentDrive-UsedSpace=%%A"
FOR /F "tokens=1-2 delims=." %%A IN ('powershell %CurrentDrive-UsedSpace% / %CurrentDrive-TotalSpace% * 100') DO (
SET "CurrentDrive-UsedSpace-Percent1=%%A"
SET "CurrentDrive-UsedSpace-Percent2=%%B"
SET "CurrentDrive-UsedSpace-Percent2=!CurrentDrive-UsedSpace-Percent2:~0,1!"
SET "CurrentDrive-UsedSpace-Percent=!CurrentDrive-UsedSpace-Percent1!.!CurrentDrive-UsedSpace-Percent2!"
)
FOR /F "tokens=1-2 delims=." %%A IN ('powershell %CurrentDrive-FreeSpace% / %CurrentDrive-TotalSpace% * 100') DO (
SET "CurrentDrive-FreeSpace-Percent1=%%A"
SET "CurrentDrive-FreeSpace-Percent2=%%B"
SET "CurrentDrive-FreeSpace-Percent2=!CurrentDrive-FreeSpace-Percent2:~0,1!"
SET "CurrentDrive-FreeSpace-Percent=!CurrentDrive-FreeSpace-Percent1!.!CurrentDrive-FreeSpace-Percent2!"
)
ECHO Drive Information For %CurrentDrive%
ECHO/ Used Space: %CurrentDrive-UsedSpace% (%CurrentDrive-UsedSpace-Percent%%%)
ECHO/ Free Space: %CurrentDrive-FreeSpace% (%CurrentDrive-FreeSpace-Percent%%%)
ECHO/ Total Space: %CurrentDrive-TotalSpace%
ECHO/
EXIT /B
:GetDriveInfo-Convert
FOR /F %%A IN ('powershell %~2 / 1024 / 1024 / 1024') DO SET "CurrentDrive_Temp=%%A"
FOR /F "Tokens=1-2 delims=." %%A IN ("%CurrentDrive_Temp%") DO SET "CurrentDrive_Temp1=%%A" & SET "CurrentDrive_Temp2=%%B"
SET "CurrentDrive_Temp3=%CurrentDrive_Temp2:~0,2%"
:: Rounds Up to get a more accurate number. Used to counter the lost bytes.
IF "%CurrentDrive_Temp2:~2,1%" GEQ "5" SET /A "CurrentDrive_Temp3+=1"
IF "%CurrentDrive_Temp3%"=="100" SET "CurrentDrive_Temp3=00" & SET /A CurrentDrive_Temp1+=1"
SET "%~1=%CurrentDrive_Temp1%.%CurrentDrive_Temp3%"
EXIT /B
问题? 其中一个驱动器毫无理由似乎不想切断。
我的结果:
Drive Information For C:
Used Space: 103.21 (86.5%)
Free Space: 16.04 (13.4%)
Total Space: 119.25
Drive Information For D:
Used Space: 481.37 (57.8%)
Free Space: 350.14 (42.1%)
Total Space: 831.51
Drive Information For E:
Used Space: 530.02 (99.7%)
Free Space: 1.49 (0.2%)
Total Space: 531.51
Drive Information For F:
Used Space: 86.4 (43.2%)
Free Space: 113.60 (56.8%)
Total Space: 200.00
Drive Information For G:
Used Space: 0.129999999999995 (0.0%)
Free Space: 199.87 (99.9%)
Total Space: 200.00
Drive Information For I:
Used Space: 86.83 (9.3%)
Free Space: 844.68 (90.6%)
Total Space: 931.51
Drive Information For J:
Used Space: 699.09 (75.0%)
Free Space: 232.39 (24.9%)
Total Space: 931.48
Drive Information For Z:
Used Space: 11.06 (22.1%)
Free Space: 38.94 (77.8%)
Total Space: 50.00
我的缩短脚本调试文件。我切断了C:驱动器的整个功能以及G:驱动器之后的所有功能,因为这是问题的驱动器,我不能发布超过30k字符xD
D:\Desktop>SETLOCAL ENABLEEXTENSIONS
D:\Desktop>SETLOCAL ENABLEDELAYEDEXPANSION
D:\Desktop>CLS
D:\Desktop>FOR /F "tokens=1*" %A IN ('fsutil fsinfo drives') DO SET "Drives=%B"
D:\Desktop>SET "Drives=C:\ D:\ E:\ F:\ G:\ H:\ I:\ J:\ K:\ L:\ X:\ Y:\ Z:\ "
D:\Desktop>FOR %A IN (C:\ D:\ E:\ F:\ G:\ H:\ I:\ J:\ K:\ L:\ X:\ Y:\ Z:\) DO fsutil fsinfo statistics %A | FINDSTR /I /C:"The FSUTIL utility requires a local NTFS volume." 1>nul || CALL:GetDriveInfo %A
D:\Desktop>fsutil fsinfo statistics D:\ | FINDSTR /I /C:"The FSUTIL utility requires a local NTFS volume." 1>nul || CALL:GetDriveInfo D:\
D:\Desktop>SET "CurrentDrive=D:\"
D:\Desktop>SET "CurrentDrive=D:
D:\Desktop>FOR /F "tokens=7 delims= " %A IN ('fsutil volume diskfree D:\ |FINDSTR /C:"Total # of free bytes"') DO Call:GetDriveInfo-Convert "CurrentDrive-FreeSpace" "%A"
D:\Desktop>Call:GetDriveInfo-Convert "CurrentDrive-FreeSpace" "375962980352"
D:\Desktop>FOR /F %A IN ('powershell 375962980352 / 1024 / 1024 / 1024') DO SET "CurrentDrive_Temp=%A"
D:\Desktop>SET "CurrentDrive_Temp=350.142810821533"
D:\Desktop>FOR /F "Tokens=1-2 delims=." %A IN ("350.142810821533") DO SET "CurrentDrive_Temp1=%A" & SET "CurrentDrive_Temp2=%B"
D:\Desktop>SET "CurrentDrive_Temp1=350" & SET "CurrentDrive_Temp2=142810821533"
D:\Desktop>SET "CurrentDrive_Temp3=14"
D:\Desktop>IF "2" GEQ "5" SET /A "CurrentDrive_Temp3+=1"
D:\Desktop>IF "14" == "100" SET "CurrentDrive_Temp3=00" & SET /A CurrentDrive_Temp1+=1"
D:\Desktop>SET "CurrentDrive-FreeSpace=350.14"
D:\Desktop>EXIT /B
D:\Desktop>FOR /F "tokens=6 delims= " %A IN ('fsutil volume diskfree D:\ |FINDSTR /C:"Total # of bytes"') DO CALL:GetDriveInfo-Convert "CurrentDrive-TotalSpace" "%A"
D:\Desktop>CALL:GetDriveInfo-Convert "CurrentDrive-TotalSpace" "892827856896"
D:\Desktop>FOR /F %A IN ('powershell 892827856896 / 1024 / 1024 / 1024') DO SET "CurrentDrive_Temp=%A"
D:\Desktop>SET "CurrentDrive_Temp=831.510738372803"
D:\Desktop>FOR /F "Tokens=1-2 delims=." %A IN ("831.510738372803") DO SET "CurrentDrive_Temp1=%A" & SET "CurrentDrive_Temp2=%B"
D:\Desktop>SET "CurrentDrive_Temp1=831" & SET "CurrentDrive_Temp2=510738372803"
D:\Desktop>SET "CurrentDrive_Temp3=51"
D:\Desktop>IF "0" GEQ "5" SET /A "CurrentDrive_Temp3+=1"
D:\Desktop>IF "51" == "100" SET "CurrentDrive_Temp3=00" & SET /A CurrentDrive_Temp1+=1"
D:\Desktop>SET "CurrentDrive-TotalSpace=831.51"
D:\Desktop>EXIT /B
D:\Desktop>FOR /F %A IN ('powershell 831.51 - 350.14') DO Set "CurrentDrive-UsedSpace=%A"
D:\Desktop>Set "CurrentDrive-UsedSpace=481.37"
D:\Desktop>FOR /F "tokens=1-2 delims=." %A IN ('powershell 481.37 / 831.51 * 100') DO (
SET "CurrentDrive-UsedSpace-Percent1=%A"
SET "CurrentDrive-UsedSpace-Percent2=%B"
SET "CurrentDrive-UsedSpace-Percent2=!CurrentDrive-UsedSpace-Percent2:~0,1!"
SET "CurrentDrive-UsedSpace-Percent=!CurrentDrive-UsedSpace-Percent1!.!CurrentDrive-UsedSpace-Percent2!"
)
D:\Desktop>(
SET "CurrentDrive-UsedSpace-Percent1=57"
SET "CurrentDrive-UsedSpace-Percent2=8910656516458"
SET "CurrentDrive-UsedSpace-Percent2=!CurrentDrive-UsedSpace-Percent2:~0,1!"
SET "CurrentDrive-UsedSpace-Percent=!CurrentDrive-UsedSpace-Percent1!.!CurrentDrive-UsedSpace-Percent2!"
)
D:\Desktop>FOR /F "tokens=1-2 delims=." %A IN ('powershell 350.14 / 831.51 * 100') DO (
SET "CurrentDrive-FreeSpace-Percent1=%A"
SET "CurrentDrive-FreeSpace-Percent2=%B"
SET "CurrentDrive-FreeSpace-Percent2=!CurrentDrive-FreeSpace-Percent2:~0,1!"
SET "CurrentDrive-FreeSpace-Percent=!CurrentDrive-FreeSpace-Percent1!.!CurrentDrive-FreeSpace-Percent2!"
)
D:\Desktop>(
SET "CurrentDrive-FreeSpace-Percent1=42"
SET "CurrentDrive-FreeSpace-Percent2=1089343483542"
SET "CurrentDrive-FreeSpace-Percent2=!CurrentDrive-FreeSpace-Percent2:~0,1!"
SET "CurrentDrive-FreeSpace-Percent=!CurrentDrive-FreeSpace-Percent1!.!CurrentDrive-FreeSpace-Percent2!"
)
D:\Desktop>ECHO Drive Information For D:
Drive Information For D:
D:\Desktop>ECHO/ Used Space: 481.37 (57.8%)
Used Space: 481.37 (57.8%)
D:\Desktop>ECHO/ Free Space: 350.14 (42.1%)
Free Space: 350.14 (42.1%)
D:\Desktop>ECHO/ Total Space: 831.51
Total Space: 831.51
D:\Desktop>ECHO/
D:\Desktop>EXIT /B
D:\Desktop>fsutil fsinfo statistics E:\ | FINDSTR /I /C:"The FSUTIL utility requires a local NTFS volume." 1>nul || CALL:GetDriveInfo E:\
D:\Desktop>SET "CurrentDrive=E:\"
D:\Desktop>SET "CurrentDrive=E:
D:\Desktop>FOR /F "tokens=7 delims= " %A IN ('fsutil volume diskfree E:\ |FINDSTR /C:"Total # of free bytes"') DO Call:GetDriveInfo-Convert "CurrentDrive-FreeSpace" "%A"
D:\Desktop>Call:GetDriveInfo-Convert "CurrentDrive-FreeSpace" "1599143936"
D:\Desktop>FOR /F %A IN ('powershell 1599143936 / 1024 / 1024 / 1024') DO SET "CurrentDrive_Temp=%A"
D:\Desktop>SET "CurrentDrive_Temp=1.48931884765625"
D:\Desktop>FOR /F "Tokens=1-2 delims=." %A IN ("1.48931884765625") DO SET "CurrentDrive_Temp1=%A" & SET "CurrentDrive_Temp2=%B"
D:\Desktop>SET "CurrentDrive_Temp1=1" & SET "CurrentDrive_Temp2=48931884765625"
D:\Desktop>SET "CurrentDrive_Temp3=48"
D:\Desktop>IF "9" GEQ "5" SET /A "CurrentDrive_Temp3+=1"
D:\Desktop>IF "49" == "100" SET "CurrentDrive_Temp3=00" & SET /A CurrentDrive_Temp1+=1"
D:\Desktop>SET "CurrentDrive-FreeSpace=1.49"
D:\Desktop>EXIT /B
D:\Desktop>FOR /F "tokens=6 delims= " %A IN ('fsutil volume diskfree E:\ |FINDSTR /C:"Total # of bytes"') DO CALL:GetDriveInfo-Convert "CurrentDrive-TotalSpace" "%A"
D:\Desktop>CALL:GetDriveInfo-Convert "CurrentDrive-TotalSpace" "570704261120"
D:\Desktop>FOR /F %A IN ('powershell 570704261120 / 1024 / 1024 / 1024') DO SET "CurrentDrive_Temp=%A"
D:\Desktop>SET "CurrentDrive_Temp=531.509761810303"
D:\Desktop>FOR /F "Tokens=1-2 delims=." %A IN ("531.509761810303") DO SET "CurrentDrive_Temp1=%A" & SET "CurrentDrive_Temp2=%B"
D:\Desktop>SET "CurrentDrive_Temp1=531" & SET "CurrentDrive_Temp2=509761810303"
D:\Desktop>SET "CurrentDrive_Temp3=50"
D:\Desktop>IF "9" GEQ "5" SET /A "CurrentDrive_Temp3+=1"
D:\Desktop>IF "51" == "100" SET "CurrentDrive_Temp3=00" & SET /A CurrentDrive_Temp1+=1"
D:\Desktop>SET "CurrentDrive-TotalSpace=531.51"
D:\Desktop>EXIT /B
D:\Desktop>FOR /F %A IN ('powershell 531.51 - 1.49') DO Set "CurrentDrive-UsedSpace=%A"
D:\Desktop>Set "CurrentDrive-UsedSpace=530.02"
D:\Desktop>FOR /F "tokens=1-2 delims=." %A IN ('powershell 530.02 / 531.51 * 100') DO (
SET "CurrentDrive-UsedSpace-Percent1=%A"
SET "CurrentDrive-UsedSpace-Percent2=%B"
SET "CurrentDrive-UsedSpace-Percent2=!CurrentDrive-UsedSpace-Percent2:~0,1!"
SET "CurrentDrive-UsedSpace-Percent=!CurrentDrive-UsedSpace-Percent1!.!CurrentDrive-UsedSpace-Percent2!"
)
D:\Desktop>(
SET "CurrentDrive-UsedSpace-Percent1=99"
SET "CurrentDrive-UsedSpace-Percent2=7196666102237"
SET "CurrentDrive-UsedSpace-Percent2=!CurrentDrive-UsedSpace-Percent2:~0,1!"
SET "CurrentDrive-UsedSpace-Percent=!CurrentDrive-UsedSpace-Percent1!.!CurrentDrive-UsedSpace-Percent2!"
)
D:\Desktop>FOR /F "tokens=1-2 delims=." %A IN ('powershell 1.49 / 531.51 * 100') DO (
SET "CurrentDrive-FreeSpace-Percent1=%A"
SET "CurrentDrive-FreeSpace-Percent2=%B"
SET "CurrentDrive-FreeSpace-Percent2=!CurrentDrive-FreeSpace-Percent2:~0,1!"
SET "CurrentDrive-FreeSpace-Percent=!CurrentDrive-FreeSpace-Percent1!.!CurrentDrive-FreeSpace-Percent2!"
)
D:\Desktop>(
SET "CurrentDrive-FreeSpace-Percent1=0"
SET "CurrentDrive-FreeSpace-Percent2=280333389776298"
SET "CurrentDrive-FreeSpace-Percent2=!CurrentDrive-FreeSpace-Percent2:~0,1!"
SET "CurrentDrive-FreeSpace-Percent=!CurrentDrive-FreeSpace-Percent1!.!CurrentDrive-FreeSpace-Percent2!"
)
D:\Desktop>ECHO Drive Information For E:
Drive Information For E:
D:\Desktop>ECHO/ Used Space: 530.02 (99.7%)
Used Space: 530.02 (99.7%)
D:\Desktop>ECHO/ Free Space: 1.49 (0.2%)
Free Space: 1.49 (0.2%)
D:\Desktop>ECHO/ Total Space: 531.51
Total Space: 531.51
D:\Desktop>ECHO/
D:\Desktop>EXIT /B
D:\Desktop>fsutil fsinfo statistics F:\ | FINDSTR /I /C:"The FSUTIL utility requires a local NTFS volume." 1>nul || CALL:GetDriveInfo F:\
D:\Desktop>SET "CurrentDrive=F:\"
D:\Desktop>SET "CurrentDrive=F:
D:\Desktop>FOR /F "tokens=7 delims= " %A IN ('fsutil volume diskfree F:\ |FINDSTR /C:"Total # of free bytes"') DO Call:GetDriveInfo-Convert "CurrentDrive-FreeSpace" "%A"
D:\Desktop>Call:GetDriveInfo-Convert "CurrentDrive-FreeSpace" "121982418944"
D:\Desktop>FOR /F %A IN ('powershell 121982418944 / 1024 / 1024 / 1024') DO SET "CurrentDrive_Temp=%A"
D:\Desktop>SET "CurrentDrive_Temp=113.60498046875"
D:\Desktop>FOR /F "Tokens=1-2 delims=." %A IN ("113.60498046875") DO SET "CurrentDrive_Temp1=%A" & SET "CurrentDrive_Temp2=%B"
D:\Desktop>SET "CurrentDrive_Temp1=113" & SET "CurrentDrive_Temp2=60498046875"
D:\Desktop>SET "CurrentDrive_Temp3=60"
D:\Desktop>IF "4" GEQ "5" SET /A "CurrentDrive_Temp3+=1"
D:\Desktop>IF "60" == "100" SET "CurrentDrive_Temp3=00" & SET /A CurrentDrive_Temp1+=1"
D:\Desktop>SET "CurrentDrive-FreeSpace=113.60"
D:\Desktop>EXIT /B
D:\Desktop>FOR /F "tokens=6 delims= " %A IN ('fsutil volume diskfree F:\ |FINDSTR /C:"Total # of bytes"') DO CALL:GetDriveInfo-Convert "CurrentDrive-TotalSpace" "%A"
D:\Desktop>CALL:GetDriveInfo-Convert "CurrentDrive-TotalSpace" "214748360704"
D:\Desktop>FOR /F %A IN ('powershell 214748360704 / 1024 / 1024 / 1024') DO SET "CurrentDrive_Temp=%A"
D:\Desktop>SET "CurrentDrive_Temp=199.999996185303"
D:\Desktop>FOR /F "Tokens=1-2 delims=." %A IN ("199.999996185303") DO SET "CurrentDrive_Temp1=%A" & SET "CurrentDrive_Temp2=%B"
D:\Desktop>SET "CurrentDrive_Temp1=199" & SET "CurrentDrive_Temp2=999996185303"
D:\Desktop>SET "CurrentDrive_Temp3=99"
D:\Desktop>IF "9" GEQ "5" SET /A "CurrentDrive_Temp3+=1"
D:\Desktop>IF "100" == "100" SET "CurrentDrive_Temp3=00" & SET /A CurrentDrive_Temp1+=1"
D:\Desktop>SET "CurrentDrive-TotalSpace=200.00"
D:\Desktop>EXIT /B
D:\Desktop>FOR /F %A IN ('powershell 200.00 - 113.60') DO Set "CurrentDrive-UsedSpace=%A"
D:\Desktop>Set "CurrentDrive-UsedSpace=86.4"
D:\Desktop>FOR /F "tokens=1-2 delims=." %A IN ('powershell 86.4 / 200.00 * 100') DO (
SET "CurrentDrive-UsedSpace-Percent1=%A"
SET "CurrentDrive-UsedSpace-Percent2=%B"
SET "CurrentDrive-UsedSpace-Percent2=!CurrentDrive-UsedSpace-Percent2:~0,1!"
SET "CurrentDrive-UsedSpace-Percent=!CurrentDrive-UsedSpace-Percent1!.!CurrentDrive-UsedSpace-Percent2!"
)
D:\Desktop>(
SET "CurrentDrive-UsedSpace-Percent1=43"
SET "CurrentDrive-UsedSpace-Percent2=2"
SET "CurrentDrive-UsedSpace-Percent2=!CurrentDrive-UsedSpace-Percent2:~0,1!"
SET "CurrentDrive-UsedSpace-Percent=!CurrentDrive-UsedSpace-Percent1!.!CurrentDrive-UsedSpace-Percent2!"
)
D:\Desktop>FOR /F "tokens=1-2 delims=." %A IN ('powershell 113.60 / 200.00 * 100') DO (
SET "CurrentDrive-FreeSpace-Percent1=%A"
SET "CurrentDrive-FreeSpace-Percent2=%B"
SET "CurrentDrive-FreeSpace-Percent2=!CurrentDrive-FreeSpace-Percent2:~0,1!"
SET "CurrentDrive-FreeSpace-Percent=!CurrentDrive-FreeSpace-Percent1!.!CurrentDrive-FreeSpace-Percent2!"
)
D:\Desktop>(
SET "CurrentDrive-FreeSpace-Percent1=56"
SET "CurrentDrive-FreeSpace-Percent2=8"
SET "CurrentDrive-FreeSpace-Percent2=!CurrentDrive-FreeSpace-Percent2:~0,1!"
SET "CurrentDrive-FreeSpace-Percent=!CurrentDrive-FreeSpace-Percent1!.!CurrentDrive-FreeSpace-Percent2!"
)
D:\Desktop>ECHO Drive Information For F:
Drive Information For F:
D:\Desktop>ECHO/ Used Space: 86.4 (43.2%)
Used Space: 86.4 (43.2%)
D:\Desktop>ECHO/ Free Space: 113.60 (56.8%)
Free Space: 113.60 (56.8%)
D:\Desktop>ECHO/ Total Space: 200.00
Total Space: 200.00
D:\Desktop>ECHO/
D:\Desktop>EXIT /B
D:\Desktop>fsutil fsinfo statistics G:\ | FINDSTR /I /C:"The FSUTIL utility requires a local NTFS volume." 1>nul || CALL:GetDriveInfo G:\
D:\Desktop>SET "CurrentDrive=G:\"
D:\Desktop>SET "CurrentDrive=G:
D:\Desktop>FOR /F "tokens=7 delims= " %A IN ('fsutil volume diskfree G:\ |FINDSTR /C:"Total # of free bytes"') DO Call:GetDriveInfo-Convert "CurrentDrive-FreeSpace" "%A"
D:\Desktop>Call:GetDriveInfo-Convert "CurrentDrive-FreeSpace" "214605746176"
D:\Desktop>FOR /F %A IN ('powershell 214605746176 / 1024 / 1024 / 1024') DO SET "CurrentDrive_Temp=%A"
D:\Desktop>SET "CurrentDrive_Temp=199.867176055908"
D:\Desktop>FOR /F "Tokens=1-2 delims=." %A IN ("199.867176055908") DO SET "CurrentDrive_Temp1=%A" & SET "CurrentDrive_Temp2=%B"
D:\Desktop>SET "CurrentDrive_Temp1=199" & SET "CurrentDrive_Temp2=867176055908"
D:\Desktop>SET "CurrentDrive_Temp3=86"
D:\Desktop>IF "7" GEQ "5" SET /A "CurrentDrive_Temp3+=1"
D:\Desktop>IF "87" == "100" SET "CurrentDrive_Temp3=00" & SET /A CurrentDrive_Temp1+=1"
D:\Desktop>SET "CurrentDrive-FreeSpace=199.87"
D:\Desktop>EXIT /B
D:\Desktop>FOR /F "tokens=6 delims= " %A IN ('fsutil volume diskfree G:\ |FINDSTR /C:"Total # of bytes"') DO CALL:GetDriveInfo-Convert "CurrentDrive-TotalSpace" "%A"
D:\Desktop>CALL:GetDriveInfo-Convert "CurrentDrive-TotalSpace" "214748360704"
D:\Desktop>FOR /F %A IN ('powershell 214748360704 / 1024 / 1024 / 1024') DO SET "CurrentDrive_Temp=%A"
D:\Desktop>SET "CurrentDrive_Temp=199.999996185303"
D:\Desktop>FOR /F "Tokens=1-2 delims=." %A IN ("199.999996185303") DO SET "CurrentDrive_Temp1=%A" & SET "CurrentDrive_Temp2=%B"
D:\Desktop>SET "CurrentDrive_Temp1=199" & SET "CurrentDrive_Temp2=999996185303"
D:\Desktop>SET "CurrentDrive_Temp3=99"
D:\Desktop>IF "9" GEQ "5" SET /A "CurrentDrive_Temp3+=1"
D:\Desktop>IF "100" == "100" SET "CurrentDrive_Temp3=00" & SET /A CurrentDrive_Temp1+=1"
D:\Desktop>SET "CurrentDrive-TotalSpace=200.00"
D:\Desktop>EXIT /B
D:\Desktop>FOR /F %A IN ('powershell 200.00 - 199.87') DO Set "CurrentDrive-UsedSpace=%A"
D:\Desktop>Set "CurrentDrive-UsedSpace=0.129999999999995"
D:\Desktop>FOR /F "tokens=1-2 delims=." %A IN ('powershell 0.129999999999995 / 200.00 * 100') DO (
SET "CurrentDrive-UsedSpace-Percent1=%A"
SET "CurrentDrive-UsedSpace-Percent2=%B"
SET "CurrentDrive-UsedSpace-Percent2=!CurrentDrive-UsedSpace-Percent2:~0,1!"
SET "CurrentDrive-UsedSpace-Percent=!CurrentDrive-UsedSpace-Percent1!.!CurrentDrive-UsedSpace-Percent2!"
)
D:\Desktop>(
SET "CurrentDrive-UsedSpace-Percent1=0"
SET "CurrentDrive-UsedSpace-Percent2=0649999999999975"
SET "CurrentDrive-UsedSpace-Percent2=!CurrentDrive-UsedSpace-Percent2:~0,1!"
SET "CurrentDrive-UsedSpace-Percent=!CurrentDrive-UsedSpace-Percent1!.!CurrentDrive-UsedSpace-Percent2!"
)
D:\Desktop>FOR /F "tokens=1-2 delims=." %A IN ('powershell 199.87 / 200.00 * 100') DO (
SET "CurrentDrive-FreeSpace-Percent1=%A"
SET "CurrentDrive-FreeSpace-Percent2=%B"
SET "CurrentDrive-FreeSpace-Percent2=!CurrentDrive-FreeSpace-Percent2:~0,1!"
SET "CurrentDrive-FreeSpace-Percent=!CurrentDrive-FreeSpace-Percent1!.!CurrentDrive-FreeSpace-Percent2!"
)
D:\Desktop>(
SET "CurrentDrive-FreeSpace-Percent1=99"
SET "CurrentDrive-FreeSpace-Percent2=935"
SET "CurrentDrive-FreeSpace-Percent2=!CurrentDrive-FreeSpace-Percent2:~0,1!"
SET "CurrentDrive-FreeSpace-Percent=!CurrentDrive-FreeSpace-Percent1!.!CurrentDrive-FreeSpace-Percent2!"
)
D:\Desktop>ECHO Drive Information For G:
Drive Information For G:
D:\Desktop>ECHO/ Used Space: 0.129999999999995 (0.0%)
Used Space: 0.129999999999995 (0.0%)
D:\Desktop>ECHO/ Free Space: 199.87 (99.9%)
Free Space: 199.87 (99.9%)
D:\Desktop>ECHO/ Total Space: 200.00
Total Space: 200.00
D:\Desktop>ECHO/
D:\Desktop>EXIT /B
D:\Desktop>fsutil fsinfo statistics H:\ | FINDSTR /I /C:"The FSUTIL utility requires a local NTFS volume." 1>nul || CALL:GetDriveInfo H:\
D:\Desktop>fsutil fsinfo statistics I:\ | FINDSTR /I /C:"The FSUTIL utility requires a local NTFS volume." 1>nul || CALL:GetDriveInfo I:\
D:\Desktop>SET "CurrentDrive=I:\"
D:\Desktop>SET "CurrentDrive=I:
D:\Desktop>FOR /F "tokens=7 delims= " %A IN ('fsutil volume diskfree I:\ |FINDSTR /C:"Total # of free bytes"') DO Call:GetDriveInfo-Convert "CurrentDrive-FreeSpace" "%A"
D:\Desktop>Call:GetDriveInfo-Convert "CurrentDrive-FreeSpace" "906973425664"
D:\Desktop>FOR /F %A IN ('powershell 906973425664 / 1024 / 1024 / 1024') DO SET "CurrentDrive_Temp=%A"
D:\Desktop>SET "CurrentDrive_Temp=844.684825897217"
D:\Desktop>FOR /F "Tokens=1-2 delims=." %A IN ("844.684825897217") DO SET "CurrentDrive_Temp1=%A" & SET "CurrentDrive_Temp2=%B"
D:\Desktop>SET "CurrentDrive_Temp1=844" & SET "CurrentDrive_Temp2=684825897217"
D:\Desktop>SET "CurrentDrive_Temp3=68"
D:\Desktop>IF "4" GEQ "5" SET /A "CurrentDrive_Temp3+=1"
D:\Desktop>IF "68" == "100" SET "CurrentDrive_Temp3=00" & SET /A CurrentDrive_Temp1+=1"
D:\Desktop>SET "CurrentDrive-FreeSpace=844.68"
D:\Desktop>EXIT /B
D:\Desktop>FOR /F "tokens=6 delims= " %A IN ('fsutil volume diskfree I:\ |FINDSTR /C:"Total # of bytes"') DO CALL:GetDriveInfo-Convert "CurrentDrive-TotalSpace" "%A"
D:\Desktop>CALL:GetDriveInfo-Convert "CurrentDrive-TotalSpace" "1000202432512"
D:\Desktop>FOR /F %A IN ('powershell 1000202432512 / 1024 / 1024 / 1024') DO SET "CurrentDrive_Temp=%A"
D:\Desktop>SET "CurrentDrive_Temp=931.51110458374"
D:\Desktop>FOR /F "Tokens=1-2 delims=." %A IN ("931.51110458374") DO SET "CurrentDrive_Temp1=%A" & SET "CurrentDrive_Temp2=%B"
D:\Desktop>SET "CurrentDrive_Temp1=931" & SET "CurrentDrive_Temp2=51110458374"
D:\Desktop>SET "CurrentDrive_Temp3=51"
D:\Desktop>IF "1" GEQ "5" SET /A "CurrentDrive_Temp3+=1"
D:\Desktop>IF "51" == "100" SET "CurrentDrive_Temp3=00" & SET /A CurrentDrive_Temp1+=1"
D:\Desktop>SET "CurrentDrive-TotalSpace=931.51"
D:\Desktop>EXIT /B
D:\Desktop>FOR /F %A IN ('powershell 931.51 - 844.68') DO Set "CurrentDrive-UsedSpace=%A"
D:\Desktop>Set "CurrentDrive-UsedSpace=86.83"
D:\Desktop>FOR /F "tokens=1-2 delims=." %A IN ('powershell 86.83 / 931.51 * 100') DO (
SET "CurrentDrive-UsedSpace-Percent1=%A"
SET "CurrentDrive-UsedSpace-Percent2=%B"
SET "CurrentDrive-UsedSpace-Percent2=!CurrentDrive-UsedSpace-Percent2:~0,1!"
SET "CurrentDrive-UsedSpace-Percent=!CurrentDrive-UsedSpace-Percent1!.!CurrentDrive-UsedSpace-Percent2!"
)
D:\Desktop>(
SET "CurrentDrive-UsedSpace-Percent1=9"
SET "CurrentDrive-UsedSpace-Percent2=32142435400586"
SET "CurrentDrive-UsedSpace-Percent2=!CurrentDrive-UsedSpace-Percent2:~0,1!"
SET "CurrentDrive-UsedSpace-Percent=!CurrentDrive-UsedSpace-Percent1!.!CurrentDrive-UsedSpace-Percent2!"
)
D:\Desktop>FOR /F "tokens=1-2 delims=." %A IN ('powershell 844.68 / 931.51 * 100') DO (
SET "CurrentDrive-FreeSpace-Percent1=%A"
SET "CurrentDrive-FreeSpace-Percent2=%B"
SET "CurrentDrive-FreeSpace-Percent2=!CurrentDrive-FreeSpace-Percent2:~0,1!"
SET "CurrentDrive-FreeSpace-Percent=!CurrentDrive-FreeSpace-Percent1!.!CurrentDrive-FreeSpace-Percent2!"
)
D:\Desktop>(
SET "CurrentDrive-FreeSpace-Percent1=90"
SET "CurrentDrive-FreeSpace-Percent2=6785756459941"
SET "CurrentDrive-FreeSpace-Percent2=!CurrentDrive-FreeSpace-Percent2:~0,1!"
SET "CurrentDrive-FreeSpace-Percent=!CurrentDrive-FreeSpace-Percent1!.!CurrentDrive-FreeSpace-Percent2!"
)
D:\Desktop>ECHO Drive Information For I:
Drive Information For I:
D:\Desktop>ECHO/ Used Space: 86.83 (9.3%)
Used Space: 86.83 (9.3%)
D:\Desktop>ECHO/ Free Space: 844.68 (90.6%)
Free Space: 844.68 (90.6%)
D:\Desktop>ECHO/ Total Space: 931.51
Total Space: 931.51
D:\Desktop>ECHO/
D:\Desktop>EXIT /B
D:\Desktop>pause
Press any key to continue . . .
D:\Desktop>EXIT /B
答案 0 :(得分:2)
新答案
当我看到你编辑的帖子时,我正在开发一个能够正确舍入结果的答案。
如果您打算使用PowerShell,您也可以使用它。我发现这个Windows PowerShell Tip of the Week显示了如何格式化数字。
整个脚本最好用PowerShell完成,但我对该语言没有任何专业知识。所以我把这个混合脚本拼凑在了一起。甚至可能有更好的方法来做混合解决方案,但这是有效的。它比原始版本快得多,因为它将PowerShell调用的数量减少到每个驱动器一个。
@echo off
setlocal enableDelayedExpansion
cls
for /f "tokens=1*" %%A in ('fsutil fsinfo drives') do for %%C in (%%B) do (
fsutil fsinfo statistics %%C |FINDSTR /I /C:"The FSUTIL utility requires a local NTFS volume." >nul || CALL:GetDriveInfo %%C
)
exit /b
:GetDriveInfo
set "free="
for /f "tokens=2 delims=:" %%A in ('fsutil volume diskfree %~1') do (
if not defined free (set free=%%A) else (set total=%%A & goto :break)
)
:break
set "cmd=$used=%total%-%free%;$usedP=$used/%total%;$used=$used/1073741824;$freeP=%free%/%total%;$free=%free%/1073741824;$total=%total%/1073741824;"{0:N2}" -f $used;"{0:P1}" -f $usedP;"{0:N2}" -f $free;"{0:P1}" -f $freeP;"{0:N2}" -f $total"
set i=0
for /f "delims=" %%N in ('echo !cmd!^|powershell -Command -') do (
set /a i+=1
set n!i!=%%N
)
set "drive=%~1"
echo Drive Information For %drive:\=%
echo Used Space: %n1% (%n2: =%)
echo Free Space: %n3% (%n4: =%)
echo Total Space: %n5%
exit /b
我不喜欢使用FSUTIL,因为它需要管理员权限,它仅限于NTFS卷,并且您的脚本每个驱动器调用两次,再加上一次。
我认为使用单个WMIC调用更简单,更好。此解决方案不限于NTFS驱动器。
编辑 - 我通过学习如何使用多个变量引用正确格式化整个字符串来简化powershell代码。我可以对之前基于FSUTIL的解决方案
做同样的事情我仍然对PowerShell输出进行后处理,以删除PowerShell在%符号前插入的空间。
@echo off
setlocal enableDelayedExpansion
cls
set "printCmd=;"Used Space: {0:N2} ({1:P1}^)`nFree Space: {2:N2} ({3:P1}^)`nTotal Space: {4:N2}" -f $used,$usedP,$free,$freeP,$total"
for /f "skip=1 tokens=1-3 delims= " %%A in ('wmic logicalDisk get deviceId^, freeSpace^, size') do if "%%C" neq "" (
set "defineVarsCmd=$used=%%C-%%B;$usedP=$used/%%C;$used=$used/1073741824;$freeP=%%B/%%C;$free=%%B/1073741824;$total=%%C/1073741824"
echo Drive Information For %%A
for /f "delims=" %%S in ('echo !defineVarsCmd!!printCmd!^|powershell -Command -') do (
set "str=%%S"
echo !str: %%=%%!
)
echo(
)
我确信知道PowerShell的人可以轻松地从上面删除批处理部分,并提供更短的纯PowerShell脚本。
原始答案
注意 - 此原始答案在第二个十进制数字后截断。它没有进行任何舍入
将数字分为2部分,整数和分数。将“00”附加到小数部分以确保其具有至少2位数。然后将数字重新组合在一起,在此过程中将小数部分截断为2位数。
以下代码需要十进制数作为第一个也是唯一的参数。
@echo off
setlocal
set num=%1
set part1=%num:.=&rem %
set "part2=%num:*.=%"
if "%part2%" neq "%num%" (set "part2=%part2%00") else set "part2=00"
set "num=%part1%.%part2:~0,2%
echo num=%num%
棘手的部分是获得整数部分。我使用变量扩展搜索和替换来将REM命令注入SET语句。
如果num = 19.123,则set part1=%num:.=&rem %
变为set part2=19&rem 123
将十进制数舍入为1/100精度的本地批处理方法
我喜欢使用FOR / F将数字解析为整数和小数部分。但是您的原始代码在舍入逻辑方面存在潜在问题。
我没有确切地确定您的一个驱动器号失败的原因,但是代码中存在类似1.09
的问题。 SET / A将09
解释为十六进制,并将抛出错误,因为9不是有效的十六进制数字。如果在.
之前缺少整数部分,您的方法也会出现问题,但在您的情况下这种情况永远不会发生。
我不认为这些问题中的任何一个都会导致您的特定故障,但我认为我会提供一个纯批处理方法,可以将任何十进制数可靠地舍入到1/100精度。
同样,脚本需要一个十进制数作为第一个也是唯一的参数。
@echo off
setlocal
for /f "tokens=1,2 delims=." %%A in ("0%~1") do (
set "part1=%%A"
set "part2=1%%B000"
)
for /f "tokens=* delims=0" %%N in ("%part1%1") do (
set /a "part1=%%N/10, roundup=%part2:~3,1%/5, part2=%part2:~0,3%+roundup, part1+=(part2/200)"
)
set "num=%part1%.%part2:~1,2%"
echo num=%num%
最后,这是另一个版本,它将小数位数保留为第二个参数。只要输入数小于200000000且请求的小数位数小于或等于9,它就应该始终有效。
@echo off
:roundDecimal number precision
setlocal enableDelayedExpansion
for /f "tokens=1,2 delims=." %%A in ("0%~1") do (
set "part1=%%A"
set "part2=1%%B0000000000"
)
set /a len=%~2+1
for /f "tokens=* delims=0" %%N in ("%part1%1") do (
set /a "part1=%%N/10, roundup=!part2:~%len%,1!/5, part2=!part2:~0,%len%!+roundup"
set /a "part1+=(!part2:~0,1!/2)"
)
if %~2 gtr 0 (set num=%part1%.!part2:~1,%~2!) else set num=%part1%
echo num=%num%