11/12/2015

Associate Some Type of File with Some Program (Windows)


Did You even notice that when you double click some file in Windows Explorer, Some program will open that file for you. How does Explorer remember that THIS Type of file should be opened with THIS Program. So, This behavior is managed by File Type Association Record, and THIS record is saved in Windows Registry. OK, Now we will take a look at all Possible way to associate some kind of file with some program.

Some Kind or Type of File only refers to File Extension, By Default, in Windows You will not be able to see file extension, But i am assuming You are familiar with Media Files, like Audio and Video Files, and You may know that mp3 files, means File Extension is .mp3 So, for example, sample_media_file.mp3 (File Extension is .mp3) and some video file, like sample_media_file.mp4 (File Extension is .mp4), sample_media_file.mkv

Method 1 (Manual Method)

  • We want to change program to open .mp4 files.
  • Right Click on File and in Context menu click, Properties
  • Now in Properties -> General Tab Click Change Button
  • New windows appears asking you to choose any program from list, If Program that you are looking for, is not in list, then you manually locate that program.
  • After choosing/Selecting THAT program, Click Apply button and then OK Button
Note:
  1. This Method Generate File Type Association Registry only for Current User. You can look at Generated Registry using regedit.exe
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts.mp4
  1. Command line that will be Executed when you Double click on some .mp4 file. program.exe %1

Method 2 (Using Command Line)

  • In this Method we will be using two command line programs (ASSOC and FTYPE)
  • Lets take a look at assoc program help
C:\Windows\system32>assoc /?
Displays or modifies file extension associations

ASSOC [.ext[=[fileType]]]

.ext      Specifies the file extension to associate the file type with
fileType  Specifies the file type to associate with the file extension

Type ASSOC without parameters to display the current file associations.
If ASSOC is invoked with just a file extension, it displays the current
file association for that file extension.  Specify nothing for the file
type and the command will delete the association for the file extension.
  • We want to declare all files with .mp4 extension as MediaFile
assoc .mp4=MediaFile
  • Now take a look at ftype program help
C:\Windows\system32>ftype /?
Displays or modifies file types used in file extension associations

FTYPE [fileType[=[openCommandString]]]

fileType  Specifies the file type to examine or change
openCommandString Specifies the open command to use when launching files
of this type.

Type FTYPE without parameters to display the current file types that
have open command strings defined.  FTYPE is invoked with just a file
type, it displays the current open command string for that file type.
Specify nothing for the open command string and the FTYPE command will
delete the open command string for the file type.  Within an open
command string %0 or %1 are substituted with the file name being
launched through the assocation.  %* gets all the parameters and %2
gets the 1st parameter, %3 the second, etc.  %~n gets all the remaining
parameters starting with the nth parameter, where n may be between 2 and 9,
inclusive.  For example:

ASSOC .pl=PerlScript
FTYPE PerlScript=perl.exe %1 %*

would allow you to invoke a Perl script as follows:

script.pl 1 2 3

If you want to eliminate the need to type the extensions, then do the
following:

set PATHEXT=.pl;%PATHEXT%

and the script could be invoked as follows:

script 1 2 3
  • Now we need to define command line that should be executed every time User Double Click on .mp4 Files.
ftype MediaFile="C:\Program Files\MPV_Player\mpv.exe" "%1"
  • Equivalent Registry generated because of these two Commands ( assoc and ftype )
----------------------------------
Keys added:5
----------------------------------
HKLM\SOFTWARE\Classes\.mp4
HKLM\SOFTWARE\Classes\MediaFile
HKLM\SOFTWARE\Classes\MediaFile\Shell
HKLM\SOFTWARE\Classes\MediaFile\Shell\Open
HKLM\SOFTWARE\Classes\MediaFile\Shell\Open\Command

----------------------------------
Values added:2
----------------------------------
HKLM\SOFTWARE\Classes\.mp4\: "MediaFile"
HKLM\SOFTWARE\Classes\MediaFile\Shell\Open\Command\: "C:\Program Files\MPV_Player\mpv.exe" "%1"

Method 3 (Using PowerShell)

  • Windows PowerShell is really cool, because this allows you to work with Windows Registry the same way as you work with FileSystem.
cd HKLM:
New-Item .\SOFTWARE\Classes\.001 -Value "first"
New-Item .\SOFTWARE\Classes\first
New-Item .\SOFTWARE\Classes\first\shell
New-Item .\SOFTWARE\Classes\first\shell\open
New-Item .\SOFTWARE\Classes\first\shell\open\command -Value '"mpv.exe" "%1"'

General Method (Add/Modify Both Global as well as Local Registry)


WHY?

To associate some type of files with some program, all You need to do is run the following command. assoc ftype
But if User choose some program manually, then Explorer create a list of program with first index for previously chosen User Program, This is why in this case, First You need to execute both above commands after that You have to add THIS application to User Chosen Programs and Update Index.
Now, When User Double click on THAT kind of File, Explorer will give him/her a choice that Hey! I found this New Program, That may be chosen by You, But The second one will be what You chosen last time.


Command Line Method

@rem -- setup a new application and path for application
REG ADD "HKEY_CLASSES_ROOT\Applications\mpv.exe\shell\open\command" /v @ /t REG_SZ /d "\"F:\Softwares\mpv\mpv.exe\"  --autofit=50%% --force-window --ontop --no-border \"%%1\"" /f
@rem -- register THIS file type to use THAT application
REG ADD "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.mp4" /v "Application" /t REG_SZ /d "mpv.exe" /f
REG ADD "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.mp4\OpenWithList" /v "g" /t REG_SZ /d "mpv.exe" /f
REG ADD "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.mkv\OpenWithList" /v "MRUList" /t REG_SZ /d "gabcdef" /f

assoc .mp4=MediaFile
ftype MediaFile="F:\Softwares\mpv\mpv.exe" --autofit=50%%%% --force-window --ontop --no-border "%%1"

PowerShell Method

# Register a New Application (demo.exe) and command line string for it ..
cd HKLM:\SOFTWARE\Classes\Applications\
New-Item demo.exe
New-Item demo.exe/shell
New-Item demo.exe/shell/open
New-Item demo.exe/shell/open/command -Value 'C:\demo.exe %1 --full'

# Now Associate Your Program (demo.exe) with Some File Type (Example, .001)
cd HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\
New-Item .001
cd .001
# Update OpenWith applications list
New-Item OpenWithList
Set-ItemProperty -Name g -Value demo.exe -Path OpenWithList
Set-ItemProperty -Name MRUList -Value gabcdef -Path OpenWithList

No comments :