# Point to directory with .txt files, then loop through those files performing actions

**URL:** <https://discourse.julialang.org/t/point-to-directory-with-txt-files-then-loop-through-those-files-performing-actions/38696>\
**Category:** New to Julia\
**Tags:** question, filesystem\
**Created:** [May 3, 2020, 7:33pm UTC](https://discourse.julialang.org/t/point-to-directory-with-txt-files-then-loop-through-those-files-performing-actions/38696 "2020-05-03T19:33:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nash](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nash/32/14482_2.png) [@Nash](https://discourse.julialang.org/u/Nash)\
**Post date:** [May 3, 2020, 7:33pm UTC](https://discourse.julialang.org/t/point-to-directory-with-txt-files-then-loop-through-those-files-performing-actions/38696/1 "2020-05-03T19:33:32Z")

</div>

I have a bundle of .txt files to process. The files are placed at a specific directory. I have written the code for processing any given file, but I need to automate the selection of each file one by one, until all the files in the directory have been processed.

What code will:

1. Point to the directory holding the bundle of .txt files.
2. Identify all the files in that directory (I will manually guarantee that only relevant files are there).
3. Loop through the identified files (I suppose a list of the files is needed, and a count of the number of files in the list for setting the loop).

For reference, let’s say the .txt files are placed at “C:/Users/me/data/

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [May 3, 2020, 7:44pm UTC](https://discourse.julialang.org/t/point-to-directory-with-txt-files-then-loop-through-those-files-performing-actions/38696/2 "2020-05-03T19:44:12Z")

</div>

The function `readdir()` produces an array of all file names in a given folder. You can then filter those files further by using `filter`, i.e.

```julia
files_to_keep = ["f1.txt", "f2.txt"]
files_to_act_on = filter(t -> t in files_to_keep, readdir())

```

Then you have a function `analyze` which takes in a string. Just do

```julia
for file in files_to_act_on
    analyze(file)
end

```

---

<div class="post-metadata">

**Author:** ![Nash](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nash/32/14482_2.png) [@Nash](https://discourse.julialang.org/u/Nash)\
**Post date:** [May 3, 2020, 8:30pm UTC](https://discourse.julialang.org/t/point-to-directory-with-txt-files-then-loop-through-those-files-performing-actions/38696/3 "2020-05-03T20:30:30Z")

</div>

So, it seems that I can use readdir() to do the following:

```
filelist = readdir("C:/Users/me/data/")

```

Then, I can, forexample, point to a specific file. For example:

```
filelist[10]

```

But, how do I dynamically use that to open the file. The following, for example does not work:

```
 open("C:/Users/me/data/filelist[10]")

```

The reason is that filelist[10] is not recognized as a variable containing the name of the 10th file.

How do I solve that problem?

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [May 3, 2020, 8:35pm UTC](https://discourse.julialang.org/t/point-to-directory-with-txt-files-then-loop-through-those-files-performing-actions/38696/4 "2020-05-03T20:35:02Z")

</div>

You can use `joinpath` for that scenario. Check out the docs for `joinpath` by writing `?joinpath` in the terminal or jupyter notebook you are working in.

It sounds like you should spend some time reading the [docs](https://docs.julialang.org/en/v1/manual/strings/) on how string work in julia.

---

<div class="post-metadata">

**Author:** ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)\
**Post date:** [May 3, 2020, 9:42pm UTC](https://discourse.julialang.org/t/point-to-directory-with-txt-files-then-loop-through-those-files-performing-actions/38696/5 "2020-05-03T21:42:34Z")

</div>

You can use join to get the absolute path.

```julia
filepaths = readdir("my folder",join=true)
for path in filepaths
    open(path) do io
         data = read(io)
         #do your stuff
    end
end

```

Other keywords that could help. String interpolation, joinpath, splitpath, endswith.

---

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [May 4, 2020, 1:18am UTC](https://discourse.julialang.org/t/point-to-directory-with-txt-files-then-loop-through-those-files-performing-actions/38696/6 "2020-05-04T01:18:11Z")

</div>

> [@Nash](#):
>
> How do I solve that problem?

Previous to responses are better for files specifically, but you might also look at the docs linked above for “string interpolation,” which is how you expand a variable in a string. Eg:

```julia
julia> name = "nash"
"nash"

julia> "Hi, $(name)!"
"Hi, nash!"

```

As I said, you shouldn’t really do that for file paths though, since all the slashes and stuff are tricky to deal with. Much better to use the provided functions
