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

FileLen function returns negative value

Status
Not open for further replies.

JTBorton

Technical User
Jun 9, 2008
345
0
0
DE
I am trying to determine the size of a file that a user selects in megabytes. My initial method was to open the file and then use the LOF function. However I tested it on a file where this method caused an error because it could not be opened for Append. So I moved to the FileLen function. Windows explorer says that the file is 3,620,736 KB, so it should convert to 3,620.7 MB. However the FileLen function is returning a value of -587333632. Since the FileLen function returns a Long, and the maximum length of a Long is 2147483647, I figured that it filled up all the bits and then perhaps started counting backwards as a negative number (complete guess).

So, with my very limited understanding of bitwise operations, I figured I could use the AND operator to find the difference between the two and then add them together.

My thinking:
0111111111
1110101011
-----------
0110101011 -> then just add that back to 0111111111

Then i could just add this back to the max value of a long by using a Double, which overflowed.

So, any suggestions? Excel 2003 prof.

Code:
Private Sub GetFileName(ByVal objBox As Object, objExtension As Object, objFileSize As Object)
Dim FileName As String, CatchSize As Double, FileSize As Double

FileName = Application.GetOpenFilename
If Not FileName = "false" Then
    objBox.Text = FileName
[COLOR=green]'    FileNum = FreeFile
'    Open FileName For Append As #FileNum
'        On Error Resume Next
'        FileSize = Round(LOF(FileNum) * 0.000000953674316, 2) 'convert bytes to Megabytes [/color]
        CatchSize = FileLen(FileName)
        If CatchSize < 1 Then
            FileSize = (CatchSize And 2147483647) ' + 2147483647
        Else
            FileSize = CatchSize
        End If
        FileSize = Round(FileSize * 0.000000953674316, 2) [COLOR=green]'convert bytes to Megabytes[/color]
        objFileSize.Caption = CStr(FileSize)
[COLOR=green]'        If Err.Number = 52 Then
'            Err.Clear
'            objFileSize.Caption = "???"
'        End If[/color]
        On Error GoTo 0
[COLOR=green]'   Close #FileNum[/color]
    objExtension.Caption = Right(FileName, 4)
Else
    objBox.Text = ""
    objExtension.Caption = ""
    objFileSize.Caption = ""
End If

-JTBorton
Another Day, Another Disaster
 
Are you saying that you are trying to open excel file of 3.6GB! Is it useable? How much memory do you have?

Or are you merely using the method to access file details?

Gavin
 
ha ha no no, I'm allowing a user to attach files onto an email, and I want to disply the size of the file. This was just a file that I stumbled on while testing that didn't test too well.

-JTBorton
Another Day, Another Disaster
 
What about this ?
Code:
...
        CatchSize = FileLen(FileName)
        If CatchSize < 0 Then
            FileSize = 4294967296 + CatchSize
        Else
            FileSize = CatchSize
        End If
...

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks for the suggestions guys. I finally solved it with this:

Code:
Public Sub Add_Attachment(ByVal FileName As String)
Dim FileSize As Double, FilePath As String, objTheFile As Object, objFileSystem As Object, J As Integer
Dim blnSlash As Boolean, FileType As String
    [highlight]Set objFileSystem = CreateObject("Scripting.FileSystemObject")[/highlight]
    On Error Resume Next
    Err.Clear
    [highlight]Set objTheFile = objFileSystem.getfile(FileName)[/highlight]
    If Err.Number = 53 Then 'File not found then
        Err.Source = "Email Engine Add_Attachment"
        MsgBox prompt:="Error number " & Err.Number & vbCrLf & "Source: " & Err.Source & vbCrLf & vbCrLf & Err.Description & vbCrLf & FileName, Buttons:=vbCritical, Title:="Email Engine Error"
        Err.Clear
        Set objFileSystem = Nothing
        Set objTheFile = Nothing
        Exit Sub
    ElseIf Err.Number > 0 Then
        Err.Raise Number:=Err.Number, Source:=Err.Source, Description:=Err.Description, HelpFile:=Err.HelpFile, HelpContext:=Err.HelpContext
    ElseIf objTheFile Is Nothing Then
        Err.Number = 91
        Err.Description = "Object Variable Not Set"
        Err.Source = "Email Engine Add_Attachment"
        MsgBox prompt:="Error number " & Err.Number & vbCrLf & "Source: " & Err.Source & vbCrLf & vbCrLf & "Unable to open file: " & Err.Description & vbCrLf & FileName, Buttons:=vbCritical, Title:="Email Engine Error"
        Err.Clear
        Set objFileSystem = Nothing
        Set objTheFile = Nothing
        Exit Sub
    End If
    On Error GoTo 0
    [highlight]FileSize = objTheFile.Size[/highlight]
    FilePath = objTheFile.Path
    'get the size of the file
    [highlight]FileSize = Application.WorksheetFunction.Round(FileSize / 1048576, 2)[/highlight] 'Size in MB
    
    Set objFileSystem = Nothing
    Set objTheFile = Nothing
End Sub

Now she's purring like a panther!

-JTBorton
Another Day, Another Disaster
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top