Tuesday, August 14, 2012

Assigning a default handler to extensionless files in Windows

I recently ran into a large number of files that have no extension. My attempt at batch processing this files was hindered. In Microsoft Windows, if you double click on a file that has no extension, the "Open With" dialog will appear and ask you which program to use to open the file.

Normally, you could choose the program and then check the box "Always use the selected program to open this kind of file" and then the file open process would be automated going forward. When the file has no extension, that box is greyed out, so the automated file opening by a certain program cannot happen.

To fix this (to cause a file without an extension to open in Notepad.exe by default) I looked into the registry.

The registry key Computer\HKEY_CLASSES_ROOT\.txt contains the instructions for the ".txt" extension.

".txt" (Key)
---- (Default) = "txtfile" - (String)
---- Content Type = "text/plain" - (String)
---- PerceivedType = "text" - (String)
---- PersistentHandler (Key)
-------- (Default) = "{5e941d80-bf96-11cd-b579-08002b30bfeb}" - (String)
---- ShellNew (Key)
-------- etc...

What I did was copy the strings and the PersistentHandler registry key into a new sibling without the "txt" portion.

" ." (Key)
---- (Default) = "txtfile" - (String)

---- Content Type = "text/plain" - (String)
---- PerceivedType = "text" - (String)
---- PersistentHandler (Key)
-------- (Default) = "{5e941d80-bf96-11cd-b579-08002b30bfeb}" - (String)

The logic behind this is that ".txt" opens ".txt" files with a certain handler and treats them a certain way. A file without an extension is effectively just "." instead of ".txt."

To my surprise, it works. Any file without an extension is automatically opened in Notepad.exe.

No comments:

Post a Comment