# Get list of pdf files

**URL:** https://discourse.julialang.org/t/get-list-of-pdf-files/28533
**Category:** New to Julia
**Tags:** filesystem
**Created:** [September 8, 2019, 9:20am UTC](https://discourse.julialang.org/t/get-list-of-pdf-files/28533 "2019-09-08T09:20:11Z")
**Posts on this page:** 19
**Page:** 1

<div class="post-metadata">

### Author: ![hasanOryx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hasanoryx/32/5373_2.png) [@hasanOryx](https://discourse.julialang.org/u/hasanOryx)
#### Post date: [September 8, 2019, 9:20am UTC](https://discourse.julialang.org/t/get-list-of-pdf-files/28533/1 "2019-09-08T09:20:11Z")

</div>

Trying to get an array of the names of all the `pdf` files in the current directory, so I wrote the below:

```julia
files = cd(readdir, pwd())
for f in files
    if match(r"*.\.pdf", f) !== nothing # Or if occursin(r"*.\.pdf", f) == true
        println(f)
    end
end

```

But I got the below error:

```bash
PCRE compilation error: quantifier does not follow a repeatable item at offset 0

Stacktrace:
 [1] error(::String) at ./error.jl:33
 [2] compile(::String, ::UInt32) at ./pcre.jl:104
 [3] compile(::Regex) at ./regex.jl:69
 [4] Regex(::String, ::UInt32, ::UInt32) at ./regex.jl:40
 [5] Regex(::String) at ./regex.jl:65
 [6] @r_str(::LineNumberNode, ::Module, ::Any) at ./regex.jl:103

```

---

<div class="post-metadata">

### Author: ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)
#### Post date: [September 8, 2019, 9:32am UTC](https://discourse.julialang.org/t/get-list-of-pdf-files/28533/2 "2019-09-08T09:32:40Z")

</div>

That’s an invalid regexp, did you mean to write `r".*\.pdf"` instead?

---

<div class="post-metadata">

### Author: ![hasanOryx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hasanoryx/32/5373_2.png) [@hasanOryx](https://discourse.julialang.org/u/hasanOryx)
#### Post date: [September 8, 2019, 9:57am UTC](https://discourse.julialang.org/t/get-list-of-pdf-files/28533/3 "2019-09-08T09:57:30Z")

</div>

> [@pfitzseb](#):
>
> r".\*.pdf"

Thanks, but it also returns files `*.pdf.txt`

---

<div class="post-metadata">

### Author: ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)
#### Post date: [September 8, 2019, 10:01am UTC](https://discourse.julialang.org/t/get-list-of-pdf-files/28533/4 "2019-09-08T10:01:22Z")

</div>

Then use `r".*\.pdf$"` instead. Do read through a couple of regexp tutorials online though, this isn’t sepcific to Julia at all.

---

<div class="post-metadata">

### Author: ![Oliver\_Lylloff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oliver_lylloff/32/9022_2.png) [@Oliver\_Lylloff](https://discourse.julialang.org/u/Oliver_Lylloff)
#### Post date: [September 8, 2019, 10:09am UTC](https://discourse.julialang.org/t/get-list-of-pdf-files/28533/5 "2019-09-08T10:09:30Z")

</div>

You could also use a filter, see for instance this post: [addpath("C:\Users\") - #4 by StefanKarpinski](https://discourse.julialang.org/t/addpath-c-users/22519/4)

---

<div class="post-metadata">

### Author: ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)
#### Post date: [September 8, 2019, 11:12am UTC](https://discourse.julialang.org/t/get-list-of-pdf-files/28533/6 "2019-09-08T11:12:24Z")

</div>

`filter(x->endswith(x, ".pdf"), readdir())`

---

<div class="post-metadata">

### Author: ![hasanOryx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hasanoryx/32/5373_2.png) [@hasanOryx](https://discourse.julialang.org/u/hasanOryx)
#### Post date: [September 8, 2019, 11:14am UTC](https://discourse.julialang.org/t/get-list-of-pdf-files/28533/7 "2019-09-08T11:14:23Z")

</div>

What is `x`, it gaves:

```bash
0-element Array{String,1}

```

---

<div class="post-metadata">

### Author: ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)
#### Post date: [September 8, 2019, 11:16am UTC](https://discourse.julialang.org/t/get-list-of-pdf-files/28533/8 "2019-09-08T11:16:57Z")

</div>

Sorry, had the order wrong (updated it). `x->endswith(x, ".pdf")` is an anonymous function and `x` is its input argument.

---

<div class="post-metadata">

### Author: ![hasanOryx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hasanoryx/32/5373_2.png) [@hasanOryx](https://discourse.julialang.org/u/hasanOryx)
#### Post date: [September 8, 2019, 11:20am UTC](https://discourse.julialang.org/t/get-list-of-pdf-files/28533/9 "2019-09-08T11:20:32Z")

</div>

It is done, 10 out of 12 had been displayed, same using the Regex.

- Do you have an idea what could be the reason preventing 2 files from appearing
- Is `x` the `iter` of `readdir()`?

---

<div class="post-metadata">

### Author: ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)
#### Post date: [September 8, 2019, 11:23am UTC](https://discourse.julialang.org/t/get-list-of-pdf-files/28533/10 "2019-09-08T11:23:19Z")

</div>

> [@hasanOryx](#):
>
> Do you have an idea what could be the reason preventing 2 files from appearing

Hard to say without more information. What is the output of `readdir()`?

> [@hasanOryx](#):
>
> Is `x` the `iter` of `readdir()` ?

Yes. `filter` applies the function (first argument) to every element of the collection (second argument) and returns a vector of all elements for which the function evaluated to true.

---

<div class="post-metadata">

### Author: ![hasanOryx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hasanoryx/32/5373_2.png) [@hasanOryx](https://discourse.julialang.org/u/hasanOryx)
#### Post date: [September 8, 2019, 11:25am UTC](https://discourse.julialang.org/t/get-list-of-pdf-files/28533/11 "2019-09-08T11:25:24Z")

</div>

> [@carstenbauer](#):
>
> What is the output of `readdir()` ?

I’ve 12 files, but it reads 10

I’ve all the files [here](https://github.com/hajsf/cv_scanner) if you can help me understanding the issue. thanks

 ![image](https://global.discourse-cdn.com/julialang/original/3X/d/e/de1a774958949f45d0621ae2ee8706c94549a0b9.png)

---

<div class="post-metadata">

### Author: ![oschulz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oschulz/32/2998_2.png) [@oschulz](https://discourse.julialang.org/u/oschulz)
#### Post date: [September 8, 2019, 11:50am UTC](https://discourse.julialang.org/t/get-list-of-pdf-files/28533/12 "2019-09-08T11:50:31Z")

</div>

Try using [Glob.jl](https://github.com/vtjnash/Glob.jl):

```julia
using Glob
glob("*.pdf")

```

---

<div class="post-metadata">

### Author: ![hasanOryx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hasanoryx/32/5373_2.png) [@hasanOryx](https://discourse.julialang.org/u/hasanOryx)
#### Post date: [September 8, 2019, 12:00pm UTC](https://discourse.julialang.org/t/get-list-of-pdf-files/28533/13 "2019-09-08T12:00:57Z")

</div>

> [@oschulz](#):
>
> glob(“\*.pdf”

Thanks, it gave the same 10 files, not recognizing the other 2!!

---

<div class="post-metadata">

### Author: ![hasanOryx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hasanoryx/32/5373_2.png) [@hasanOryx](https://discourse.julialang.org/u/hasanOryx)
#### Post date: [September 8, 2019, 12:23pm UTC](https://discourse.julialang.org/t/get-list-of-pdf-files/28533/14 "2019-09-08T12:23:21Z")

</div>

@carstenbauer and @oschulz

I just noticed that these 2 files that are not caught are saved as `.PDF` in caps, not as `.pdf` in smalls.

So I was able to get them using `regex` with `(?i) (?-i)` as below, if you have simplified code will be appreciated.

```julia
read_files = cd(readdir, pwd())
for rf in read_files
    if occursin(r"(?i).*\.pdf\z(?-i)", rf) == true
        println(rf)
        end
end

```

---

<div class="post-metadata">

### Author: ![oschulz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oschulz/32/2998_2.png) [@oschulz](https://discourse.julialang.org/u/oschulz)
#### Post date: [September 8, 2019, 12:34pm UTC](https://discourse.julialang.org/t/get-list-of-pdf-files/28533/15 "2019-09-08T12:34:26Z")

</div>

Hm, Glob.jl does support caseless operation for `Glob.FilenameMatch`, but I’m not sure how to use that with it’s `glob()` function. @jameson, can I pull you into this thread for some help?

---

<div class="post-metadata">

### Author: ![cchderrick](https://avatars.discourse-cdn.com/v4/letter/c/ecd19e/32.png) [@cchderrick](https://discourse.julialang.org/u/cchderrick)
#### Post date: [September 8, 2019, 12:38pm UTC](https://discourse.julialang.org/t/get-list-of-pdf-files/28533/16 "2019-09-08T12:38:48Z")

</div>

you can put `i` after the regex to enable case-insensitive matching, something like that:

```julia
r".*\.pdf$"i

```

Doc: [@r\_str](https://docs.julialang.org/en/v1/base/strings/#Base.@r_str)

or you can just lower case the input:

```julia
x->endswith(lowercase(x), ".pdf")

```

---

<div class="post-metadata">

### Author: ![jameson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jameson/32/23_2.png) [@jameson](https://discourse.julialang.org/u/jameson)
#### Post date: [September 10, 2019, 1:08am UTC](https://discourse.julialang.org/t/get-list-of-pdf-files/28533/17 "2019-09-10T01:08:52Z")

</div>

Glob.jl also supports regexes, as shown in the README as usage 3, so this, for example, should match the rather unlikely path “./name.pdf/name.pDf/name.PdF”:

```julia
glob( ["name.pdf", r".*\.pdf"i, fn"*.pdf"i] )

```

---

<div class="post-metadata">

### Author: ![hasanOryx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hasanoryx/32/5373_2.png) [@hasanOryx](https://discourse.julialang.org/u/hasanOryx)
#### Post date: [September 10, 2019, 12:21pm UTC](https://discourse.julialang.org/t/get-list-of-pdf-files/28533/18 "2019-09-10T12:21:02Z")

</div>

> [@jameson](#):
>
> fn

Thanks, what is `fn`?

---

<div class="post-metadata">

### Author: ![jameson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jameson/32/23_2.png) [@jameson](https://discourse.julialang.org/u/jameson)
#### Post date: [September 10, 2019, 4:22pm UTC](https://discourse.julialang.org/t/get-list-of-pdf-files/28533/20 "2019-09-10T16:22:08Z")

</div>

filename—it’s the matcher engine in the Glob.jl package
