Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need Help Finding the Size of a File 1

Status
Not open for further replies.

Hawkeye123456

Programmer
May 5, 2000
24
US
I want to tell how BIG (in bites or KB or whatever) a file is.&nbsp;&nbsp;I want to be able to do this on any type (.exe, .zip, .mp3,...) I want to be able to use this for a Web site index program I am writting.<br><br>Thanks a bunch, and I love this site.
 
this is from the MSDN , it uses FileSystemObject which is availible in Visual Basic, this doesnt say it does what you are asking, but if you look at one of the click commands, it returns the filesize of a file it passes to it.<br><br><FONT FACE=monospace><br>HOWTO: Use FileSystemObject with Visual Basic <br><br>--------------------------------------------------------------------------------<br>The information in this article applies to:<br><br>Microsoft Visual Basic Learning, Professional, and Enterprise Editions for Windows, versions 5.0, 6.0<br><br>--------------------------------------------------------------------------------<br><br><br>SUMMARY<br>FileSystemObject provides a non-hierarchical structure to manipulate, read, and create ASCII and Unicode text files. This structure is very different from the hierarchical structure of the original implementation of File I/O in Visual Basic. FileSystemObject does not support binary file access, so you must still use the original File I/O model in Visual Basic for binary file access. <br><br><br><br>MORE INFORMATION<br>FileSystemObject can be found in Scrrun.dll. In addition to FileSystemOject, Scrrun.dll includes four other objects available for File I/O and other tasks. These objects include the File object, the TextStreamObject object, the Folder object, and the Drive object. All of these objects have properties and methods that are detailed in the Help files. <br><br>You can obtain Scrrun.dll by installing one of the following packages: <br><br><br>Windows Script Host<br>Windows NT Option Pack<br>Microsoft Internet Information Server 3.0<br>Scripting 3.1 upgrade<br>Visual Studio 6.0<br>Visual Basic 6.0 <br><br>FileSystemObject was originally created for the Visual Basic Scripting Edition. FileSystemObject is not included in the object library for Visual Basic or Visual Basic for Applications. To use FileSystemObject, you must select the Microsoft Scripting Run-time in the Project References dialog box for your project. <br><br>The following sample illustrates how to implement some of the FileSystemObject functionality. For more information, please see the Visual Basic Help files and the Visual Basic Books Online. <br><br>Steps to Create Sample Project<br>Start a new Standard EXE project in Visual Basic. Form1 is created by default. <br><br><br>Click References on the Project menu and select the Microsoft Scripting Runtime. If the Microsoft Scripting Runtime does not appear in the list, browse for Scrrun.dll on your system. Install one of the tools listed previously if necessary. <br><br><br>Add four CommandButton controls to Form1. The CommandButton controls demonstrate the following functionality:<br><br><br>&nbsp;&nbsp;&nbsp;Command1: How to read an existing text file using FileSystemObject<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;and TextStreamObject.<br><br>&nbsp;&nbsp;&nbsp;Command2: How to view file information using FileSystemObject and<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;the File object.<br><br>&nbsp;&nbsp;&nbsp;Command3: How to iterate through folders using FileSystemObject and<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;the Folder object.<br><br>&nbsp;&nbsp;&nbsp;Command4: How to view drive information using FileSystemObject and<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;the Drive object.<br>&nbsp;<br>Paste the following code into the General Declarations section of Form1:<br><br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Option Explicit<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Private Sub Command1_Click()<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'Declare variables.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dim fso As New FileSystemObject<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dim ts As TextStream<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'Open file.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Set ts = fso_OpenTextFile(Environ(&quot;windir&quot;) & &quot;\system.ini&quot;)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'Loop while not at the end of the file.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Do While Not ts.AtEndOfStream<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Debug.Print ts.ReadLine<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Loop<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'Close the file.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ts.Close<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;End Sub<br><br><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Private Sub Command2_Click()<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dim fso As New FileSystemObject<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dim f As File<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'Get a reference to the File object.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Set f = fso.GetFile(Environ(&quot;windir&quot;) & &quot;\system.ini&quot;)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MsgBox f.Size 'displays size of file<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;End Sub</b><br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Private Sub Command3_Click()<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dim fso As New FileSystemObject<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dim f As Folder, sf As Folder, path As String<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'Initialize path.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path = Environ(&quot;windir&quot;)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'Get a reference to the Folder object.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Set f = fso.GetFolder(path)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'Iterate through subfolders.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;For Each sf In f.SubFolders<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Debug.Print sf.Name<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Next<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;End Sub<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Private Sub Command4_Click()<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dim fso As New FileSystemObject<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dim mydrive As Drive<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dim path As String<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'Initialize path.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path = &quot;C:\&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'Get object.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Set mydrive = fso.GetDrive(path)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'Check for success.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MsgBox mydrive.DriveLetter 'displays &quot;C&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;End Sub<br>&nbsp;<br>Run your project. Click each CommandButton control and observe the results. <br><br><br><br><br><br>REFERENCES<br>For information, please see the following article in the Microsoft Knowledge Base: <br><br><br>Q185601 HOWTO: Recursively Search Directories Using FileSystemObject <br><br>Additional query words: <br><br>Keywords : kbFileIO KbVBA kbVBp kbVBp500 kbVBp600 kbGrpVB kbDSupport <br>Version : WINDOWS:5.0,6.0 <br>Platform : WINDOWS <br>Issue type : kbhowto <br>Technology : <br></font><br><br> <p>Karl<br><a href=mailto:kb244@kb244.8m.com>kb244@kb244.8m.com</a><br><a href= </a><br>Experienced in , or have messed with : VC++, Borland C++ Builder, VJ++6(starting),VB-Dos, VB1 thru VB6, Delphi 3 pro, Borland C++ 3(DOS), Borland C++ 4.5, HTML,Visual InterDev 6, ASP(WebProgramming), QBasic(least i didnt start with COBOL)
 
