Search code examples
windowsbatch-filefile-association

Windows: establish file association to batch file


I created a custom file extension I would associate to a batch script. I used

ASSOC .myext=MY.FILETYPE
FTYPE MY.FILETYPE=cmd /c "C:\Path\of\my\batch.bat" %1 %*

by now the batch file "C:\Path\of\my\batch.bat" is a simple one-liner

echo %1

And roughly works: double clicking a .myext file pops up a cmd shell echoing the file path.
But a problem arises when the .myext file is in a path containing spaces: the echoed filepath is truncated to the space.
Double quoting the %1 in the FTYPE statement seems not to work.

FTYPE MY.FILETYPE=cmd /c "C:\Path\of\my\batch.bat" "%1" %*

Solution

  • Double quoting %1 is correct, but it fails as cmd.exe contains a bug when the command and at least one parameter contains quotes.
    So you need to make the command without quotes by inserting CALL.

    FTYPE MY.FILETYPE=cmd /c call "C:\Path\of\my\batch.bat" "%1" %*