• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 236
  • Last Modified:

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.


0
Rabel
Asked:
Rabel
  • 6
  • 3
  • 2
1 Solution
 
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
 
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
b0lsc0ttCommented:
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
 
b0lsc0ttCommented:
Your welcome.  Thanks for the fun question!
0
 
hongjunCommented:
welcome :)
Glad I helped
0
 
hongjunCommented:
We helped
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 6
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now