Monday, 30 May 2016

Haskell: findExecutable : Get the path of given executable

System.Directory module provides findExecutable function which return the path of given executable file. Following is the signature of the findExecutable function.

Prelude System.Directory> :t findExecutable
findExecutable :: String -> IO (Maybe FilePath)

Observe the signature, it takes a executable file name as input and return Just Path, if the executable exists, else return Nothing.


Prelude System.Directory> findExecutable "java"
Just "/usr/bin/java"
Prelude System.Directory> findExecutable "cabal"
Just "/usr/local/bin/cabal"
Prelude System.Directory> 
Prelude System.Directory> findExecutable "krishna"
Nothing

Note
findExecutable searcher for the file in the directories listed in system PATH. On Windows, findExecutable calls the Win32 function SearchPath.





Previous                                                 Next                                                 Home

Haskell: Search for files

Haskell System.Directory module provides number of functions to search for a file, executables on your machine. Following tables summarizes the functions provided by System.Directory module for file searching.

Function
Signature
Description
findExecutable :: String -> IO (Maybe FilePath)
Get the path of given executable
findExecutables :: String -> IO [FilePath]
Get all the paths of executable file
findExecutablesInDirectories:: [FilePath] -> String -> IO [FilePath]
Search for an executable in given directories
findFile :: [FilePath] -> String -> IO (Maybe FilePath)
Search for a file in given directories
findFiles :: [FilePath] -> String -> IO [FilePath]
Search for a file in given directories
findFilesWith:: (FilePath -> IO Bool) -> [FilePath] -> String -> IO [FilePath]
Get all the files that satisfiy given property






Previous                                                 Next                                                 Home

Haskell: makeRelativeToCurrentDirectory: Construct a path relative to current directory


System.Directory module provides makeRelativeToCurrentDirectory function, which construct a path relative to current directory. Following is the signature of makeRelativeToCurrentDirectory function.

Prelude System.Directory> :t makeRelativeToCurrentDirectory 
makeRelativeToCurrentDirectory :: FilePath -> IO FilePath
Prelude System.Directory> getCurrentDirectory 
"/Users/harikrishna_gurram"
Prelude System.Directory> makeRelativeToCurrentDirectory "/Users/harikrishna_gurram"
"."
Prelude System.Directory> makeRelativeToCurrentDirectory "/Users/harikrishna_gurram/Shared"
"Shared"
Prelude System.Directory> makeRelativeToCurrentDirectory "/Users/harikrishna_gurram/Shared/Movies"
"Shared/Movies"
Prelude System.Directory> makeRelativeToCurrentDirectory "/Users/harikrishna_gurram/Shared/Movies/1"
"Shared/Movies/1"


Previous                                                 Next                                                 Home

Haskell: makeAbsolute: Convert path to absolute path

System.Directory module provides makeAbsolute function, which convert path to absolute path. If the path is relative to current directory then t he current directory is prepended and then the combined result is normalised. If the path is already absolute, the path is simply normalised.

This operation may fail with following errors.
Error
Description
HardwareFault
A physical I/O error has occurred.
isDoesNotExistError or NoSuchThing
There is no path referring to the working directory.
isPermissionError or PermissionDenied
The process has insufficient privileges to perform the operation.
ResourceExhausted
Insufficient resources are available to perform the operation.

UnsupportedOperation
The operating system has no notion of current working directory.

Following is the signature of normalize function. ‘normalise’ function is available at System.FilePath module.

Prelude System.FilePath> :t normalise
normalise :: FilePath -> FilePath


Following are the rules for normalize function , which normalizes a file path.
Posix:   normalise "/file/\\test////" == "/file/\\test/"
Posix:   normalise "/file/./test" == "/file/test"
Posix:   normalise "/test/file/../bob/fred/" == "/test/file/../bob/fred/"
Posix:   normalise "../bob/fred/" == "../bob/fred/"
Posix:   normalise "./bob/fred/" == "bob/fred/"
Windows: normalise "c:\\file/bob\\" == "C:\\file\\bob\\"
Windows: normalise "c:\\" == "C:\\"
Windows: normalise "C:.\\" == "C:"
Windows: normalise "\\\\server\\test" == "\\\\server\\test"
Windows: normalise "//server/test" == "\\\\server\\test"
Windows: normalise "c:/file" == "C:\\file"
Windows: normalise "/file" == "\\file"
Windows: normalise "\\" == "\\"
Windows: normalise "/./" == "\\"
         normalise "." == "."
Posix:   normalise "./" == "./"
Posix:   normalise "./." == "./"
Posix:   normalise "/./" == "/"
Posix:   normalise "/" == "/"
Posix:   normalise "bob/fred/." == "bob/fred/"
Posix:   normalise "//home" == "/home"

Prelude System.Directory> makeAbsolute "."
"/Users/harikrishna_gurram"
Prelude System.Directory> 
Prelude System.Directory> makeAbsolute ".."
"/Users/harikrishna_gurram/.."
Prelude System.Directory> 
Prelude System.Directory> makeAbsolute ".//Shared"
"/Users/harikrishna_gurram/Shared"
Prelude System.Directory> 
Prelude System.Directory> makeAbsolute ".\\Shared"
"/Users/harikrishna_gurram/.\\Shared"
Prelude System.Directory> 
Prelude System.Directory> makeAbsolute ".////Shared"
"/Users/harikrishna_gurram/Shared"
Prelude System.Directory> 
Prelude System.Directory> makeAbsolute ".////////Shared"
"/Users/harikrishna_gurram/Shared"
Prelude System.Directory> 
Prelude System.Directory> makeAbsolute ".///Shared"
"/Users/harikrishna_gurram/Shared"
Prelude System.Directory> 
Prelude System.Directory> makeAbsolute "/.///Shared"
"/Shared"
Prelude System.Directory> makeAbsolute "//.///Shared"
"/Shared"
Prelude System.Directory> makeAbsolute "///.///Shared"
"/Shared"
Prelude System.Directory>


Previous                                                 Next                                                 Home