# Glob with variable names

**URL:** <https://discourse.julialang.org/t/glob-with-variable-names/57982>\
**Category:** General Usage\
**Tags:** question\
**Created:** [March 26, 2021, 12:22am UTC](https://discourse.julialang.org/t/glob-with-variable-names/57982 "2021-03-26T00:22:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jake](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jake/32/46007_2.png) [@Jake](https://discourse.julialang.org/u/Jake)\
**Post date:** [March 26, 2021, 12:22am UTC](https://discourse.julialang.org/t/glob-with-variable-names/57982/1 "2021-03-26T00:22:08Z")

</div>

I am trying out some pattern matching with glob. When I try it using subfolders it works if the subfolder is hardcoded, but not when the subfolder is a variable.

The purpose of the code is to delete files older than a set length of time. I have posted the code and the output of f1 should agree to the output of f2 but it does not. Any suggestions or is this a bug?

```julia
using Revise
using Glob

# function to delete old files
function datfilepurge(purgatoryfolder::String, retain::Period)
    datfiles = readdir(glob"$purgatoryfolder/*.dat")
    for (i, file) in enumerate(datfiles)
        age = now() - ctime(joinpath(purgatoryfolder, file))
        if age > retain
            rm(joinpath(purgatoryfolder, file))
        end
    end
end

purgatoryfolder = "purgatorytest"

# setup a subfolder and put some files into it
rm(purgatoryfolder, force = true, recursive = true)
mkdir(purgatoryfolder)
write(joinpath(purgatoryfolder, "file1.dat"), "file1 text"); sleep(1)
write(joinpath(purgatoryfolder, "file2.dat"), "file2 text"); sleep(1)
write(joinpath(purgatoryfolder, "file3.dat"), "file3 text"); sleep(1)
write(joinpath(purgatoryfolder, "file4.dat"), "file4 text"); sleep(1)
write(joinpath(purgatoryfolder, "file5.dat"), "file5 text"); sleep(1)

t = "$purgatoryfolder/*.dat" # prove that this is a valid construct
f1 = readdir(glob"$purgatoryfolder/*.dat") # This returns an emptyp String vector
f2 = readdir(glob"purgatorytest/*.dat") # This returns the files in the folder
@show(t, f1, f2)

retain = Second(3) # The length of time to retain the files for
datfilepurge(purgatoryfolder, retain) # Remove old files

f3 = readdir(glob"$purgatoryfolder/*.dat")
f4 = readdir(glob"purgatorytest/*.dat")
@show(f3, f4)
nothing

```

with the following output

```julia
t = "purgatorytest/*.dat"
f1 = String[]
f2 = ["purgatorytest\\file1.dat", "purgatorytest\\file2.dat", "purgatorytest\\file3.dat", "purgatorytest\\file4.dat", "purgatorytest\\file5.dat"]
f3 = String[]
f4 = ["purgatorytest\\file1.dat", "purgatorytest\\file2.dat", "purgatorytest\\file3.dat", "purgatorytest\\file4.dat", "purgatorytest\\file5.dat"]

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [March 26, 2021, 12:34am UTC](https://discourse.julialang.org/t/glob-with-variable-names/57982/2 "2021-03-26T00:34:30Z")

</div>

> [@Jake](#):
>
> ```julia
> f1 = readdir(glob"$purgatoryfolder/*.dat") # This returns an emptyp String vector
> 
> ```

`$` interpolation doesn’t work (by default) in string macros like `glob"..."` or `raw"...."` or `r"...."`: it is treated as a literal dollar sign. Try `Glob.GlobMatch("$purgatoryfolder/*.dat")` instead.

You can also do `readdir(glob"*.dat", purgatoryfolder)` or `glob("*.dat", purgatoryfolder)`.

---

<div class="post-metadata">

**Author:** ![jameson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jameson/32/23_2.png) [@jameson](https://discourse.julialang.org/u/jameson)\
**Post date:** [January 8, 2025, 4:26pm UTC](https://discourse.julialang.org/t/glob-with-variable-names/57982/3 "2025-01-08T16:26:12Z")

</div>

Jumping in late to note that (as the author of Glob), this is somewhat intentionally unimplemented, as putting a path into the fnmatch is rather risky (it doesn’t work well on Windows with the path separator is different or on Unix where metacharacters are legal in the path itself)
