2

有谁知道如何只显示小数点后的 2 个字符。

示例:19.123948493114 变成 19.12

不能使用 %variable:~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 
4

1 回答 1

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 脚本。


原始答案

注意 - 这个原始答案在小数点后第二位被截断。它不做任何舍入

把数字分成两部分,整数和小数。“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%
于 2012-09-29T12:52:36.603 回答