How to print the longest filename in directory in Julia

Written by Administrator on Sunday May 10, 2020

First of all, let's get a list of all filenames in current directory:

files = readdir(pwd())

Then let's make a Dictionary with filename as a key and length of the filename as a value:

info = Dict{String,Integer}()

for (n,f) in enumerate(files)
    info["$(f)"] = length(f)
end

And now we can list filenames sorted alphabetically with a length of filename in brackets:

for key in sort(collect(keys(info)))
    println("$key ($(info[key]))")
end

Result:

bookings (8)
books.txt (9)
count_files.jl (14)
hello-world.jl (14)
libs (4)
list_only_dirs.jl (17)
list_only_files.jl (18)
show_longest_filename.jl (24)