3

I am trying to associate two file extensions with my program (an .exe file), lets say that they are ext1 & ext2.

I want to associate ext1 files with my program in a way that if it is shell executed, this commandline (or command) should run\execute:

my_program.exe shell_execute ext1 "<full path of the file>"

Similarly for ext2:

my_program.exe shell_execute ext2 "<full path of the file>"

How do I associate the file extensions to my program?

user4157124
  • 2,809
  • 13
  • 27
  • 42
TheDcoder
  • 509
  • 1
  • 7
  • 23
  • That's not an AutoIt related question, but more a Windows Registry Problem... so you should remove this tag ;-) – Samoth Feb 23 '16 at 23:13
  • I am trying to do this in AutoIt so I tagged it :P – TheDcoder Feb 24 '16 at 05:55
  • [related](https://stackoverflow.com/a/35239117/2152082) (although not native AutoIt but `cmd`, but shouldn't be too difficult to [Run](https://www.autoitscript.com/autoit3/docs/functions/Run.htm) or [RunWait](https://www.autoitscript.com/autoit3/docs/functions/RunWait.htm)) – Stephan Mar 31 '20 at 20:14

1 Answers1

3

Here is a simple file association solution,

; e.g. 
;_FiletypeAssociation('.test', 'test', 'notepad "%1"', 'test description')
;_FiletypeAssociation('.pdf', 'FoxitReader.Document', '"%ProgramFiles%\FoxitReader.exe" "%1"')

Func _FiletypeAssociation($extension, $type, $program, $description = '')
    Local $sHKCR = @OSArch = 'x64' ? 'HKCR64' : 'HKCR'

    $exitcode = RunWait(@ComSpec & ' /c ftype ' & $type & '=' & $program & _
             ' && assoc ' & $extension & '=' & $type, '', @SW_HIDE)
    If $description And Not $exitcode Then
        Return RegWrite($sHKCR & '\' & $type, '', 'Reg_sz', $description)
    EndIf
    Return Not $exitcode
EndFunc
Milos
  • 2,927
  • 1
  • 15
  • 27
  • Here is what I tried: `_FiletypeAssociation('.txt', 'htmlfile', '"%ProgramFiles%\Google\Chrome\Application\chrome.exe" "%1"', 'test description')`. The script was launched with administrative privileges and seems to work correctly: `assoc .txt` gives me `.txt=htmlfile`. However, when I double-click txt file, it is opened with Sublime Text (this is my default) instead of Chrome. Could you explain what I'm doing wrong? – john c. j. Mar 30 '20 at 13:12
  • On 64bit you may need to use HKCR64. See the updated code – Milos Mar 31 '20 at 14:05
  • 1
    @johnc.j.: check also `ftype htmlfile` – Stephan Apr 01 '20 at 14:02