none
FileLen Function does not return the expected value RRS feed

  • Question

  • Dear All,

    I'm using the function FileLen to display the size of files, but even when I have the file path, it is returning null. Under the code of the function:

    ....

    If FileExists(fullpath) then

        Dim oFS

        Set oFS = CreateObject ("Scripting.FileSystemObject")

        If oFS.getFileLength(fullpath) = "" Then

              getFileLength = "No Length Available"

        Else

              getFileLength = FileLen(fullpath)

        End If

        Set oFS = Nothing

    Else

       getFileLength = ""

    End If

    ...

    Regards,

    Rafael.

    Wednesday, February 19, 2014 12:06 PM

All replies

  • re:  file size and the file system object

    Maybe...
    Sub Test()
        Dim oFS As Object
        Dim oFile As Object
        Dim fullpath As String
        Dim vAnswer As String
        
        fullpath = "C:\Program Files\FastStone Capture\Tips.db"   '<<<<
        Set oFS = CreateObject("Scripting.FileSystemObject")

        If oFS.FileExists(fullpath) Then
          Set oFile = oFS.GetFile(fullpath)
          vAnswer = "File size is " & oFile.Size & " bytes   " & vbCr & _
                          "Path length is " & VBA.Len(fullpath) & " characters   "
        Else
          vAnswer = "No Length Available "
        End If
        MsgBox vAnswer
        Set oFile = Nothing
        Set oFS = Nothing
    End Sub
    '---

    Another way...
    Sub moreTests()
        Dim fullpath As String
        Dim sAnswer As String
        
        fullpath = "C:\Program Files\FastStone Capture\Tips.db"
        sAnswer = "File size is " & FileLen(fullpath)
        MsgBox sAnswer
    End Sub
    '---

    Jim Cone
    Portland, Oregon USA
    https://goo.gl/IUQUN2 (Dropbox)
    (free & commercial excel add-ins & workbooks)

    • Edited by James Cone Sunday, October 30, 2016 11:46 PM
    Wednesday, February 19, 2014 1:50 PM
  • Thanks James, I will test and then post the result.
    Wednesday, February 19, 2014 7:00 PM
  • James,

    I tested the solution but it still has problems, below is the complete code:

    Function getFileSize(ByVal serverName As String, ByVal strPath As String, ByVal strFilename As String, ByVal strEnv As String) As String

             Dim fullPat As String

             Dim fullpath As String

             If UCase(serverName) = UCase (nameserver) Then

                   fullpath = "\\" & serverName & strPath & strFilename

             ElseIf serverName = "nameserver" And strEnv = "Name Environment" Then

                   fullpath = "\\" & serverName & "\c$" & strPath & strFilename

             Else

                  fullpath = "\\" & serverName & "\c$" & strPath & strFilename

             End If

             If FileExists(fullpath) then

                Dim oFS

                Set oFS = CreateObject ("Scripting.FileSystemObject")

             If oFS.getFileLength(fullpath) = "" Then

                getFileSize = "No Length Available"

             Else

                getFileSize= FileLen(fullpath)

            End If

                Set oFS = Nothing

            Else

              getFileSize = ""

           End If
    End Function
    Thursday, February 20, 2014 11:36 AM
  • Re: code still has problems

    Some questions...

    What computer operating system are you using?
    What Office program is the code running in?
    Which one of these three is correct:
       serverName
       nameserver
       "nameserver"
    Do you know how to step (move) thru code using the F8 key?
       If so, at what code line does the "problem" occur and what is the message displayed?
    '---
    Jim Cone

    Thursday, February 20, 2014 3:29 PM
  • Hi Jim,

    I'm using XP SP 3 and Office 2007.

    nameserver = Name of Server

    serverName is a variable

    "nameserver" = is a Name of Server in the fullpath variable.

    Thanks for help me.

    Regards,

    Rafael

    Thursday, February 20, 2014 6:52 PM
  • Step where the problem occurred is entered into the loop function:

    Function DigIn (sPath As String, sWhat As String) As String

                  Dim fs

                  Dim dDirs

                  Dim dDir

                  Dim fFile

                  Dim c        As Variant

                  Dim tmp   As String

                  Set fs = CreateObject("Scripting.FileSystemObject")

                  Set dDirs = fs.GetFolder(sPath &  "\")

                  For Each dDir In dDirs.SubFolders

                        tmp = DigIn(dDirs.Path & "\" & sWhat)

                  If tm <> "" Then

                        Do

                             lst.Add dDirs.Path & "\" & tmp

                             tmp = Dir

                         Loop Until tmp = ""

                         Exit Function

                   End If

     End Function

    Regards,

    Rafael

                  


    Thursday, February 20, 2014 7:11 PM
  • Re:  FileLen function

    Sorry, I am unable to help.
    '---
    Jim Cone
    Thursday, February 20, 2014 7:41 PM