Thursday, 2 June 2016

Haskell: exeExtension: Get the extension of executable file


System.Directory module provides exeExtension function, which gives the extension of executable file. On Windows this function return .exe, on POSIX it returns " ".
Prelude System.Directory> :t exeExtension
exeExtension :: String
Prelude System.Directory> 
Prelude System.Directory> exeExtension
""



Previous                                                 Next                                                 Home

Haskell: findFilesWith: Get all the files that satisfiy given property

System.Directory module provides findFilesWith function, Search through the given set of directories for the given file and with the given property (usually permissions) and returns a list of paths where the given file exists and has the property. Following is the signature of findFilesWith function.

findFilesWith :: (FilePath -> IO Bool) -> [FilePath] -> String -> IO [FilePath]

Following program takes list of directories and a file name to search, return all the paths where this file is executable.

DirectoryUtil.hs
import System.IO
import System.Directory
import Data.List.Split

getExecutablePaths listOfDirs file =
    findFilesWith isExecutable listOfDirs file
        where isExecutable file = do
         perms <- getPermissions file
         return $ executable perms

main = do
 putStrLn "Enter the directories to search (, separated)"
 directories <- getLine

 putStrLn "Enter file to search"
 file <- getLine

 let listOfDirs = (splitOn "," directories)

 executables <- getExecutablePaths listOfDirs file

 putStrLn (show executables)

$ runghc DirectoryUtil.hs
Enter the directories to search (, separated)
/usr/bin,/var,/Users,/bin,/Users/harikrishna_gurram
Enter file to search
java
["/usr/bin/java","/bin/java"]



Previous                                                 Next                                                 Home

Haskell: findFiles: Search for a file in given directories

System.Directory module provides findFiles function, which takes list of directories to search, filename as arguments and return list of paths. Following is the signature of findFiles function.

Prelude System.Directory> :t findFiles

findFiles :: [FilePath] -> String -> IO [FilePath]
Prelude System.Directory> findFiles ["/Users/harikrishna_gurram", "/var", "/Users/harikrishna_gurram/logs"] "temp.log"
["/Users/harikrishna_gurram/temp.log","/Users/harikrishna_gurram/logs/temp.log"]
Prelude System.Directory> 
Prelude System.Directory> findFiles ["/Users/harikrishna_gurram", "/var", "/Users/harikrishna_gurram/logs"] "temp1.log"
[]



Previous                                                 Next                                                 Home

Haskell: findFile: Search for a file in given directories

System.Directory module provides findFile function which takes list of directories and a file as an argument and return ‘Just path’ if the file is available, else Nothing. Following is the signature of findFile function.

Prelude System.Directory> :t findFile

findFile :: [FilePath] -> String -> IO (Maybe FilePath)
Prelude System.Directory> findFile ["/", "/Users/harikrishna_gurram", "/var"] "academyLogin.log"
Just "/Users/harikrishna_gurram/academyLogin.log"
Prelude System.Directory> 
Prelude System.Directory> findFile ["/", "/Users/harikrishna_gurram", "/var"] "academyLogin.log.123"
Nothing


Previous                                                 Next                                                 Home

Haskell: findExecutablesInDirectories: Search for an executable in given directories

System.Directory module provides findExecutablesInDirectories function, which takes a list of directories, file to search and returns a list of all occurrences that are executable.

Following is the signature of findExecutablesInDirectories function.
findExecutablesInDirectories :: [FilePath] -> String -> IO [FilePath]

Prelude System.Directory> findExecutablesInDirectories ["/usr/bin", "/Users/harikrishna_gurram", "/var"] "javac"
["/usr/bin/javac"]


Previous                                                 Next                                                 Home

Haskell: findExecutables: Get all the paths of executable file


System.Directory module provides findExecutables function which takes file name as argument and return a list of all occurrences that are executable. It returns an empty list, if it don’t find any executable. Following is the signature of findExecutables function.
Prelude System.Directory> :t findExecutables 
findExecutables :: String -> IO [FilePath]
Prelude System.Directory> findExecutables "java"
["/usr/bin/java","/bin/java"]
Prelude System.Directory> findExecutables "javac"
["/usr/bin/javac"]
Prelude System.Directory> findExecutables "cabal"
["/usr/local/bin/cabal"]
Prelude System.Directory> findExecutables "Krishna"
[]

Note
On Windows this function returns only the first occurrence of the executable.





Previous                                                 Next                                                 Home