Julia copying folder into an existing folder

47 views Asked by At

Hi I am wondering how to copy folders into an existing folder in julia. In my case I have a folder called BigEarthNet-v1.0 which contains a number of sub-folders where each subfolder contains a number of tif files (i.e BigEarthNet-V1.0 -> [folder1, folder2, ...], where folder1 -> [image1.tif,image2.tif, ...]). I want to copy randomly 100 subfolders into an exiting folder called BigEarthNet-v1-concise. Literally make a concise dataset.

folders = readdir("BigEarthNet-v1.0")
random_paths = rand(folders, 100)
for folder in random_paths
    cp(joinpath("BigEarthNet-v1.0",folder), "BigEarthNet-v1.0-concise", force = true)
end

If I don't enter force = true i get an error. If I do the code works but literally only the last folder gets copied and it is also the files inside the folder rather than the folder itself. From what I am understand force =true deletes the existing folder BigEarthNet-v1-concise and recreates the folder again and this is why I get this behavior. Anyway to achieve what I am trying to do ?

1

There are 1 answers

0
ahnlabb On BEST ANSWER

You probably want something like:

from = "BigEarthNet-v1.0"
to = "BigEarthNet-v1.0-concise"
mkdir(to)
folders = readdir(from)
random_paths = rand(folders, 100)
for folder in random_paths
    cp(joinpath(from, folder), joinpath(to, folder))
end

The POSIX cp command behaves differently depending on if the target is an existing directory or not. This can make commands less verbose. If dir is an existing directory: cp "$path" dir is equivalent to cp "$path" "dir/$(basename "$path")"

If target exists and names an existing directory, the name of the corresponding destination path for each file in the file hierarchy shall be the concatenation of target, a single <slash> character if target did not end in a <slash>, and the pathname of the file relative to the directory containing source_file.

This makes common usage in the shell less verbose but it is less explicit and a decision has been made not to replicate this behavior in Julia. See e.g. this issue.