none
Getting File Size RRS feed

  • Question

  • Windows 10 Pro x64
    Access 2010

    I am trying to walk directories and capture the files and file sizes.

    If the path\filename is very long (i.e 263 characters) and I try to use fso.GetFile(sPathFile).Size
    It throws an error (file not found).

    If I spawn: "CMD /C ""DIR """ & vsPath & "\" & sFileName & """ > " & gsWorkFile & """"
    to try and get the file size it also returns file not found.

    In fact, if I open a cmd window and change to the directory and do dir <file name> it also returns file not found.
    But if I just do dir it lists the files including the one I am trying to find the size of.

    Is there some way to get the size of a file whose path\filename is very long?


    http://www.saberman.com

    Wednesday, March 28, 2018 5:29 AM

Answers

  • The code at the link uses a file system object which is what I am using.

    ?sPathFile
    O:\OldStuff\NYXP-0012\Machines\NYW7-0011\CDrive\USers\SABerman\AppData\Roaming\com.nyt.timesreader.78C54164786ADE80CB31E1C5D95607D0938C987A.1\Local Store\nyt\20110909\images\2011\09\09\business\dbpix-judge-edward-harrington-private\dbpix-judge-edward-harrington-private-articleInline.jpg

    ?len(sPathFile)
     287


    http://www.saberman.com

    Hi,

    I just gave it a try and I didn't get any error. So, I am guessing the problem might be in the use of GetFile().

    What I did to try it was I modified this List Files Demo to show the file sizes as well and then I created a Text file using the long file name you posted. The demo does not use the GetFile() method, so I am guessing maybe that is why it worked for me. Instead, it uses the following construct:

    For Each var in fsoFolder.Files
         strFiles = ";" & var.Path & ";" & var.Size & strFiles
    Next

    Just my 2 cents...

    Here's a screenshot of what I did. Hope it helps...



    • Edited by .theDBguy Wednesday, March 28, 2018 4:00 PM
    • Marked as answer by saberman Wednesday, March 28, 2018 5:12 PM
    Wednesday, March 28, 2018 3:53 PM

All replies

  • I use http://www.devhut.net/2013/09/08/vba-list-of-files-within-a-folder-and-get-their-properties/ and have never encountered any issue.

    What does your sPathFile variable look like when it err? Can you supply an example.


    Daniel Pineault, 2010-2017 Microsoft MVP
    Professional Support: http://www.cardaconsultants.com
    MS Access Tips and Code Samples: http://www.devhut.net


    Wednesday, March 28, 2018 10:13 AM
  • The code at the link uses a file system object which is what I am using.

    ?sPathFile
    O:\OldStuff\NYXP-0012\Machines\NYW7-0011\CDrive\USers\SABerman\AppData\Roaming\com.nyt.timesreader.78C54164786ADE80CB31E1C5D95607D0938C987A.1\Local Store\nyt\20110909\images\2011\09\09\business\dbpix-judge-edward-harrington-private\dbpix-judge-edward-harrington-private-articleInline.jpg

    ?len(sPathFile)
     287


    http://www.saberman.com

    Wednesday, March 28, 2018 3:25 PM
  • The code at the link uses a file system object which is what I am using.

    ?sPathFile
    O:\OldStuff\NYXP-0012\Machines\NYW7-0011\CDrive\USers\SABerman\AppData\Roaming\com.nyt.timesreader.78C54164786ADE80CB31E1C5D95607D0938C987A.1\Local Store\nyt\20110909\images\2011\09\09\business\dbpix-judge-edward-harrington-private\dbpix-judge-edward-harrington-private-articleInline.jpg

    ?len(sPathFile)
     287


    http://www.saberman.com

    Hi,

    I just gave it a try and I didn't get any error. So, I am guessing the problem might be in the use of GetFile().

    What I did to try it was I modified this List Files Demo to show the file sizes as well and then I created a Text file using the long file name you posted. The demo does not use the GetFile() method, so I am guessing maybe that is why it worked for me. Instead, it uses the following construct:

    For Each var in fsoFolder.Files
         strFiles = ";" & var.Path & ";" & var.Size & strFiles
    Next

    Just my 2 cents...

    Here's a screenshot of what I did. Hope it helps...



    • Edited by .theDBguy Wednesday, March 28, 2018 4:00 PM
    • Marked as answer by saberman Wednesday, March 28, 2018 5:12 PM
    Wednesday, March 28, 2018 3:53 PM
  • Does the built-in VBA function FileLen() work, or does it also have a problem with the length of the path?

    Dirk Goldgar, MS Access MVP
    Access tips: www.datagnostics.com/tips.html

    Wednesday, March 28, 2018 3:55 PM
  • Sorry, I take it back. When I created a long folder structure, it gave me an error too.

    Using FileLen() also failed with a "Path not Found" error.

    Hmm...

    Wednesday, March 28, 2018 4:15 PM
  • The official Windows API maximum path length is 260, and I expect that's the issue.  According to this article it's possible in Windows 10 Anniversary Edition to modify the registry or group policy to remove that limit (if my quick scan is correct).  I think earlier versions are out of luck as far as that change goes.  However, you could use short-name syntax to make the path shorter, or map a folder somewhere down that path to a drive letter and build your path from there.

    Dirk Goldgar, MS Access MVP
    Access tips: www.datagnostics.com/tips.html

    Wednesday, March 28, 2018 4:32 PM
  • It works for the sample but your demo only shows the short name of the file.

    ?f.Name
    DBPIX-~1.JPG
    ?f.Path
    O:\OldStuff\NYXP-0012\Machines\NYW7-0011\CDrive\USers\SABerman\AppData\Roaming\com.nyt.timesreader.78C54164786ADE80CB31E1C5D95607D0938C987A.1\Local Store\nyt\20110909\images\2011\09\09\business\dbpix-judge-edward-harrington-private\DBPIX-~1.JPG
    ?len(f.Path)
     244

    Don't know if it is a bug in the file object or if it uses the short name when the path\file length gets too long.

    Since it uses the short name for the file the length of the path with the file name is only 244. 

    I am using the FindFirstFileW and FindNextFileW APIs which provide the long file name.

    I will integrate some of your code for when the length of the path\filename gets too long.

    Edit:
    f.ShortPath is the same as f.Path (long directory names)
    f.Name is the same as f.ShortName (short file name)

    Edit2:
    I was wrong about it only showing the short name of the file.  Actual, it only enumerates through the files whose names are short.  I think the directory structure maybe corrupted.


    http://www.saberman.com



    • Edited by saberman Wednesday, March 28, 2018 10:46 PM
    Wednesday, March 28, 2018 5:12 PM
  • Hi,

    Glad to hear you found a workaround. If I find a better approach, I'll let you know. Good luck with your project.

    Wednesday, March 28, 2018 5:18 PM
  • The only way I could get it to work was to walk the folder.files collection.  But that doesn't provide the long file name.  var.name and var.shortname return the same short name.

    I need to find something that will return the short file name from the long one.  That way I can use the folder object to get the file object and then the size.

    Edit:
    Again, the problem was that it did not see the files with longer names. 


    http://www.saberman.com


    • Edited by saberman Wednesday, March 28, 2018 10:47 PM
    Wednesday, March 28, 2018 5:26 PM
  • Hi,

    In your original code, how exactly were you populating sPathFile? In other words, how were you getting the long file name to begin with? For example, do you use a FileDialog object to let the user browse to it?

    Wednesday, March 28, 2018 5:49 PM
  • >In your original code, how exactly were you populating sPathFile?
    Using the FindFirstFileW and FindNextFileW APIs

    http://www.saberman.com

    Wednesday, March 28, 2018 5:53 PM
  • >In your original code, how exactly were you populating sPathFile?
    Using the FindFirstFileW and FindNextFileW APIs

    http://www.saberman.com

    Okay, so if you're using APIs to get the file names, have you tried also using the API version of GetFileSize rather than using the FSO version?

    Just curious...

    Wednesday, March 28, 2018 6:05 PM
  • I tried using:
        Private Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal cchBuffer As Long) As Long

        Dim sShortPath As String * 255

    If I try:
    lReturn = GetShortPathName(sPathFile, sShortPath, 255)
    where sPathFile has the path and the file name it returns 0

    If I Try:
    ?GetShortPathName(vsPath,sShortPath,255)
    where vsPath only contains the path it returns 231 which is the length of vsPath and sShortPath is the same as vsPath -- has long directory names.

    How can I get the short name version of the PathFilename string?


    http://www.saberman.com

    Wednesday, March 28, 2018 7:25 PM