Sign in to follow this  
Followers 0
Jon

Best way to fix FileFindFirstFile?

10 posts in this topic

This is a bug report that surprised me.

http://www.autoitscript.com/trac/autoit/ticket/2453

I had no idea that the Windows FindFirstFile/NextFile functions also searched the short filename. I can add a second stage search to ensure that only the long filenames are queried but that may very well break behaviour that people were relying on.

I'm thinking that a possible fix would be an optional 2nd parameter for both FileFindFirstFile and FileFindNextFile that indicates only the long file name should be queried. 

Any better ideas?

Share this post


Link to post
Share on other sites



Out of curiousity I went ahead and did some tests on my own using a few different methods to try to avoid matching shortnames with a wildcard spec:

  • NtQueryDirectoryFile:  I used the FileDirectoryInformation class to purposefully not collect information on short filenames.
    Result: still matched the wildcard spec against shortnames.
  • FindFirstFileEx (vs. FindFirstFile): I used this with fInfoLevelId (FINDEX_INFO_LEVELS enum) set to FindExInfoBasic, to skip shortnames.  Per MSDN it "does not query the short file name," which I had mistakenly assumed to mean that it would not query them as part of the search routine. (Note this option is only recognized on Win7/2008R2+)
    Result: Still matched the wildcard spec against the shortnames, even if not actually collecting them. I would posit that this is because it calls the lower level NtQueryDirectoryFile to do the searching.

So it seems that in order to skip the short filename matches problem, there is no Windows API method that works.. it has to be done using internal checks.  You can't even use PathMatchSpec, as that states it works on a maximum of MAX_PATH characters. This would require you to write your own wildcard string match function (or see "Wildcard string compare (globbing)"), or do a conversion to regular expressions and use the PCRE engine.

To me, this is way too much extra effort (and code) to workaround a Windows API failure.  Better to just document it and let people know about it.

While on the subject, it would probably be good to note (and to document) that FileGetShortName() and FileGetLongName() will fail on Windows systems where short filenames are disabled - which is more common in corporate environments where keeping short filenames impacts performance. (See 'Windows Server 2012 File Server Tip: Disable 8.3 Naming (and strip those short names too)' and  'How to Disable the 8.3 Name Creation on NTFS Partitions')

P.S. As an aside, on Win7/2008R2+, there's supposed to be performance gains in searching using FindFirstFileEx when dwAdditionalFlags is set to FIND_FIRST_EX_LARGE_FETCH, and fInfoLevelId set to FindExInfoBasic

Share this post


Link to post
Share on other sites

MS handling of filenames is terrible. Note that you can't even characterize a short name by a tilde, since the tilde is a valid character in filenames.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

Short names aren't even useful anymore. Why are they a feature?

I'd say it'd be better to just document it since all other Windows APIs do the same thing.

Share this post


Link to post
Share on other sites

Short names have a use if you want to be lazy and not have to worry about quoting a file path and name to avoid spaces.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

Short names have a use if you want to be lazy and not have to worry about quoting a file path and name to avoid spaces.

But that only works if you know for sure what the short name will become and if you're writing a program, you should never expect the end user to use short names.

Share this post


Link to post
Share on other sites

That's where filegetshortname comes in, you don't need to know what it is, you just need to have something to give to that function.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

So you're wasting extra time getting the short name when you could just put quotes around it?

Share this post


Link to post
Share on other sites

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0