Solved

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

Posted on 2010-08-19
8
5,140 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 56

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 33474798
==> 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
ID: 33475333
Thanks billprew,

Cool, this is very educational.

Thanks,
Rene
0
 
LVL 56

Expert Comment

by:Bill Prew
ID: 33477993
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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
LVL 10

Author Comment

by:ReneGe
ID: 33478650
Thanks pal!

Rene
0
 
LVL 10

Author Comment

by:ReneGe
ID: 33478662
0
 
LVL 10

Author Comment

by:ReneGe
ID: 33562365
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 56

Expert Comment

by:Bill Prew
ID: 33563251
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
ID: 33563330
bp,

Your just too cool!!

Cheers,
Rene
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
Progress
Introduction to Processes

627 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