+ Reply to Thread
Results 1 to 6 of 6

Use Windows API to determine if string is a file or a folder

  1. #1
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Use Windows API to determine if string is a file or a folder

    I am looking for a function that will use a Windows API to check whether the passed string is a file or a folder.

    I used to use GetAttrib() but I can no longer rely on this as it runs into an error on long file names (i.e. when folder path exceeds 247 characters or file fullpath exceeds 260 characters)(218 len if the file is an excel workbook)

    I have worked out a function that does this in FileSystemObject but I would prefer to use a Windows API to avoid the performance hit of FSO.
    *******************************************************

    HELP WANTED! (Links to Forum threads)
    Trying to create reusable code for Custom Events at Workbook (not Application) level

    *******************************************************

  2. #2
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Use Windows API to determine if string is a file or a folder

    I'd have thought that this should cover it, you can use the Unicode version if you need even longer paths. See: https://docs.microsoft.com/en-us/win...ectedfrom=MSDN.

    This should also answer your other question

    Please Login or Register  to view this content.

  3. #3
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Use Windows API to determine if string is a file or a folder

    Many thanks for this Kyle.

    I have tried to rewrite the function to support extended paths. I rarely use Windows APIs so I'm not sure what I'm doing! Is the code below OK for GetFileAttributesW ?

    Also does anybody know if there is a separate 64 bit version of GetFileAttributesW or do I just use PtrSafe prefix?

    Please Login or Register  to view this content.

  4. #4
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Use Windows API to determine if string is a file or a folder

    My modified function 'fnfptPathType' returns -1 for all arguments passed. However if I call this from another function 'fnblnPathStringIsFolder' it works?!

    The 'fnblnPathStringIsFolder' function has passed my testing so I am using that.

    Thanks again Kyle. (The forum wont let me rep you because I just repped you for the function that uses windows API to verify file/folder exists)

    Please Login or Register  to view this content.

  5. #5
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Use Windows API to determine if string is a file or a folder

    Your code doesn't work, since you're calling a unicode function, you need to pass the stringas unicode:
    Please Login or Register  to view this content.
    Should be:
    Please Login or Register  to view this content.
    Also, this is a bit nasty:
    Please Login or Register  to view this content.
    This would be cleaner and easier to test:
    Please Login or Register  to view this content.
    Then:
    Please Login or Register  to view this content.

  6. #6
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Use Windows API to determine if string is a file or a folder

    Quote Originally Posted by Kyle123 View Post
    Your code doesn't work, since you're calling a unicode function, you need to pass the stringas unicode:
    Please Login or Register  to view this content.
    Should be:
    Please Login or Register  to view this content.
    Thanks Kyle. I have corrected this.

    Quote Originally Posted by Kyle123 View Post
    Also, this is a bit nasty:
    Yes I agree it's messy. I have use for a module level variable here to be used by other functions in this module (I didn't mention these functions earlier since they are outside the focus of my original question)

    Thanks again for your help Kyle. I apologise that I am unable to rep you for solving both threads as the forum wont allow me to rep the same user in succession!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Deleting a file or a folder using the Windows Gadget Platform
    By Ronen_H in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-12-2016, 12:06 PM
  2. how to determine if a file opens on 2 windows or not
    By shraga in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-24-2010, 12:09 PM
  3. Windows API to Determine if File is Local/Convert Path to Drive Letter
    By Johnny in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-10-2006, 09:30 AM
  4. Replies: 0
    Last Post: 11-16-2005, 01:52 PM
  5. Replies: 4
    Last Post: 03-05-2005, 04:06 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1