What means tilde in windows file pattern

2.9k views Asked by At

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.

2

There are 2 answers

1
Alex K. On BEST ANSWER

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.

2
Engin Sözer On

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.