FilePath package:filepath

A library for FilePath manipulations, using Posix or Windows filepaths depending on the platform. Both System.FilePath.Posix and System.FilePath.Windows provide the same interface. Given the example FilePath: /directory/file.ext We can use the following functions to extract pieces. And we could have built an equivalent path with the following expressions:
  • "/directory" </> "file.ext".
  • "/directory/file" <.> "ext".
  • "/directory/file.txt" -<.> "ext".
Each function in this module is documented with several examples, which are also used as tests. Here are a few examples of using the filepath functions together: Example 1: Find the possible locations of a Haskell module Test imported from module Main:
[replaceFileName path_to_main "Test" <.> ext | ext <- ["hs","lhs"] ]
Example 2: Download a file from url and save it to disk:
do let file = makeValid url
System.Directory.createDirectoryIfMissing True (takeDirectory file)
Example 3: Compile a Haskell file, putting the .hi file under interface:
takeDirectory file </> "interface" </> (takeFileName file -<.> "hi")
References: [1] Naming Files, Paths and Namespaces (Microsoft MSDN)
File and directory names are values of type String, whose precise meaning is operating system dependent. Files can be opened, yielding a handle which can then be used to operate on the contents of that file.
Library for manipulating FilePaths in a cross platform way. This package provides functionality for manipulating FilePath values, and is shipped with GHC. It provides two variants for filepaths:
  1. legacy filepaths: type FilePath = String
  2. operating system abstracted filepaths (OsPath): internally unpinned ShortByteString (platform-dependent encoding)
It is recommended to use OsPath when possible, because it is more correct. For each variant there are three main modules: For more powerful string manipulation of OsPath, you can use the os-string package (OsPath is a type synonym for OsString). An introduction into the new API can be found in this blog post. Code examples for the new API can be found here.
Equality of two FILEPATHs. If you call System.Directory.canonicalizePath first this has a much better chance of working. Note that this doesn't follow symlinks or DOSNAM~1s. Similar to normalise, this does not expand "..", because of symlinks.
x == y ==> equalFilePath x y
normalise x == normalise y ==> equalFilePath x y
equalFilePath "foo" "foo/"
not (equalFilePath "/a/../c" "/c")
not (equalFilePath "foo" "/foo")
Posix:   not (equalFilePath "foo" "FOO")
Windows: equalFilePath "foo" "FOO"
Windows: not (equalFilePath "C:" "C:/")
Equality of two filepaths. If you call System.Directory.canonicalizePath first this has a much better chance of working. Note that this doesn't follow symlinks or DOSNAM~1s. Similar to normalise, this does not expand "..", because of symlinks.
x == y ==> equalFilePath x y
normalise x == normalise y ==> equalFilePath x y
equalFilePath "foo" "foo/"
not (equalFilePath "/a/../c" "/c")
not (equalFilePath "foo" "/foo")
Posix:   not (equalFilePath "foo" "FOO")
Windows: equalFilePath "foo" "FOO"
Windows: not (equalFilePath "C:" "C:/")
Equality of two filepaths. If you call System.Directory.canonicalizePath first this has a much better chance of working. Note that this doesn't follow symlinks or DOSNAM~1s. Similar to normalise, this does not expand "..", because of symlinks.
x == y ==> equalFilePath x y
normalise x == normalise y ==> equalFilePath x y
equalFilePath "foo" "foo/"
not (equalFilePath "/a/../c" "/c")
not (equalFilePath "foo" "/foo")
not (equalFilePath "foo" "FOO")
Equality of two FILEPATHs. If you call System.Directory.canonicalizePath first this has a much better chance of working. Note that this doesn't follow symlinks or DOSNAM~1s. Similar to normalise, this does not expand "..", because of symlinks.
x == y ==> equalFilePath x y
normalise x == normalise y ==> equalFilePath x y
equalFilePath "foo" "foo/"
not (equalFilePath "/a/../c" "/c")
not (equalFilePath "foo" "/foo")
Posix:   not (equalFilePath "foo" "FOO")
Windows: equalFilePath "foo" "FOO"
Windows: not (equalFilePath "C:" "C:/")
Equality of two filepaths. If you call System.Directory.canonicalizePath first this has a much better chance of working. Note that this doesn't follow symlinks or DOSNAM~1s. Similar to normalise, this does not expand "..", because of symlinks.
x == y ==> equalFilePath x y
normalise x == normalise y ==> equalFilePath x y
equalFilePath "foo" "foo/"
not (equalFilePath "/a/../c" "/c")
not (equalFilePath "foo" "/foo")
equalFilePath "foo" "FOO"
not (equalFilePath "C:" "C:/")