1

I can't seem to find a way to do this. Basically I want to do this in pseudocode:

MainScript.fsx:

printfn "starting an external script"

launch Script1.fsx

printfn "Finished"

Script1.fsx:

printfn "I am Script1 running"

So the output window should show (after running MainScript.fsx):

"starting an external script"

"I am Script1 running"

"Finished"

Basically the launch Script1.fsx is the method I don't know how to implement.

Thanks in advance,

Bob

Community
  • 1
  • 1
Beaker
  • 2,804
  • 5
  • 33
  • 54
  • 1
    In fsi you can load it using `#load "Script1.fsx"` will load, compile and run script1, but it outputs some extra messages like "loading script1.fsx". – khachik Apr 13 '11 at 19:07
  • What's the difference with this question? http://stackoverflow.com/questions/5643120/f-programmatically-running-fsx-script-file – Laurent Apr 13 '11 at 19:21
  • The previous question was to run the scripts outside of Visual Studio, this one is to run them within the IDE for prototyping and debugging using the interactive window. – Beaker Apr 13 '11 at 19:26
  • @khackik - That was the answer. I guess I was over-complicating things. :) If you could create an answer out of your comment I will award the answer to you. – Beaker Apr 13 '11 at 19:32
  • @Beaker: You cannot call #load from a function (I've edited my answer). – Laurent Apr 13 '11 at 19:34
  • @Laurent - That is good info to know, but not a deal-breaker for specifically what I am doing. – Beaker Apr 13 '11 at 19:43

1 Answers1

2

You can use ProcessStartInfo to start a new process. You need to give the path to fsi.exe, and use your fsx file as an argument. You need to set UseShellExecute to false to have the result in your interactive shell.

For example:

let exec fsi script =
    let psi = new System.Diagnostics.ProcessStartInfo(fsi)
    psi.Arguments <- script
    psi.UseShellExecute <- false
    let p = System.Diagnostics.Process.Start(psi)
    p.WaitForExit()
    p.ExitCode

exec @"c:\Program Files\Microsoft F#\v4.0\Fsi.exe" "foo.fsx"
Laurent
  • 2,951
  • 16
  • 19
  • Could you make this the answer to my previous related question? I'll mark it as the answer for that one. I just mentioned a function because I didn't realize there was a keyword called #load that would do what I really needed. Khachik's comment is what I needed to do in my scenario. – Beaker Apr 13 '11 at 19:42
  • I think you could use the same function for both scenarios. It's often more convenient to have the same code for both fsi in VS and for execution in fsx file. – Laurent Apr 13 '11 at 19:45
  • good point. However still free free to add your answer to my related question as well. It is better than what I posted. – Beaker Apr 13 '11 at 19:48