Programatically associate file extensions with application on Windows

46

19

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?

deed02392

Posted 2012-03-31T13:29:07.900

Reputation: 2 662

See also https://stackoverflow.com/q/212906/10245

– Tim Abell – 2018-04-05T19:08:01.910

Does the solution have to be powershell as per your tag? – Richard – 2012-03-31T13:48:16.413

1Not at all, just trying to get the interest of the Windows command line junkies. :) – deed02392 – 2012-03-31T13:53:50.600

Use Windows Easy Transfer to transfer your system settings, its built into Windows 7-Vista and can be downloaded for XP...http://www.microsoft.com/download/en/details.aspx?id=7349

– Moab – 2012-03-31T14:24:09.670

That's not an option since the original Windows install isn't available anymore. – deed02392 – 2012-03-31T14:40:29.543

Answers

67

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.

uSlackr

Posted 2012-03-31T13:29:07.900

Reputation: 8 755

1For some reason, after a while all my notepad++ associations switch to "Office XML Handler". However if I do an Assoc on any of those extensions, it always gives "Notepad++_file" and ftype says that Notepad++_file is associated with "MSOXMLED.EXE" somehow. Anyways this answer helped me set association of "Notepad++_file" back to notepad++.exe without going through each and every file extension. – Waqas Ilyas – 2018-03-01T18:13:56.660

1Interesting, when I do >assoc .php it says it has no file association? It seems what is set up when you specify a default program to open a file type is not 'registered' in this way. Why might that be? – deed02392 – 2012-03-31T18:35:50.193

19+1 for two command line tools that i didn't know existed. – Ian Boyd – 2012-03-31T19:41:56.633

1This solution makes text files open up in notepad++ and not the previously assigned program (notepad). It won't make notepad++ open when you click on a new, unregistered, file extension (eg. pl, py or c) like the submitter asked. – Richard – 2012-04-01T09:34:13.043

@Richard I just re-read the post an I don't see where he asks that. – uSlackr – 2012-04-01T19:52:30.890

1@deed02392 you can add a new association with assoc 'Assoc .php=phpfile' should work – uSlackr – 2012-04-01T19:53:26.957

1The bit "some kind of batch file that could automatically associate a selection of file extensions with N++" - although it could have been me misunderstanding it! – Richard – 2012-04-01T21:51:15.357

You understood correctly Richard, the solution should automate the recognition of previously unencountered file extensions and associate notepad++ as the open handler for these files. – deed02392 – 2012-04-03T10:54:08.347

2The intent of my answer was to show you how to use assoc & ftype, not to write the batch file for you. But I'll play along – uSlackr – 2012-04-03T19:56:15.643

+1 Did not know these commands. Found this very useful!! – Angshuman Agarwal – 2012-06-20T16:16:56.410

5

Here's a script that worked for me on Windows 10

$exts=@("txt","log","csproj","sql","xml","flobble")
echo "## setting up file associations"
foreach ($ext in $exts){
    $extfile=$ext+"file"
    $dotext="." + $ext
    cmd /c assoc $dotext=$extfile
    cmd /c "ftype $extfile=""C:\Program Files (x86)\Notepad++\notepad++.exe"" %1"
    echo ""
}
  • Needs to be run in an administrator (elevated) powershell window.
  • Explorer immediately refreshed and showed new file icons.

https://gist.github.com/timabell/bc90e0808ec1cda173ca09225a16e194

Thanks to the other answers for the information I needed to make this work.

Tim Abell

Posted 2012-03-31T13:29:07.900

Reputation: 349

4

At the minimum, you need to create one registry key which gives notepad++ an ID and path and then one for each extension you wish to register to use it.

To create the ID and path (check the path points to the correct location):

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\notepad_pp]
@=""

[HKEY_CLASSES_ROOT\notepad_pp\shell]

[HKEY_CLASSES_ROOT\notepad_pp\shell\open]

[HKEY_CLASSES_ROOT\notepad_pp\shell\open\command]
@="\"C:\\Program Files (x86)\\Notepad++\\notepad++.exe\" \"%1\""

and then keep repeating the next bit, one for each extension (in this example, .pl is for Perl):

[HKEY_CLASSES_ROOT\.pl]
@="notepad_pp"

Save this file with the extension .reg and you should now be able to re-associate all the extensions just by double-clicking on this file and confirming you want to import the entries into the registry.

Richard

Posted 2012-03-31T13:29:07.900

Reputation: 4 197

1Interesting, I've been looking and it seems what Windows 7 does when you set a program to load from is create an entry in HKCR\ext_auto_file\shell\open\command with a value of "C:\Program Files\Notepad++\notepad++.exe" "%1". Where were you basing yours from? – deed02392 – 2012-03-31T18:26:03.023

It's basically the same, just with ext_auto_file renamed to something slightly more meaningful and multiple file extensions pointing to one single entry. – Richard – 2012-03-31T22:14:20.263