Skip to content

Commit f6465f4

Browse files
committed
add Haskell snippet to find files in a directory by extension
1 parent 123c169 commit f6465f4

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
title: Find Files in Directory by Type
3+
description: Finds all files in a directory with a specific extension.
4+
author: ACR1209
5+
tags: haskell,file,search,extension,filesystem
6+
---
7+
8+
```hs
9+
import System.Directory (listDirectory)
10+
import System.FilePath (takeExtension)
11+
12+
findFilesByExtension :: FilePath -> String -> IO [FilePath]
13+
findFilesByExtension dir ext = do
14+
files <- listDirectory dir
15+
return $ filter (\f -> takeExtension f == ext) files
16+
17+
main :: IO ()
18+
main = do
19+
let directory = "."
20+
let ext = ".txt"
21+
files <- findFilesByExtension directory ext
22+
mapM_ putStrLn files -- Output: list of txt files on the current directory
23+
```

0 commit comments

Comments
 (0)