Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an alternative to the Windows FindFirstFile/FindNextFile API that doesn't search short file names?

I'm using the Windows API calls FindFirstFile and FindNextFile to search for files matching a certain wildcard string in a directory. For example, I might want to find all XML files in a directory, and so I search using the pattern "*.xml".

I'm running into the problem that if I'm searching for a 3-letter extension, and a file's extension starts with that 3-letter extension, it will get picked up by my search, even if the extension contains more characters after those first 3. For example, if my file is named somelongname.xmlaaaa, this will get picked up by the search for "*.xml". The short name of that file is somelo~1.xml, which matches my wildcard string.

I could do my own wildcard matching to get around this problem, but presumably a Windows API would be able to do this more efficiently than I could, and my code would be simpler. As far as I can tell there isn't a way to force these functions to ignore short names. Is there any API Windows exposes with this functionality?

like image 444
Chris Vasselli Avatar asked Nov 09 '11 19:11

Chris Vasselli


1 Answers

Check out FindFirstFileEx - it has a fInfoLevelId parameter that takes a FINDEX_INFO_LEVELS enumeration:

typedef enum _FINDEX_INFO_LEVELS {
  FindExInfoStandard,
  FindExInfoBasic,
  FindExInfoMaxInfoLevel 
} FINDEX_INFO_LEVELS;

FindExInfoBasic

The FindFirstFileEx function does not query the short file name, improving overall enumeration speed. The data is returned in a WIN32_FIND_DATA structure, and the cAlternateFileName member is always a NULL string.

However:

Windows Server 2008, Windows Vista, Windows Server 2003, and Windows XP: This value is not supported until Windows Server 2008 R2 and Windows 7.

like image 67
JoeFish Avatar answered Oct 11 '22 07:10

JoeFish