1

I am using Cleanide for Clean3.0 programming language. What I am trying to do is to implement a function that receive name of a directory in my system, and return a list of all the files in that directory.

I don't know if the defintion of such function needs to be like File -> [string] or maybe something else, even that directory is a file maybe this is not the developers of Clean meant...

Thank a lot!

Z E Nir
  • 332
  • 1
  • 2
  • 15

1 Answers1

0

This functionality is not available in the StdEnv environment, but there are two libraries that can help with this:

  • The Directory library contains a module Directory which has a function getDirectoryContents :: !Path !*env -> (!(!DirError, [DirEntry]), !*env) | FileSystem env.

  • The Platform library contains a module System.Directory which has a function readDirectory :: !FilePath !*w -> (!MaybeOSError [FilePath], !*w).

In both cases the first argument is a path to the directory and the second argument is the *World, which is the typical way of Clean to perform impure operations (see chapter 9 of the language report).

Code examples

With Directory:

import Directory

Start w
# (dir,w) = getDirectoryContents (RelativePath []) w
= dir

With Platform:

import System.Directory

Start w
# (dir,w) = readDirectory "." w
= dir
  • Thanks a lot. Please consider the fact that I'm beginner, can you post a code example? – Z E Nir Mar 07 '19 at 13:23
  • @ZENir I have updated the answer with examples for both modules. –  Mar 07 '19 at 13:39
  • Thanks. I still not sure how to get the contents of a specific directory. lets say `"C:\Users\User\myDir"` can you help me with that? – Z E Nir Mar 07 '19 at 14:12
  • 1
    @ZENir You would use that as the first argument. So e.g. `"C:\\Users\\User\\myDir"` for Platform or `AbsolutePath "C" [PathDown "Users",PathDown "User",PathDown "myDir"]` for Directory. –  Mar 07 '19 at 14:15