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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help about getting the File Size in VB 2

Status
Not open for further replies.

moonoo

Programmer
May 17, 2000
62
US
Hi,
I want to check the size of a file is Visual basic . This
File is already opened by the open statement . For getting
the size I am using something like this

Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFile("C:\VBProj\" & strCurFile)
MsgBox f.size

But@this code is giving the correct result only if the
file is closed . For already opened files it's giving
result 0.

How do i solve this problem . Please help...

Regards
 
Hi moonoo

Use LOF(filenumber)

:) Sunaj

 
How did you Open the files: with traditional file i/o statements or with the FileSystemObject? I didn't get any difficulties with either method using LOF, FileLen of Fso.FileSize. Note that for LOF the file MUST be opened.

I submitted the following code:

Const c_strFileName As String = "D:\MainFolder\NormalFile.doc"

Public Sub Main()

Debug.Print vbCrLf & String(20, "-") & vbCrLf & "Traditional File I/O Tests" & vbCrLf

Dim lngFileHnd As Long

lngFileHnd = FreeFile()
Open c_strFileName For Input Access Read As lngFileHnd
Debug.Print c_strFileName & " Size: " & LOF(lngFileHnd) & " bytes - " & _
"OPEN FILE - LOF"
Debug.Print c_strFileName & " Size: " & FileLen(c_strFileName) & " bytes - " & _
"OPEN File - FileLen"
Close lngFileHnd


'Following statement will cause an error, because the file is NOT open!

On Error GoTo PROC_ERROR
Debug.Print c_strFileName & " Size: " & LOF(lngFileHnd) & " bytes - " & _
"CLOSED File - LOF"
On Error GoTo 0

'Use FileLen for closed files

Debug.Print c_strFileName & " Size: " & FileLen(c_strFileName) & " bytes - " & _
"CLOSED File - FileLen"


'FSO methods and properties: File can be open or not

Debug.Print vbCrLf & String(20, "-") & vbCrLf & "FSO Tests" & vbCrLf

Dim objFso As FileSystemObject
Dim objFile As File
Dim objTextStream As TextStream

Set objFso = New FileSystemObject
Set objFile = objFso.GetFile(c_strFileName)
Set objTextStream = objFso_OpenTextFile(objFile, ForReading)

Debug.Print objFile.Name & " Size: " & objFile.Size & " bytes - OPEN File"
objTextStream.Close
Debug.Print objFile.Name & " Size: " & objFile.Size & " bytes - CLOSED File"

Set objTextStream = Nothing
Set objFile = Nothing
Set objFso = Nothing

Debug.Print vbCrLf & String(20, "-")
PROC_EXIT:
Exit Sub

PROC_ERROR:
Debug.Print Err.Number & " " & Err.Description
Err.Clear
Resume Next

End Sub


and got the following results:


--------------------
Traditional File I/O Tests

a:\MainFolder\NormalFile.doc Size: 4608 bytes - OPEN FILE - LOF
a:\MainFolder\NormalFile.doc Size: 4608 bytes - OPEN File - FileLen
52 Bad file name or number
a:\MainFolder\NormalFile.doc Size: 4608 bytes - CLOSED File - FileLen

--------------------
FSO Tests

NormalFile.doc Size: 4608 bytes - OPEN File
NormalFile.doc Size: 4608 bytes - CLOSED File

--------------------

As you can observe, all results are consistent except for the unopened LOF case, where you get an error.

Sorry for the odd formatting: this editor has some (!° trouble with VB's continuation characters _________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top