How to rename files with spaces in file and directory names

Your code is so elegant!

But I need the check to make sure it also works if some of the files do NOT contain any space in the filename. This is my code now:

const BASEDIR="./input"
const DOCDIR="./doc"

function fix_names(root, names)
    for name in names
        old_name = joinpath(root, name)
        new_name = joinpath(root, replace(name, ' ' => '_'))
        if old_name != new_name
            mv(old_name, new_name)
        end
    end
end

function fix_names(dir)
    for (root, dirs, files) in walkdir(dir, topdown=false)
        fix_names(root, [dirs; files])
    end
end

fix_names(BASEDIR)
fix_names(DOCDIR)

Case collisions cannot happen. You will only have a collision if there is no space in the filename.

Thank you very much!

2 Likes