1

I'm in the process of upgrading a batch script that I have to the newer Windows(10) features. My script calls 7-zip. I'm currently researching the different ways I can use to see if 7-zip is installed. I may eventually use Wix and the registry but I'm having trouble knowing the mechanics of the current system that Windows uses to determine which application a file opens within my batch script. I have done research on the internet but hit a wall. I have discovered the Assoc and Ftype commands, but there seems to be more that I am missing.

I have a directory on my thumb drive...

 Directory of D:\stuff b\Mikey's Applications\Personal\WhichCMD

17/07/2019  12:30 PM    <DIR>          .
17/07/2019  12:30 PM    <DIR>          ..
17/07/2019  12:22 PM             1,601 64bit.txt
17/07/2019  12:20 PM             1,641 32biton64.txt
18/07/2019  11:32 AM             2,047 32bit.txt
18/07/2019  04:53 PM               821 32biton64.7z
               4 File(s)          6,110 bytes
               2 Dir(s)  10,150,608,896 bytes free
`

When I type 32biton64.txt it brings up Notepad. Wonderful!

When I type 32biton64.7z it brings up the 7-zip File Manager. Wonderful!

When I type assoc .txt I get...

D:\stuff b\Mikey's Applications\Personal\WhichCMD>assoc .txt
.txt=txtfile

which is wonderful!

When I type assoc .7z I get an error ...

D:\stuff b\Mikey's Applications\Personal\WhichCMD>assoc .7z
File association not found for extension .7z

So there is another system that Windows uses to find an application other than " Assoc and Ftype". What is it?

Mikey
  • 89
  • 7
  • This is interesting! Well, I believe `ftype` and `assoc` are quite old commands that have never really been updated through all the Windows versions, but file association in Windows has been extended by some kind of indirect references as far as I got, which I guess those commands do not understand... – aschipfl Jul 19 '19 at 12:30
  • Yes, there are more systems used nowadays, especially by Windows Explorer. `HKEY_CLASSES_ROOT` (short `HKCR`) is not the only method anymore which contains the resulting file association for the process in its current environment (account, 32 or 64 bit environment) as stored in `HKCU\Software\Classes`, `HKCU\Software\Classes\Wow6432Node`, `HKLM\Software\Classes` and `HKLM\Software\Classes\Wow6432Node`. See also [Registry Keys Affected by WOW64](https://learn.microsoft.com/en-us/windows/desktop/WinProg64/shared-registry-keys). – Mofi Jul 20 '19 at 12:27
  • `HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts` is a registry key managed by Windows Explorer. It can be deleted completely safely at any time and Windows Explorer recreates on next access. It contains the information which application has the user explicitly chosen for opening a file with a specific file extension. Additionally Windows Explorer records here all applications ever used by any application to open a file with a specific file extension. These data are used together with other data to suggest the user the applications after using context menu __Open with__. – Mofi Jul 20 '19 at 12:38
  • But that's still not all. Microsoft documented recommended [Application Registration](https://learn.microsoft.com/en-us/windows/desktop/shell/app-registration), see also [Where is “START” searching for executables?](https://stackoverflow.com/a/27386403/3074564) The Microsoft documentation about application registration is most likely the most important documentation for your task. But take also a look on the other items in menu on left side of this page. They contain the information you want to know. – Mofi Jul 20 '19 at 12:44
  • Next there are the registry keys `HKLM\Software\RegisteredApplications` and and most often as recommended `HKLM\Software\AppName\Capabilities` and `HKLM\Software\AppName\Capabilities\FileAssociations`. An application like *WinRAR* (sorry, 7-Zip not used by me) stores under `HKLM\Software\WinRAR\Capabilities\FileAssociations` which file extensions can be associated with this application referenced by `WinRAR` value in `HKLM\Software\RegisteredApplications`. Windows uses this information on presenting the various file association dialog windows existing in the meantime. – Mofi Jul 20 '19 at 12:52
  • See Microsoft documentation for [Default Programs](https://learn.microsoft.com/en-us/windows/win32/shell/default-programs) and again look also on items in menu to the left side of this MS documentation page. – Mofi Jul 20 '19 at 12:55
  • However, do not expect 7-Zip is currently installed by looking if file extension `.7z` is registered in Windows registry. One of the main problems on uninstalling a application is that most often neither the uninstaller knows nothing about file associations done by the installed application without or with user interaction or by Windows after user interaction. For that reason most often a file association exists in Windows registry with an application not anymore installed at all. Also file extension `.7z` can be also associated with a different application than *7-Zip*, for example *WinRAR*. – Mofi Jul 20 '19 at 12:58
  • I am sure, without having installed *7-Zip* ever on my computers, that *7-Zip* installed by the user using the MSI (Microsoft Installer) package or the EXE installer results in an appropriate application registration in `HKLM\Software\Microsoft\Windows\CurrentVersion\App Paths` or `HKCU\Software\Microsoft\Windows\CurrentVersion\App Paths` and for that reason the appropriate *7-Zip* key should be queried under these two registry keys. A simple extraction to any directory using the .7z or .exe or .msi file by the user is of course problematic for detecting if *7-Zip* is __installed__ on a PC. – Mofi Jul 20 '19 at 13:07
  • @Mofi I've moved a bit further now and am testing for 7-Zip installation in my Wix installer. I found the path of 7z.exe in `HKLM\Software\7-Zip>Path` and set an environment variable using it. It works. Thanks for your information, it will be helpful for me. – Mikey Jul 21 '19 at 00:28

0 Answers0