How to get F# scripts files and other script languages to work like .exe, .cmd, and .bat files in Windows
Asked Answered
E

1

2

It is possible to configure F# script files so that they may be invoked directly without directly specifying the script runner application and their file extension, and made accessible through the command PATH environment variable.

The steps to do so are as follows:

  1. Set the particular script engine as the default "open with" program for the script file type extension using Windows Explorer
  2. Appended the script extension to the PathExt environment variable, which will classify it as executable
  3. Optionally, include the directory path containing the scripts to Windows Path environment variable

My question: how to get arguments through to the script when you are not directly invoking it's runner application with it.

Eldaelden answered 18/11, 2014 at 21:2 Comment(6)
Open an elevated command prompt, and create a file type: ftype fsxfile="path\to\fsi.exe" "%1" %*. Associate this type with the file extension: assoc .fsx=fsxfile.Daff
@eryksun Thanks for the information on assoc, ftype. However, after applying them, the problem remained. It seems the settings applied from the command line as you suggested do not actually replace those set in windows explorer. However, the changes are reported by both assoc and ftype query options. Still, the behaviour remains whatever is set by explorer previously. If you have any additional insights that would be great.Eldaelden
ftype and assoc modify the local machine settings, under the registry key HKLM\SOFTWARE\Classes. The settings of the current user take precedence; they're under HKCU\Software\Classes. Also, Explorer has its own file associations under HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts, but you should be able to manage those through the GUI, to select the filetype you created using cmd's ftype command.Daff
@eryksun Thank again. It seems that my registery is not behaving as expected. An "sfc /scannow" reports inability to fix certain problems. The performance of my system has been rather poor lately so I am in the process of girding up for a system re-init, software re-installation, and the concomitant wtf of the overlooked stuff. If you having any suggestions I welcome your wisdom.Eldaelden
If you have a specific question regarding the system file checker (sfc) and/or a corrupt registry, I recommend you ask on superuser.Daff
@eryksun :) I performed the following with success in getting sfc to report no integrity violations: (1) chkdsk via explorer disk/properties/tools - while reporting it wouldn't, it did perform autofix of found errors) (2) "sfc /scannow" - reported errors (3) "dism /online /cleanup-image /restorehealth" - repaired "store" corruption (4) "sfc /scannow" - reported no integrity violations found.Eldaelden
E
3

The following provides a simple technique for configuring your windows system to include F# as a scripting language on par with cmd and batch files. Consequently, the scripts are accessible from the environment path, invocable without a file type extension, and may be passed any number of arguments. This technique should be applicable to other scripting languages such as powershell, python, etc.

The following is a batch file for establishing this configuration for F# 3.1 in .Net 4.0:

configure.bat
  rem absolute filename of the F# script runner, Fsi.exe
  set fsharpScriptRunner="C:\Program Files (x86)\Microsoft SDKs\F#\3.1\Framework\v4.0\Fsi.exe"

  rem script runner command for fsi.exe to execute script and exit without advertising itself
  set fsharpScriptRunnerCmd=%fsharpScriptRunner% --nologo --exec 

  rem associate "FSharpScript" files with fsharpScriptRunnerCmd, appending
  rem the file name and remaining line arguments into the commandline
  rem after the delimiter "--"
  ftype FSharpScript=%fsharpScriptRunnerCmd% %%1 "--" %%*

  rem associate file extension ".fsx" with the file type "FSharpScript"
  assoc .fsx=FSharpScript

  rem add ".fsx" to the list of file types treated as executable
  set pathext=%pathext%;.fsx

Note, the presence of "--" in the "ftype" command line serves as a delimiter to assist the script in distinguish it's command line arguments from those of the script runner; an F# script receives all the arguments and must parse out it's arguments. My clumsy F# version of this parsing using the provided delimiter is as follows:

open System

let args = 
   Environment.GetCommandLineArgs()
   |> Seq.skipWhile (fun p -> p <> "--") 
   |> Seq.skip 1
   |> List.ofSeq

Enjoy.

Eldaelden answered 18/11, 2014 at 21:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.