just so you know the size that the file object returns is in Bytes. <p>Karl<br><a href=mailto:kb244@kb244.8m.com>kb244@kb244.8m.com</a><br><a href= </a><br>Experienced in , or have messed with : VC++, Borland C++ Builder, VJ++6(starting),VB-Dos, VB1 thru VB6, Delphi 3 pro, Borland C++ 3(DOS), Borland C++ 4.5, HTML,Visual InterDev 6, ASP(WebProgramming), QBasic(least i didnt start with COBOL)
 
if all you need is the filesize - the function <FONT FACE=monospace><b>FileLen</font></b> does only that<br><br><FONT FACE=monospace><b>MySize = FileLen(&quot;TESTFILE&quot;)</font></b><br><br>Code above set the variable <FONT FACE=monospace><b>MySize</font></b> to the number of bytes in the file <FONT FACE=monospace><b>TESTFILE</font></b> in the current directory<br><br> <p>Mike<br><a href=mailto:Mike_Lacey@Cargill.Com>Mike_Lacey@Cargill.Com</a><br><a href= Cargill's Corporate Web Site</a><br>Please don't send me email questions without posting them in Tek-Tips as well. Better yet -- Post the question in Tek-Tips and send me a note saying "Have a look at so-and-so in the thingy forum would you?"
 
well Keep my post in mind if you want to mess with other purposes of the files (short and simple , good job Mike hehe) <p>Karl<br><a href=mailto:kb244@kb244.8m.com>kb244@kb244.8m.com</a><br><a href= </a><br>Experienced in , or have messed with : VC++, Borland C++ Builder, VJ++6(starting),VB-Dos, VB1 thru VB6, Delphi 3 pro, Borland C++ 3(DOS), Borland C++ 4.5, HTML,Visual InterDev 6, ASP(WebProgramming), QBasic(least i didnt start with COBOL)
 
Sorry Karl - didn't mean to sound critical, the fso is a powerful object, good post. <p>Mike<br><a href=mailto:Mike_Lacey@Cargill.Com>Mike_Lacey@Cargill.Com</a><br><a href= Cargill's Corporate Web Site</a><br>Please don't send me email questions without posting them in Tek-Tips as well. Better yet -- Post the question in Tek-Tips and send me a note saying "Have a look at so-and-so in the thingy forum would you?"
 
