Rename files in a directory

In my directory, I have many files whose names all include the string “May_2022”. For example “Bob_May_2022”. I want to change the name of every file so that the part “May_2022” is replaced by “05_22”. For example to “Bob_05_22”.

How can I do this using Julia code?

Should be possible with the commands mv and walkdir from Base.Filesystem.

Something like

# untested code!
for (root, dirs, files) in walkdir(pwd())  
    for fn in files
        if occursin("May_2022", fn)
            mv(joinpath(root,fn), joinpath(root, replace(fn, "May_2022" => "05_22"))) 
        end
    end
end

This goes recursively through your files and does the job. For sure there is a more elegant solution :smiley:

3 Likes

That worked!