How to Programmatically Associate File Extensions with Applications on Windows

batchfile associationfile extensionpowershellwindows

I've just recently reinstalled Windows and in setting up my environment I've noticed that all my associations for the various programming languages I edit in Notepad++ have gone (naturally).

I am thinking for the future, wouldn't it be grand to have some kind of batch file that could automatically associate a selection of file extensions with N++ at the click of a button, rather than having to wait until I encounter all these extensions then go through the rigmarole of navigating to the N++ exe etc.

I can't do this with the Default Programs utility that comes with Windows 7, because it only works with extensions that have been 'encountered'.

So is it possible to programatically associate file extensions with application on Windows?

Best Answer

Use Ftype & Assoc to fix this (and it is scriptable).

Use Assoc to get the filetype

>Assoc .txt

gives you:

.txt = txtfile

Then

>Ftype txtfile=C:\Program Files (x86)\Notepad++\notepad++.exe %1

Once you know the file type you can use Ftype to associate it with an action.

This would work for .php files (just plop them in a batch file)

Assoc .php=phpfile
Ftype phpfile="C:\Program Files (x86)\Notepad++\notepad++.exe" %1

And you can copy these lines to add other text-based files as you would like.

Related Question