# Loop and iterating question

**URL:** <https://discourse.julialang.org/t/loop-and-iterating-question/9389>\
**Category:** New to Julia\
**Created:** [February 28, 2018, 12:15pm UTC](https://discourse.julialang.org/t/loop-and-iterating-question/9389 "2018-02-28T12:15:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![arnuld](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/arnuld/32/5454_2.png) [@arnuld](https://discourse.julialang.org/u/arnuld)\
**Post date:** [February 28, 2018, 12:15pm UTC](https://discourse.julialang.org/t/loop-and-iterating-question/9389/1 "2018-02-28T12:15:42Z")

</div>

AIM: Want to run a shell command on all the .jl files  
PROBLEM: how can i create a loop to iterate over unknown number of elements and run the shell command on them. I want it to be independent of number of files in directory.

```julia
const cmd1 = "ls"
const cmd2 = "-lh"
const cmd3 = "--color=auto"

function find_size()
    const myfile = "/home/arnuld/Templates/file.jl"
    run(`$cmd1 $cmd2 $cmd3 $myfile`)
end

find_size()

for i = 1:10
    run(`$cmd1 $cmd2 $cmd3 $i.jl`)
end

```

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [February 28, 2018, 12:20pm UTC](https://discourse.julialang.org/t/loop-and-iterating-question/9389/2 "2018-02-28T12:20:32Z")

</div>

Does the following help?

```julia
help?> walkdir
search: walkdir

  walkdir(dir; topdown=true, follow_symlinks=false, onerror=throw)

  Return an iterator that walks the directory tree of a directory. The iterator returns a tuple containing (rootpath, dirs, files). The directory tree can be traversed top-down or bottom-up. If walkdir encounters a SystemError it will rethrow the error by default. A custom
  error handling function can be provided through onerror keyword argument. onerror is called with a SystemError as argument.

  for (root, dirs, files) in walkdir(".")
      println("Directories in $root")
      for dir in dirs
          println(joinpath(root, dir)) # path to directories
      end
      println("Files in $root")
      for file in files
          println(joinpath(root, file)) # path to files
      end
  end

```

---

<div class="post-metadata">

**Author:** ![arnuld](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/arnuld/32/5454_2.png) [@arnuld](https://discourse.julialang.org/u/arnuld)\
**Post date:** [March 1, 2018, 4:44am UTC](https://discourse.julialang.org/t/loop-and-iterating-question/9389/3 "2018-03-01T04:44:39Z")

</div>

Yeah. nice. I played with it and wrote this. It works. Thanks for the help 🙂

```julia
# This program will iterate over all .jpg files in the given directory
# and tell their size

dirpath = "/home/arnuld/Pictures"

extensionName = ".jpg"
extensionSize = 4 # for .jpg has 4 characters: dot, j, p, g

function runShellCommand(str, sz)
    for(root, dirs, files) in walkdir(dirpath)
        println("Files in $root")
        for file in files
            s = joinpath(root, file)
            l = length(s)
            pos = l - sz + 1
            if s[pos:end] == str
                run(`ls -lh $s`)
            end
        end
    end
end

runShellCommand(extensionName, extensionSize)

```

---

<div class="post-metadata">

**Author:** ![mtsch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mtsch/32/2072_2.png) [@mtsch](https://discourse.julialang.org/u/mtsch)\
**Post date:** [March 1, 2018, 8:40am UTC](https://discourse.julialang.org/t/loop-and-iterating-question/9389/4 "2018-03-01T08:40:33Z")

</div>

Note that you can use `splitext` to get the file extension, so `extensionSize` is not needed in your case.

---

<div class="post-metadata">

**Author:** ![arnuld](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/arnuld/32/5454_2.png) [@arnuld](https://discourse.julialang.org/u/arnuld)\
**Post date:** [March 2, 2018, 8:47am UTC](https://discourse.julialang.org/t/loop-and-iterating-question/9389/5 "2018-03-02T08:47:12Z")

</div>

Cool 🕶 . Too many end statements though 😑

```julia
# This program will iterate over all .jpg files in the given directory
# and tell their size

dirpath = "/home/arnuld/Pictures"

extensionName = ".jpg"

function runShellCommand(str)
    for(root, dirs, files) in walkdir(dirpath)
        println("Files in $root")
        for file in files
            s = joinpath(root, file)
            if(extensionName == splitext(s)[2])
                run(`ls -lh $s`)
            end
        end
    end
end

runShellCommand(extensionName)

```
