Solved

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

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

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 55

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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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 55

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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Starting up a Project

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