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

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/

1 Like

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.

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

for file in files_to_act_on
1 Like

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:


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


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?

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 on how string work in julia.


You can use join to get the absolute path.

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

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

1 Like

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> name = "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

1 Like