Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to list all files in current directory

What I want is to write a Haskell function to return the files of current directory e.g

Change the current directory to

 :cd c:/code/haskell

Then write a function which returns the files in a set e.g

 [x | x <-getDirectoryContents ]

Edited:

I have wrote a function sth like this which lists files (ref: http://zvon.org/other/haskell/Outputdirectory/index.html)

import Directory 

main = _dir "/tmp/FOO"

_dir _path =do
    setCurrentDirectory _path
    _cd <- getCurrentDirectory
    print _cd
    _file <- getDirectoryContents _cd
    print _file

so calling _dir "c:/code/haskell" will list all files + directory names (non-recursive) . What I want now is to call this in a predicate function, for example:

[ x| x <- _dir  "c:/code/haskell" | x start with 'haskell_' ]  

so I can apply a filter on file name

like image 616
sakhunzai Avatar asked Jul 11 '11 11:07

sakhunzai


People also ask

How do I list all files in a current directory in Windows?

You can use the DIR command by itself (just type “dir” at the Command Prompt) to list the files and folders in the current directory.

Which is the correct command to list all the files from current directory?

The ls command is used to list files or directories in Linux and other Unix-based operating systems. Just like you navigate in your File explorer or Finder with a GUI, the ls command allows you to list all files or directories in the current directory by default, and further interact with them via the command line.

How do you list all files in a directory Windows CMD?

Type dir /A:D. /B > FolderList. txt and press Enter to generate a top-level folder list. When the list is complete, a new, blank prompt with a flashing cursor will appear.


2 Answers

It seems you are looking for:

getDirectoryContents :: FilePath -> IO [FilePath]

Refer : http://www.haskell.org/ghc/docs/6.12.2/html/libraries/directory-1.0.1.1/System-Directory.html#1

like image 117
Ankur Avatar answered Sep 19 '22 21:09

Ankur


How about the following:

import Data.List
import System.Directory

main = do all <- getDirectoryContents "/tmp/FOO"
          let filtered = filter (isPrefixOf "haskell") all
          print filtered
like image 26
stusmith Avatar answered Sep 18 '22 21:09

stusmith