Format totals with thousands separator and convert bytes to megabytes

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
PAB
Posts: 154
Joined: 12 Aug 2019 13:57

Format totals with thousands separator and convert bytes to megabytes

#1 Post by PAB » 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:

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.
Any help will be greatly appreciated.

Thanks in advance.


T3RRY
Posts: 258
Joined: 06 May 2020 10:14

Re: Format totals with thousands separator and convert bytes to megabytes

#3 Post by T3RRY » 02 Sep 2025 07:22

Re formatting with thousands seperator:
see the :# No Prefix example

Code: Select all

@Echo off
 %= Examples =%

:# 0 prefixed
 Call :FormatNum 1532151544621668007951135446231798748995413134278463598153468247 rv[1]1 -0
 Call :FormatNum 4071 rv[1]2 -0
 Call :FormatNum 20 rv[1]3 -0
 Call :FormatNum 3 rv[1]4 -0
 Set rv[1]

:# Whitespace prefixed
 Call :FormatNum 1532151544621668007951135446231798748995413134278463598153468247 rv[2]1 -S
 Call :FormatNum 4071 rv[2]2 -S
 Call :FormatNum 20 rv[2]3 -S
 Call :FormatNum 3 rv[3]4 -S
 Set rv[2]

:# No Prefix
 Call :FormatNum 1532151544621668007951135446231798748995413134278463598153468247 rv[3]1
 Call :FormatNum 4071 rv[3]2
 Call :FormatNum 20 rv[3]3
 Call :FormatNum 3 rv[3]4
 Set rv[3]

PAUSE
Goto:Eof
%= end exammples =%

:FormatNum <Int> <ReturnVar> [-0|-S]
:+ FormatNum function by T3RRY ; Created: 25th July 2021
:+ Purpose: Function to Format numbers up to 1485 digits long with coma's IE:
:+  10 = 10 ; 100 = 100 ; 1000 = 1,000 ; 10000 = 10,000 ; 100000 = 100,000 ; 1000000 = 1,000,000
:+ -S and -0 switches accepted as third arg to prepend with whitespice or leading zero's respectively.

%= define FormatDelim proir to call to overide default ',' delim =% If not defined FormatDelim Set "FormatDelim=,"

 If /I "%~2"=="-0" Exit /b 3 %= Errorlevel 3 - arg2 incorrectly occuipied by arg3 =%
 If /I "%~2"=="-S" Exit /b 3
 If "%~2"=="" Exit /b 2      %= Errorlevel 2 - arg2 missing =%
 If "%~1"=="" Exit /b 1      %= Errorlevel 1 - all args missing =%

Setlocal EnableDelayedExpansion
 set "tmp=.%~1" %= No Leading zeros =%
 If "%~3"=="-0" set "tmp=.00%~1" %= Leading zeros =%
 If /I "%~3"=="-S" set "tmp=.  %~1" %= Leading whitespace =%

 %= Approximate Max string length as a multiple of 3 to reduce execution time =%
 For %%i in (1485 918 567 351 216 135 81 54 27 18 9)Do If "!tmp:~%%i,1!"=="" ( Set /A "Max=%%i" )

 For /l %%n in (!Max! -3 0)Do (
  For /f "Delims=" %%o in ('Set /A %%n+3')Do If not "%%n"=="0" (
   set "Size=!Size!%FormatDelim%!tmp:~-%%o,-%%n!"
   set "Size=!Size:%FormatDelim%%FormatDelim%=%FormatDelim%!"
  )Else  set "Size=!Size!%FormatDelim%!tmp:~-3!"
 )
set "Size=!Size:%FormatDelim%%FormatDelim%=%FormatDelim%!"

 %= iterations below are required to remove all paired comas. =%
rem For /l %%i in (0 1 8)Do set "Size=!Size:%FormatDelim%%FormatDelim%=%FormatDelim%!"
 %= remove excess leading chars =%
 set "Size=!Size:~1!"
 set "Size=!Size:.00%FormatDelim%=!"
 set "Size=!Size:.0%FormatDelim%=!"
 set "Size=!Size:.  %FormatDelim%=!"
 set "Size=!Size:. %FormatDelim%=!"
 set "Size=!Size:.%FormatDelim%=!"
 set "Size=!Size:.=!"
Endlocal & Set "%~2=%Size%"
Exit /B 0

PAB
Posts: 154
Joined: 12 Aug 2019 13:57

Re: Format totals with thousands separator and convert bytes to megabytes

#4 Post by PAB » 02 Sep 2025 08:04

Thanks elzooilogico and T3RRY for taking the time to answer.

T3RRY, unfortunately, that is way beyond my capabilities but I will spend some time trying to figure it out and adapt it to my code.

I am just an old bloke scanning the Internet for pieces of code to put together and adapt to get the task I am trying to do done.

Thanks again.

miskox
Posts: 670
Joined: 28 Jun 2010 03:46

Re: Format totals with thousands separator and convert bytes to megabytes

#5 Post by miskox » 02 Sep 2025 10:53

For thousands separators I use this (replace . with , if required):

Code: Select all

@echo off

set "line=1234567"
echo Original line:%line%

set num=0

:LOOP
call set tmpa=%%line:~%num%,1%%%
set /a num+=1
if not "%tmpa%" equ "" (
set rline=%tmpa%%rline%
goto LOOP
)
echo Reversed line:%rline%

set /a num=0
set line=%rline%
set rline=&rem
set /a separator_cnt=0

:LOOP2
call set tmpa=%%line:~%num%,1%%%
set /a num+=1
REM replace . below to , if required.
if not "%tmpa%"=="" if "%separator_cnt%"=="3" set tmpa=%tmpa%.&set /a separator_cnt=0
if not "%tmpa%"=="" (
	set rline=%tmpa%%rline%
	set /a separator_cnt+=1
	goto LOOP2
)
set rline=              %rline%
REM right justification, 15 characters in size
set rline=%rline:~-15%

echo %rline% bytes
goto :EOF



Output:

Code: Select all

c:\>bat.cmd
Original line:1234567
Reversed line:7654321
      1.234.567 bytes
c:\>
Saso

Post Reply