I have the following file located in a "New Folder" of Desktop:
// File location: "C:\Users\my_user_name\Desktop\New folder\AddOne.fs"
//
module internal AddOneModule
let AddOneFunction x = x + 1
I can access this file by using #load on the full path name using FSI F# Interactive.
Microsoft (R) F# Interactive version 4.1
Copyright (c) Microsoft Corporation. All Rights Reserved.
For help type #help;;
> #load "C:\Users\my_user_name\Desktop\New folder\AddOne.fs";;
//[Loading C:\Users\my_user_name\Desktop\New folder\AddOne.fs]
//namespace FSI_0002
// val AddOneFunction : x:int -> int
> open AddOneModule;;
> AddOneFunction 100;;
// val it : int = 101
How do I change the working directory so that I can access the file using relative path?
F# interactive:how to display/change current working directory
I tried something similar to the post above, but FSI still tries to find the file in the Temp folder:
(RESET FSI)
Microsoft (R) F# Interactive version 4.1
Copyright (c) Microsoft Corporation. All Rights Reserved.
For help type #help;;
> open System;;
> Environment.CurrentDirectory <- @"C:\Users\my_user_name\Desktop\New folder";;
//val it : unit = ()
> #load "AddOne.fs";;
// #load "AddOne.fs";;
// ^^^^^^^^^^^^^^^^^
//C:\Users\my_user_name\Desktop\New folder\stdin(3,1): error FS0078: Unable to find
//the file 'AddOne.fs' in any of
// C:\Users\my_user_name\AppData\Local\Temp
Thank you for your help.
Instead of changing the working directory you may achieve the desired using a trick with
__SOURCE_DIRECTORY__
built-in identifier.To begin with, you need a certain anchor point in directory structure. For illustration purposes let's assume you are on Windows and let this anchor point be your user directory, which is defined by
%USERPROFILE%
environment variable. Place there a scriptanchorfsi.fsx
containing the following single line of code:#I __SOURCE_DIRECTORY__
That's basically all you need to do. Now, regardless from what location you shoot your
fsi
using command linefsi --load:%USERPROFILE%\anchorfsi.fsx
, you can use relative paths in your scripts and in interactive commands.Turning to the setup in your question with loading
.\desktop\new folder\addone.fs
, the following screenshot demonstrates achieving the desired:Notice how the entered relative path
".\desktop\new folder\addone.fs"
was correctly mapped to the absolute oneC:\Users\gene\desktop\new folder\addone.fs
without any dependency upon thefsi
working directory whatsoever.