even so, but it would seem that some commands are more robust than other, the general rule in critical programming is to write the smallest ammount of code possible, including the ammount of work required to perform a task <br><i>some would argue that 10 lines of code compared to 1 is no big difference they probally would be only a nanoseconds of difference, but thats looking at the microscopic perspective, back up and see that same peice of code ran over a billion times then how many lines of wasted time is there?</i><br><br>thats my little two bits of why I prefer your response in this particular question. :) <p>Karl<br><a href=mailto:kb244@kb244.8m.com>kb244@kb244.8m.com</a><br><a href= </a><br>Experienced in , or have messed with : VC++, Borland C++ Builder, VJ++6(starting),VB-Dos, VB1 thru VB6, Delphi 3 pro, Borland C++ 3(DOS), Borland C++ 4.5, HTML,Visual InterDev 6, ASP(WebProgramming), QBasic(least i didnt start with COBOL)
 
a <FONT FACE=monospace>Long</font> should be ok<br> <p>Mike<br><a href=mailto:Mike_Lacey@Cargill.Com>Mike_Lacey@Cargill.Com</a><br><a href= Cargill's Corporate Web Site</a><br>Please don't send me email questions without posting them in Tek-Tips as well. Better yet -- Post the question in Tek-Tips and send me a note saying "Have a look at so-and-so in the thingy forum would you?"
 
an interger usally has the limit of -32 thousand to 32 thousand<br><br>and unsigned integer has a limit of 0 -&gt; 64K<br><br>since there is a chance you'll be getting file sizes larger than 64 kilobytes, a long is ok, since thats like well heres a table.<br><br>The values of the integral types are integers in the following ranges: (as from the MSDN as well)<br><br>The following table shows the supported data types, including storage sizes and ranges.<br><FONT FACE=monospace><br>Data type Storage size Range <br>Byte 1 byte 0 to 255 <br>Boolean 2 bytes True or False <br>Integer 2 bytes -32,768 to 32,767 <br>Long<br>(long integer) 4 bytes -2,147,483,648 to 2,147,483,647 <br>Single<br>(single-precision floating-point) 4 bytes -3.402823E38 to -1.401298E-45 for negative values; 1.401298E-45 to 3.402823E38 for positive values <br>Double<br>(double-precision floating-point) 8 bytes -1.79769313486231E308 to <br>-4.94065645841247E-324 for negative values; 4.94065645841247E-324 to 1.79769313486232E308 for positive values <br>Currency<br>(scaled integer) 8 bytes -922,337,203,685,477.5808 to 922,337,203,685,477.5807 <br>Decimal 14 bytes +/-79,228,162,514,264,337,593,543,950,335 with no decimal point; <br>+/-7.9228162514264337593543950335 with 28 places to the right of the decimal; smallest non-zero number is <br>+/-0.0000000000000000000000000001 <br>Date 8 bytes January 1, 100 to December 31, 9999 <br>Object 4 bytes Any Object reference <br>String <br>(variable-length) 10 bytes + string length 0 to approximately 2 billion&nbsp;&nbsp;<br>String<br>(fixed-length) Length of string 1 to approximately 65,400 <br>Variant<br>(with numbers) 16 bytes Any numeric value up to the range of a Double <br>Variant<br>(with characters) 22 bytes + string length Same range as for variable-length String <br>User-defined<br>(using Type) Number required by elements The range of each element is the same as the range of its data type. <br><br><br>Note&nbsp;&nbsp;&nbsp;Arrays of any data type require 20 bytes of memory plus 4 bytes for each array dimension plus the number of bytes occupied by the data itself. The memory occupied by the data can be calculated by multiplying the number of data elements by the size of each element. For example, the data in a single-dimension array consisting of 4 Integer data elements of 2 bytes each occupies 8 bytes. The 8 bytes required for the data plus the 24 bytes of overhead brings the total memory requirement for the array to 32 bytes.<br><br>A Variant containing an array requires 12 bytes more than the array alone.<br><br>Note&nbsp;&nbsp;&nbsp;Use the StrConv function to convert one type of string data to another.<br></font><br><br>so as you can see Long is like 2 Gigs and I doubt you'll find any files that big.<br> <p>Karl<br><a href=mailto:kb244@kb244.8m.com>kb244@kb244.8m.com</a><br><a href= </a><br>Experienced in , or have messed with : VC++, Borland C++ Builder, VJ++6(starting),VB-Dos, VB1 thru VB6, Delphi 3 pro, Borland C++ 3(DOS), Borland C++ 4.5, HTML,Visual InterDev 6, ASP(WebProgramming), QBasic(least i didnt start with COBOL)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top