asp tutorials, asp.net tutorials, sample code, and Microsoft news from 15Seconds
Data Access  |   Troubleshooting  |   Security  |   Performance  |   ADSI  |   Upload  |   Email  |   Control Building  |   Component Building  |   Forms  |   XML  |   Web Services  |   ASP.NET  |   .NET Features  |   .NET 2.0  |   App Development  |   App Architecture  |   IIS  |   Wireless
 
Pioneering Active Server
 Power Search





Active News
15 Seconds Weekly Newsletter
• Complete Coverage
• Site Updates
• Upcoming Features

More Free Newsletters
Reference
News
Articles
Code Samples
Components
Tools
New
Free
Downloads
Vendors
FAQ
Feedback
Books
Links
DL Archives
Community
Messageboard
List Servers
Mailing List
WebHosts
Consultants
Tech Jobs
15 Seconds
Home
Site Map
Press
Legal
Privacy Policy
internet.commerce














internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

HardwareCentral
Compare products, prices, and stores at Hardware Central!

Purpose:

This function returns a string containing the size of a file converted to the most "natural" unit based on its size. It selects the size unit where the value is between 1 and the factor to the next size unit (using a factor of 1024 (= 2^10)). Some companies may choose to use 1000 instead.

The function allows submitting numbers beyond the factor for the last unit size specified in vaUnits.

If the number of bytes is up to 1 kb, it is returned as a string with no decimals, otherwise it will return 2 decimals.

Usage:


Response.Write "<td width=""60%"">" & sFileName & "</td><td>" _
& FileSizeString(lTotalBytes) & "</td>"'


Function FileSizeString(ByVal vFileSize)

Dim vaUnits
Dim iUnitIndex
Dim iDecimals
Const iKSize = 1024

	'up to 1024 PetaBytes should be OK for now...
	vaUnits = Array(" Bytes", " kb", " Mb", " Gb", " Tb", " Pb")
	iDecimals = 0	'If number in Bytes don't display any decimals
	iUnitIndex = 0	'indicates which unit size to use.
	'Find the most natural unit that has a unit description:
 
	While vFileSize > iKSize and iUnitIndex < UBound(vaUnits)
		'Reduce to next higher unit:
		vFileSize = vFileSize / iKSize
		iUnitIndex = iUnitIndex + 1
		iDecimals = 2
	Wend
	FileSizeString = FormatNumber(vFileSize, iDecimals) &
vaUnits(iUnitIndex)

End Function


Submitted by Tore Bostrup



email this code sample to a colleague

Related Products
ASP MagicFile
The MagicFile object allows you to manipulate files and directories from an Active Server Page. You can create directories, remove them, search for specific files, and copy files from one destination to another. Directories and files can be searched recursively or non-recursively, depending upon your needs.
[Top]

Support the Active Server Industry



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers