How to change size in bytes

I am using a code that lists the size of files but it comes out in bytes how would I put in a code that makes it kb's or if needed mb's.  This is the code I am using.

"<br><b>Size: </b>"
response.write file.Size&" bytes"

Any help is appriecated.


LVL 2
RabelAsked:
Who is Participating?
 
hongjunCommented:
<br><b>Size: </b>
<%
Dim bytes, kBytes, mBytes

bytes = file.Size
kBytes = bytes / 1024
mBytes = kBytes / 1024

response.write bytes & " bytes<br>"
response.write kbytes & " kbytes<br>"
response.write mbytes & " mbytes<br>"
%>
0
 
hongjunCommented:
Dim bytes, kBytes, mBytes

bytes = file.Size
kBytes = bytes / 1024
mBytes = kBytes / 1024
0
 
RabelAuthor Commented:
Thank for the quick reply how/where would I put that code in - sorry fairly new to asp  
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
b0lsc0ttIT ManagerCommented:
Rabel,

Hongjun's suggestions is great but I wanted to just add a comment.  In situations where the file size may be different then I have used a server object and an if statement to determine which to use (bytes, KB, or MB).  If you are interested and want more information then let us know.  It is more work so if the file size is consistent then Hongjun's suggestion is what I would use.  A sample if statement is below.  The variable strFileLength contains the file's size as a string.  The variable strFileSize will include the appropriate bytes, KB, or MB description.

       strFileLength = Len(objRSFiles.Fields("FileSize").Value)
       If strFileLength > 6 then
          strFileSize = Round(CSng(objRSFiles.Fields("FileSize").Value) / 1000000, 2) & " MB"
       Elseif strFileLength > 3 then
          strFileSize = Round(CSng(objRSFiles.Fields("FileSize").Value) / 1000, 2) & " KB"
       Else
          strFileSize = objRSFiles.Fields("FileSize").Value & " Bytes"
       End if


b0lsc0tt
0
 
RabelAuthor Commented:
Ok that is really good thanks hongjun - is there a way to make it only have 2 numbers after kb I think I'll just go with that one so you do have to do it for the others but right now I get  

413.8659846106 kbytes

anyway I can get that to say 413.87 instead, thanks again
0
 
hongjunCommented:
0
 
hongjunCommented:
0
 
RabelAuthor Commented:
Thanks Guys
0
 
b0lsc0ttIT ManagerCommented:
Your welcome.  Thanks for the fun question!
0
 
hongjunCommented:
welcome :)
Glad I helped
0
 
hongjunCommented:
We helped
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.