# Trying to populate a structure inside a for loop but getting "MethodError: Cannot \`convert\` an object of type String to an object of type file\_info"

**URL:** <https://discourse.julialang.org/t/trying-to-populate-a-structure-inside-a-for-loop-but-getting-methoderror-cannot-convert-an-object-of-type-string-to-an-object-of-type-file-info/75437>\
**Category:** New to Julia\
**Created:** [January 29, 2022, 11:20pm UTC](https://discourse.julialang.org/t/trying-to-populate-a-structure-inside-a-for-loop-but-getting-methoderror-cannot-convert-an-object-of-type-string-to-an-object-of-type-file-info/75437 "2022-01-29T23:20:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon69491625](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@anon69491625](https://discourse.julialang.org/u/anon69491625)\
**Post date:** [January 29, 2022, 11:20pm UTC](https://discourse.julialang.org/t/trying-to-populate-a-structure-inside-a-for-loop-but-getting-methoderror-cannot-convert-an-object-of-type-string-to-an-object-of-type-file-info/75437/1 "2022-01-29T23:20:36Z")

</div>

just starting out and trying to get information about an directory on a linux disk that I pass as an arguement. I thought I’d try out building a structure use it to define an array. Then I would add each file’s information as it gets parsed. BUT I am getting the error below and I am not sure why?

thanks for any help AND if there is a better way to go about this happy to listen.

```julia
using Dates

struct file_info
       path::String
       human_time :: DateTime
       unix_time :: DateTime
end

for (root, dirs, files) in walkdir(ARGS[1])

    today_file_info = file_info[]
   
    for file in files
        filePath = joinpath(root, file);
        lastUpdatedTime = unix2datetime(stat(filePath).mtime);
        raw_time = stat(filePath).mtime
        println("=============")
        println(filePath," ", lastUpdatedTime, " ", raw_time )   
        println("=============")
        push!(today_file_info, filePath, lastUpdatedTime, raw_time)
    end
    
end

```

```julia
dave@deepthought:~/tontine_2022/sandbox$ julia "1_29_22_getting_file_dates.jl" /home/dave/tontine_2022
=============
/home/dave/tontine_2022/config/1 10 22 all the csv files.ods 2022-01-10T22:16:31.361 1.641852991361334e9
=============
ERROR: LoadError: MethodError: Cannot `convert` an object of type String to an object of type file_info
Closest candidates are:
  convert(::Type{T}, ::T) where T at ~/julia-1.7.0/share/julia/base/essentials.jl:218
  file_info(::String, ::DateTime, ::DateTime) at ~/tontine_2022/sandbox/1_29_22_getting_file_dates.jl:4
  file_info(::Any, ::Any, ::Any) at ~/tontine_2022/sandbox/1_29_22_getting_file_dates.jl:4
Stacktrace:
 [1] setindex!(A::Vector{file_info}, x::String, i1::Int64)
   @ Base ./array.jl:903
 [2] _append!
   @ ./array.jl:1060 [inlined]
 [3] append!
   @ ./array.jl:1050 [inlined]
 [4] push!
   @ ./array.jl:1051 [inlined]
 [5] top-level scope
   @ ~/tontine_2022/sandbox/1_29_22_getting_file_dates.jl:23
in expression starting at /home/dave/tontine_2022/sandbox/1_29_22_getting_file_dates.jl:12
dave@deepthought:~/tontine_2022/sandbox$ 

```

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [January 29, 2022, 11:26pm UTC](https://discourse.julialang.org/t/trying-to-populate-a-structure-inside-a-for-loop-but-getting-methoderror-cannot-convert-an-object-of-type-string-to-an-object-of-type-file-info/75437/2 "2022-01-29T23:26:58Z")

</div>

The following

```julia
using Dates

struct file_info
       path::String
       human_time :: DateTime
       unix_time :: Float64
end

for (root, dirs, files) in walkdir(".")

    today_file_info = file_info[]

    for file in files
        filePath = joinpath(root, file);
        lastUpdatedTime = unix2datetime(stat(filePath).mtime);
        raw_time = stat(filePath).mtime
        println("=============")
        println(filePath," ", lastUpdatedTime, " ", raw_time )
        println("=============")
        push!(today_file_info, file_info(filePath, lastUpdatedTime, raw_time))
    end

end

```

seems to work for me.

---

<div class="post-metadata">

**Author:** ![anon69491625](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@anon69491625](https://discourse.julialang.org/u/anon69491625)\
**Post date:** [January 29, 2022, 11:41pm UTC](https://discourse.julialang.org/t/trying-to-populate-a-structure-inside-a-for-loop-but-getting-methoderror-cannot-convert-an-object-of-type-string-to-an-object-of-type-file-info/75437/3 "2022-01-29T23:41:51Z")

</div>

thanks so much for helping me out. I certainly didn’t have clue as to how the struct mapping worked. Also thank you for fixing the unix\_time cockup. I missed that one as well.

**Is there a more interesting way to get this information in Julia?**

I just want to see the chronology of the file changes in a linux directory. I was thinking about messing around with calling bash functions but thought to learn a little about julia instead.

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [January 29, 2022, 11:51pm UTC](https://discourse.julialang.org/t/trying-to-populate-a-structure-inside-a-for-loop-but-getting-methoderror-cannot-convert-an-object-of-type-string-to-an-object-of-type-file-info/75437/4 "2022-01-29T23:51:55Z")

</div>

> [@anon69491625](#):
>
> I just want to see the chronology of the file changes in a linux directory

Maybe you want to [watch](https://docs.julialang.org/en/v1/stdlib/FileWatching/) for changes?

---

<div class="post-metadata">

**Author:** ![anon69491625](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@anon69491625](https://discourse.julialang.org/u/anon69491625)\
**Post date:** [January 30, 2022, 12:10am UTC](https://discourse.julialang.org/t/trying-to-populate-a-structure-inside-a-for-loop-but-getting-methoderror-cannot-convert-an-object-of-type-string-to-an-object-of-type-file-info/75437/5 "2022-01-30T00:10:03Z")

</div>

I looked into that using FileWatching and Glob. Remember I’m a noob so bear with me. My usecase is that I am using a cronjob to start a couple of python scripts that feeds data ever 5 seconds to two csv files. The file names are new everyday and constructed from a fixed name with date time info attached.  
the\_daily\_hedgehog\_data\_1\_29\_22\_8\_30.csv and so on. Another cronjob moves them overnight to another directory. I prefer to use cron for these tasks as it’s pretty robust. I know I could do it programmatically.

I want to know the name of those files so I can get a Julia script to start reading them as soon as the cronjob starts it up. So the python scripts are already running.

I know I could use the splendid PyCall for this or the equally excellent ZMQ but, for now, I want to have build a csv file of the data ( simplest approach), read it into a julia script and mess around with it there.

If there is a more Julia way of getting this done please point me at it. I’ll certainly go back to FileWatching but this way seemed a lot simpler.  
happy to be advised on this. I want to set off properly.

thanks again for helping me here.
