Solved

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

Posted on 2010-08-19
8
5,014 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 54

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 54

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 54

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Eclipse with various Java releases 7 85
statistics basics sources 2 73
Move files based on file names? 8 67
CMD VB Script: remove empty rows 12 13
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …

739 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