26 people like it.

Working with paths

Concatenating paths shouldn't be done just using string concatenation, because the directory separator may differ on various platforms. This snippet shows a simple custom operator for working with paths.

1: 
2: 
3: 
4: 
5: 
6: 
7: 
8: 
9: 
open System.IO

/// Custom operator for combining paths
let (+/) path1 path2 = Path.Combine(path1, path2)

// Root path obtained from some function
let root = (...)
// Create path for a specific file
let file = root +/ "Subdirectory" +/ "Test.fsx"
namespace System
namespace System.IO
val path1 : string
val path2 : string
type Path =
  static val DirectorySeparatorChar : char
  static val AltDirectorySeparatorChar : char
  static val VolumeSeparatorChar : char
  static val InvalidPathChars : char[]
  static val PathSeparator : char
  static member ChangeExtension : path:string * extension:string -> string
  static member Combine : [<ParamArray>] paths:string[] -> string + 3 overloads
  static member GetDirectoryName : path:string -> string
  static member GetExtension : path:string -> string
  static member GetFileName : path:string -> string
  ...

Full name: System.IO.Path
Path.Combine([<System.ParamArray>] paths: string []) : string
Path.Combine(path1: string, path2: string) : string
Path.Combine(path1: string, path2: string, path3: string) : string
Path.Combine(path1: string, path2: string, path3: string, path4: string) : string
val root : string

Full name: Script.root
"C:\\test"
val file : string

Full name: Script.file
Raw view Test code New version

More information

Link:http://fssnip.net/1g
Posted:13 years ago
Author:Tomas Petricek
Tags: path , custom operator