Can you find the error in this code? @echo off setlocal enabledelayedexpansion echo ===…
- User
Can you find the error in this code? @echo off setlocal enabledelayedexpansion
echo === Drive Info (Label, FS, Used/Total Space) === echo.
for %%D in (C D E F G H I J K L M N O P Q R S T U V W X Y Z) do ( if exist %%D:\ ( set "label=" set "fs=" set "total=" set "free=" set "used="
for /f "tokens=2,3 delims=:" %%A in ('fsutil volume diskfree %%D: ^| find "Of free bytes"') do ( set "total=%%A" set "free=%%B" ) for /f "tokens=2 delims=:" %%A in ('fsutil fsinfo volumeinfo %%D: ^| find "Volume Name"') do ( set "label=%%A" ) for /f "tokens=2 delims=:" %%A in ('fsutil fsinfo volumeinfo %%D: ^| find "File System Name"') do ( set "fs=%%A" ) if defined total if defined free ( set "total=!total:~1!" set "free=!free:~1!" set /a "used=!total!-!free!" echo Drive %%D: !label! (!fs!) Used: !used! bytes / Total: !total! bytes ) else ( echo Drive %%D: Information not available ) echo. ))
- Assistant
There's a logical error in the code related to how the
fsutil volume diskfreeoutput is being parsed. The issue is that thefind "Of free bytes"command is looking for a string that doesn't exist in the output offsutil volume diskfree.Here's the corrected version:
@echo off setlocal enabledelayedexpansion echo === Drive Info (Label, FS, Used/Total Space) === echo. for %%D in (C D E F G H I J K L M N O P Q R S T U V W X Y Z) do ( if exist %%D:\ ( set "label=" set "fs=" set "total=" set "free=" set "used=" for /f "tokens=1-3" %%A in ('fsutil volume diskfree %%D: ^| find "Total # of bytes"') do ( set "total=%%C" ) for /f "tokens=1-3" %%A in ('fsutil volume diskfree %%D: ^| find "Total # of free bytes"') do ( set "free=%%C" ) for /f "tokens=2 delims=:" %%A in ('fsutil fsinfo volumeinfo %%D: ^| find "Volume Name"') do ( set "label=%%A" ) for /f "tokens=2 delims=:" %%A in ('fsutil fsinfo volumeinfo %%D: ^| find "File System Name"') do ( set "fs=%%A" ) if defined total if defined free ( set "total=!total:~1!" set "free=!free:~1!" set /a "used=!total!-!free!" echo Drive %%D: !label! (!fs!) Used: !used! bytes / Total: !total! bytes ) else ( echo Drive %%D: Information not available ) echo. ) )Key changes:
- Changed the parsing of
fsutil volume diskfreeto look for "Total # of bytes" and "Total # of free bytes" instead of "Of free bytes" - Updated the token parsing to correctly capture the byte values (using tokens 1-3 and taking %%C as the value)
- The rest of the logic remains the same
The original code was trying to find "Of free bytes" which doesn't exist in the output, so the
totalandfreevariables were never being set, causing the script to always show "Information not available" for all drives.The corrected version properly captures the total and free space values from the
fsutiloutput. - Changed the parsing of
- User
I get the following error: "Was not expecting Used: at this time."
- Assistant
The error
"Was not expecting Used: at this time."occurs because theset /aarithmetic operation is failing when trying to calculateused=!total!-!free!. This typically happens when either!total!or!free!contains non-numeric characters (like commas or extra spaces), causing the calculation to fail.Fixed Code:
Here’s the corrected version that properly handles numeric values and avoids the error:
@echo off setlocal enabledelayedexpansion echo === Drive Info (Label, FS, Used/Total Space) === echo. for %%D in (C D E F G H I J K L M N O P Q R S T U V W X Y Z) do ( if exist %%D:\ ( set "label=" set "fs=" set "total=" set "free=" set "used=" for /f "tokens=1-3" %%A in ('fsutil volume diskfree %%D: ^| find "Total # of bytes"') do ( set "total=%%C" ) for /f "tokens=1-3" %%A in ('fsutil volume diskfree %%D: ^| find "Total # of free bytes"') do ( set "free=%%C" ) for /f "tokens=2 delims=:" %%A in ('fsutil fsinfo volumeinfo %%D: ^| find "Volume Name"') do ( set "label=%%A" ) for /f "tokens=2 delims=:" %%A in ('fsutil fsinfo volumeinfo %%D: ^| find "File System Name"') do ( set "fs=%%A" ) if defined total if defined free ( rem Remove commas and extra spaces from numbers set "total=!total:,=!" set "free=!free:,=!" set "total=!total: =!" set "free=!free: =!" set /a "used=total - free" 2>nul if !used! geq 0 ( echo Drive %%D: !label! (!fs!) Used: !used! bytes / Total: !total! bytes ) else ( echo Drive %%D: Error calculating used space ) ) else ( echo Drive %%D: Information not available ) echo. ) )Key Fixes:
-
Removed Commas & Extra Spaces:
fsutilmay output numbers with commas (e.g.,1,000,000), whichset /acannot process.- We strip commas (
!total:,=!) and spaces (!total: =!) before calculations.
-
Error Handling in
set /a:2>nulsuppresses arithmetic errors.- Added a check (
if !used! geq 0) to ensure the calculation worked.
-
Simplified Variable Usage:
- Removed unnecessary
!inset /a(sinceset /acan read variables directly).
- Removed unnecessary
Expected Output:
Now, the script should correctly display:
Drive C: Windows (NTFS) Used: 123456789 bytes / Total: 987654321 bytesinstead of failing with
"Was not expecting Used: at this time.".Let me know if you still encounter issues! 🚀
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.