Solved

Batch File: Convert bytes to either KB, MB or GB with 2 decimal points

Posted on 2010-08-19
8
4,706 Views
Last Modified: 2012-05-10
Hi there,

I need to create a batch file that will convert a number in bytes to either KB, MB or GB with 2 decimal points.

@echo off

FOR %%A IN (4321,1342.56,2145386496) do (
   call :ConvertNumber
   ECHO "%%A" is easyer to read like this: %ConvertedNumber%
)
ECHO.
PAUSE
EXIT

:ConvertNumber
whatever script
exit /b

RESULT:
4.21 KB
1.99 GB


Hoping this will be easy points for you.

Cheers,
Rene
0
Comment
Question by:ReneGe
  • 5
  • 3
8 Comments
 
LVL 51

Accepted Solution

by:
Bill Prew earned 500 total points
Comment Utility
==> 1342.56

This is actually quite a bit harder than you might think in a BAT file.  Could something be worked up, yes (and as a Math major I like to play with things like this).  But math support in BAT is relatively weak, and only integer numbers are supported so any manipulation of decimals has to be performed manually in the BAT script.  In addition, numbers are limited to 2^31 in size, so they max out at 2147483647, and so as a result any number larger than 2GB can not be dealt with.

Some approximations can be done and that is what I have typically seen on EE, there are some questions that already have solutions to this, a little searching will likely turn these up.

I often use a couple of standalone EXE utilities that do math for any complex algebra in a BAT script.

http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/Q_24881845.html

~bp
0
 
LVL 10

Author Comment

by:ReneGe
Comment Utility
Thanks billprew,

Cool, this is very educational.

Thanks,
Rene
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
Here are a couple of the tools I use from time to time.  Best to rename CALC to something else, since calc.exe is now the Windows GUI calculator.

~bp
utils.zip
0
 
LVL 10

Author Comment

by:ReneGe
Comment Utility
Thanks pal!

Rene
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 10

Author Comment

by:ReneGe
Comment Utility
0
 
LVL 10

Author Comment

by:ReneGe
Comment Utility
Just sharing a related simple script I just did.

Cheers,
Rene
@echo off

setlocal enabledelayedexpansion



for %%A IN (1,1024,1048576,1073741824) do (

	SET FileSize=%%A

	CALL :ConvertFileSize

	echo %%A Bytes=!FileSize!

	)

PAUSE

exit



:ConvertFileSize

IF !FileSize! GEQ 1073741824 (SET /a FileSize=!FileSize! / 1073741824 && SET FileSize=!FileSize! GB && Exit/b)

IF !FileSize! GEQ 1048576 (SET /a FileSize=!FileSize! / 1048576 && SET FileSize=!FileSize! MB && Exit/b)

IF !FileSize! GEQ 1024 (SET /a FileSize=!FileSize! / 1024 && SET FileSize=!FileSize! KB && Exit/b)

IF !FileSize! LSS 1024 SET FileSize=!FileSize! Bytes

exit /b

Open in new window

0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
Just to show how the mathematician in me might approach it...

~bp
@echo off
setlocal enabledelayedexpansion
 
for %%A IN (1,1024,1048576,1073741824) do (
        SET FileSize=%%A
        CALL :ConvertFileSize
        echo %%A Bytes=!FileSize!
        )
PAUSE
 
for %%A IN (1,1024,1048576,1073741824) do (
        CALL :ScaleNumber %%A "FileSize"
        echo %%A Bytes=!FileSize!
        )
PAUSE
exit /b
 
:ConvertFileSize
IF !FileSize! GEQ 1073741824 (SET /a FileSize=!FileSize! / 1073741824 && SET FileSize=!FileSize! GB && Exit/b)
IF !FileSize! GEQ 1048576 (SET /a FileSize=!FileSize! / 1048576 && SET FileSize=!FileSize! MB && Exit/b)
IF !FileSize! GEQ 1024 (SET /a FileSize=!FileSize! / 1024 && SET FileSize=!FileSize! KB && Exit/b)
IF !FileSize! LSS 1024 SET FileSize=!FileSize! Bytes
exit /b
 
:ScaleNumber [input-number] [output-variable-name]
  set Number=%~1
  set Scale=1
  for /L %%A in (1,1,3) do (
    if !Number! GEQ 1024 (
      set /A "Number = Number >> 10"
      set /A Scale += 1
    )
  )
  for /F "tokens=%Scale% delims=," %%A in ("Bytes,KB,MB,GB") do set %~2=%Number% %%A
  exit /b

Open in new window

0
 
LVL 10

Author Comment

by:ReneGe
Comment Utility
bp,

Your just too cool!!

Cheers,
Rene
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This is about my first experience with programming Arduino.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now