this_josh
this_josh

Reputation: 383

How to get the name of the originally ran file in Julia

I'd like to create a log file which is named after the originally run Julia file, for example here julia foo.jl I'd want foo.jl. From within a Julia session how can I get this information>

Upvotes: 1

Views: 38

Answers (2)

fredrikekre
fredrikekre

Reputation: 10984

The global constant PROGRAM_FILE is set to the script name.

Upvotes: 4

this_josh
this_josh

Reputation: 383

This can be done by inspecting the stack

# first get the top of the stack
f = stacktrace()[1]

# then get the file's name as a string, note the is absolute.
abs_filename = String(f.file)
println(abs_filename)

# to get only the filename use
println(basename(abs_filename))

Upvotes: -1

Related Questions