Format totals with thousands separator and convert bytes to megabytes
Posted: 02 Sep 2025 05:29
Good afternoon.
I have come up with the following code which now works great.
However, I am having trouble formatting the output.
I would like the thousands separator in both the files totals [ #,##0 ] and the size totals [ #,###.00 ], along with the size totals being in MB's instead of Bytes and with two decimal places [ #,###.00 ].
Here is the code:
Any help will be greatly appreciated.
Thanks in advance.
I have come up with the following code which now works great.
However, I am having trouble formatting the output.
I would like the thousands separator in both the files totals [ #,##0 ] and the size totals [ #,###.00 ], along with the size totals being in MB's instead of Bytes and with two decimal places [ #,###.00 ].
Here is the code:
Code: Select all
@echo off
echo. & echo Processing . . .
echo. & setlocal DisableDelayedExpansion
for %%a in ("D:\") do set "Folder=%%~fa"
for /d %%f in ("%Folder%\*") do (
set /a "Size=0", "Files=0", "Directories=0"
for /f "tokens=1,3,5" %%a in ('
dir /-c /a /w /s "%%~ff\*" 2^>nul ^| findstr /b /c:" "
') do if "%%~c"=="" (
set "Files=%%~a"
set "Size=%%~b"
) else set /a "Directories=%%~a/3"
setlocal EnableDelayedExpansion
echo(%%~nxf ^| !Directories! Directories ^| !Files! Files ^| !Size! Bytes
endlocal
)>> %Temp%\A.txt
sort %Temp%\A.txt /o %Temp%\A.txt
for /f "delims=" %%i in (%Temp%\A.txt) do echo. %%i
del %Temp%\A.txt >nul 2>&1
set /a Counter=0
for /d %%a in ("%Folder%\*") do (
set "Size=0"
for /f "tokens=1,3,5" %%a in ('dir /-c /a /w /s "%%~ff\*" 2^>nul ^| findstr /b /c:" "') do if "%%~c"=="" set "Size=%%~a"
set /a Counter+=1
)
PowerShell Write-Host ^
`n 'Total Entries": "' -NoNewline; (%Counter%).ToString('#,##0')
echo. & echo Processing Complete.
Thanks in advance.