.net – Executing F# scripts

%fnet

I'm trying to practice my F# by writing small console scripts in F# in place of my usual method of writing shell scripts/batch files. I currently run them using "fsi script.fsx [args]". Is there a way I can associate these fsx files with fsi so that I can run them directly like "script.fsx [args]"?

Best Answer

Yes it is possible.

There are two keys here:

  1. You need to bind an application to execute to the ".fsx" extension. The exact way to do this may vary a bit between operating system versions (I have only done this in the past by editing the registry directly, so I can't provide a generic solution here); make sure the command to be executed does include the script arguments (as %*), so the shell\open\command entry in the registry for the binding should look like

    "c:\Program Files\FSharp-1.9.9.9\bin\fsi.exe" "@%1" %*

    See Gradbot's response for more details.

  2. You may want to add the extension you want to trigger script execution (".fsx" in this case) to the PATHEXT environment variable, so CMD.exe can find the script even without you specifying the script's extension. A warning: if you are concerned about your system's security, reconsider this step - the fact that VBScripts are registered on a default Windows XP and older systems to provide a similar feature has been exploited in the past by virus writers (I don't know if this is still true on Vista or windows 7).
Related Topic