3

I have pattern to search. Say "*.txt".

Now I have some files I do not want to list there. I believe they do not match this pattern. But on windows, they do.

I know tilde character is used to make short form of legacy 8.3 filename. That is LongFilename.json might be LONGFI~1.JSO. But I did not know they are handled somehow on windows in file search patterns. They are. I cannot find any documentation about what they mean and how to match files my way.

My problem is NOT with short forms. Or I think it is not directly related to it.

I have file "A.txt". Now I wanted temporary file and used "A.txt~". It is unix backup files that is not usually visible. But on windows, they should not have special meaning by itself. Only for my application.

Now I want list of "*.txt" files. Command dir *.txt returns to my surprise also all .txt~ files in the same directory. And I do not want them. I use FindFileFirst from Win32 API. I did not find anything about tilde character in documentation. FindFileFirst(".txt", handle) returns also files "A.txt~". Can I use some flag to exclude them? I know I can make special condition, like I have for "." and "..". How does ~ operator work? A.txt~1 is also matched. Is everything after tilde ignored? Is that feature or bug?

I am testing that on Windows 7 Professional, 64 edition, if that changes anything.

Alex K.
  • 159,548
  • 29
  • 245
  • 267
Pihhan
  • 807
  • 4
  • 11
  • I've seen something like this too. In my case, it wasn't with the tilde. To use your example, I was searching for "*.txt", but it was matching as though I'd said "*.txt*". Rename your file so the extension is ".txt1" and see if it still gets matched. It might not be about the tilde, but about how that function performs a match. – Darren Jan 22 '14 at 13:19
  • Oh! You are right of course. I did not even consider it might be that way. It seems I am unable to match file ending with something, because there is implicit * at the end. A.txt1 really matches dir *.txt :-( – Pihhan Jan 22 '14 at 13:30

2 Answers2

4

FindFirstFile also includes short names for legacy reasons so the pattern *.txt will include anything with an 8.3 representation ending in *.txt which includes *.txtANYTHING , not just the ~ character (see dir /xfor what's being matched against).

You will need to filter in your FindNext enumeration.

Alex K.
  • 159,548
  • 29
  • 245
  • 267
  • Yes, it seems so. It only applies to suffix of length 3. *.html does not match *.html1. But *.htm does match *.html and *.html1. Strange side effects of legacy files handling. – Pihhan Jan 22 '14 at 13:39
0

If you are searching for .txt files for example, you can use "kind:text" option in windows to exclude txt~ and similar files since they are not a recognized type anymore.

That's something that works on regular windows search. I'm not 100% sure about the API, but it should also be there.

Engin Sözer
  • 97
  • 1
  • 6
  • Problem is I use old Win32 in application, where I found problems with that. Where would I enter "kind:text"? I do not want to use File Explorer. – Pihhan Jan 22 '14 at 13:37
  • This is more like an SQL approach. This page might help. I'm not really the expert on the topic but this worths a shot I guess. http://msdn.microsoft.com/en-us/library/windows/desktop/bb266512%28v=vs.85%29.aspx – Engin Sözer Jan 22 '14 at 14:42