# Including a folder

**URL:** https://discourse.julialang.org/t/including-a-folder/56308
**Category:** General Usage
**Tags:** question, macros
**Created:** [March 2, 2021, 7:45am UTC](https://discourse.julialang.org/t/including-a-folder/56308 "2021-03-02T07:45:49Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Amval](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amval/32/6217_2.png) [@Amval](https://discourse.julialang.org/u/Amval)
#### Post date: [March 2, 2021, 7:45am UTC](https://discourse.julialang.org/t/including-a-folder/56308/1 "2021-03-02T07:45:49Z")

</div>

Hello,

I want to be able to do something like this:

```julia
module Example
include_folder("my/modules")

end

```

And `include_folder` should look similar to this:

```julia
module Utils
function include_folder(path::AbstractString)
    paths = joinpath.(Ref(path), readdir(path))
    julia_files = filter(f -> endswith(f, ".jl"), paths)
    include.(julia_files)
end
end

```

The problem is that is not going to include the files in the module of the caller, but wherever `include_folder` is defined. I know that macros have an extra ` __module__ ` argument, representing the caller’s module, so I have tried to define a macro.

This is what I got so far:

```julia
macro include_folder(path)
    quote
        paths = joinpath.(Ref($path), readdir($path))
        local julia_files = filter(f -> endswith(f, ".jl"), paths)
        :(include.($julia_files))
    end
end

```

Which is wrong and returns:

> include.([“my/modules/mod1.jl”, “…”])

I don’t think I understand how macros work, and my attempts at running that code inside the macro have failed (wrapping the quote in an extra eval, ommiting the last quote, etc).

So, a few questions:

- is it possible to do this in a simpler way? (Avoiding macros alltogether)
- is this a terrible idea for any reason?
- how can I make this work?

Right now this work, but seems very wrong:

```julia
@eval @include_folder("my/modules")

```

Any help is appreciated, thanks in advance.

---

<div class="post-metadata">

### Author: ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)
#### Post date: [March 2, 2021, 8:33am UTC](https://discourse.julialang.org/t/including-a-folder/56308/2 "2021-03-02T08:33:32Z")

</div>

According to the documentation for `include`, “Every module (except those defined with baremodule) has its own definition of include, which evaluates the file in that module.”

But you can “Use Base.include to evaluate a file into another module.”

So maybe you can pass the required module to your `include_folder` function, which then `Base.includes` the files in that module:

```julia
module Utils
    export include_folder
    function include_folder(m::Module, path::AbstractString)
        paths = joinpath.(Ref(path), readdir(path))
        julia_files = filter(f -> endswith(f, ".jl"), paths)
        Base.include.(Ref(m), julia_files)
    end
end

```

And then pass the current module to `include_folder` with `@ __MODULE__ `

```julia
module Example
    using Utils    
    include_folder(@ __MODULE__ , "my/modules")
end

```

---

<div class="post-metadata">

### Author: ![Amval](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amval/32/6217_2.png) [@Amval](https://discourse.julialang.org/u/Amval)
#### Post date: [March 2, 2021, 8:51am UTC](https://discourse.julialang.org/t/including-a-folder/56308/3 "2021-03-02T08:51:51Z")

</div>

Thanks, that works. I must have skipped that part in the documentation. This would work:

```julia
macro include_folder(path)
    quote
        paths = joinpath.(Ref($path), readdir($path))
        local julia_files = filter(f -> endswith(f, ".jl"), paths)
        Base.include.(Ref($ __module__ ), $julia_files)
    end
end

```

However I would be curious if someone could tell me how to solve my firs attempt at a macro. Not for this particular problem, but for better understanding of how macros work.
