53

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
  • 3,132
  • 6
  • 30
  • 36

4 Answers4

71

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
  • 9,053
8

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.

KERR
  • 624
Tim Abell
  • 395
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
  • 6,420
0

assoc & ftype commands no longer works, because file type association is now protected by a secret hash in the registry ... And I can tell You, because I tried it ...

Here is the solution :

  1. https://kolbi.cz/blog/2017/10/25/setuserfta-userchoice-hash-defeated-set-file-type-associations-per-user/

  2. https://setuserfta.com

Hope this help

DjiPih
  • 11