Portable file association for USB sticks?

I use a lot of portable apps, and I use two programs especially made for this. They both do the same thing, but in different ways.

eXpresso (made using AutoHotkey) intercepts mouse clicks and changes the action to open whichever program that you associate the file type with.

Portable File Associator (made using AutoIt) makes file association entries in the HKCU registry hive, which does not need admin rights, and it can be set to remove them when the program is exited. I prefer this one, as it can also be made to make the entries and then exit without removing the associations, which is useful on my own computers (the instructions for more advanced use are in a file somewhere in the program folder). The way it works also allows you to define icons to the file types. When you download this program, the password for the archive file is in the file name.

Programs made using AutoIt and AutoHotkey can sometimes get flagged as viruses by anti-virus programs, as viruses have been made using them, but these programs are totally safe.


Here is what I use, which might be overcomplicated:

  • Have a batch file on the USB drive that maps its folder to the Q: or another drive letter (subst Q: "%~dp0" basically)
  • Use a portable explorer with custom file associations. I use FreeCommander; under “Extras, Preferences, Programs” you can set custom programs to open when hitting F4 on your files, depending on their extension. I have mapped 7z,zip,tar,etc. to 7zip-portable for example. So all you have to do is set the editor to Q:\7z-portable\7zportable.exe and that's all.

You can use the standard assoc command in a batch script (.bat) to save, set and reset file associations.

The assoc command can display and association, for example, in a Command Prompt (cmd) :

image1

The command can also set a file-association :

image2

You can create the following two .bat files on the root folder on the USB disk (or elsewhere). These batch files require the presence in the same folder of a file named prefix that you create only once using notepad and typing the string "assoc ", without the quotes and with the ending blank and (very important) without pressing Enter (so it doesn't have an end-of-line).

The first .bat file we will call myapps.bat, and whose purpose will be to assign the new file association, after creating another restore.bat file that you will use before ejecting the USB disk in order to restore the old file associations.

An example myapps.bat to set the file associations of html, abab and htm to myprog1.exe etc. :

@echo off
echo @echo off >restore.bat
cmd /q /c doassoc html "%cd%\myprog1.exe"
cmd /q /c doassoc abab "%cd%\myprog2.exe"
cmd /q /c doassoc htm "%cd%\myprog3.exe"
del ftemp1
del ftemp

The variable %cd% stands for the current directory, but you can also use %cd:~0,2% which will give you the current disk (for example G:) together with any other relative file specification on the disk.

The doassoc.bat file which does the work for one file association will contain :

echo assoc .%1= >ftemp1
assoc .%1 >nul
IF %ERRORLEVEL% NEQ 0 goto notfound
assoc .%1 >ftemp
copy /b /y prefix+ftemp ftemp1 >nul
:notfound
copy /b /y restore.bat+ftemp1 restore.bat >nul
assoc .%1=%2

An example of the result when running myapps.bat followed by restore.bat is :

image3