How To Copy An Entire Directory

Godot Version

4.3

Question

I am making a programme which includes the ability to duplicate a folder inside a working directory, but I have ran into issues when it comes to the DirAccess.copy/copy_absolute commands.

var to_copy : String = "path" # absolute path of the folder we want to copy
var to_copy_to : String = to_copy + get_random_numbers(5) # to_copy plus 5 random numbers ranged 0-9 to give a new name for the folder
for i in range(0, 11): # tries to make a copy 10 times, if we can't we return an error later in the function
	if DirAccess.dir_exists_absolute(to_copy_to): # if it exists, tries to make a new name to copy with
		to_copy_to = to_copy + get_random_numbers(5)
	else: # a directory with this name shouldn't already exists, therefor continue with copy
		DirAccess.copy_absolute(to_copy, to_copy_to)
		printerr(DirAccess.copy_absolute(to_copy, to_copy_to))
		break

This code has been giving me a error code 12, saying in the console "Script @ function: Failed to open to_copy’s path/value "
And before when I had tried this using an instance of DirAccess to use the relative version I was also getting an opening error.

Am I misunderstanding how this command works, do you need a different command to copy a folder over a file, would I need to make a new directory and manually copy every file inside?

Thanks.

it might be slightly counter-intuitive, but you use rename_absolute to copy entire folders.
so you’ll want to do

DirAccess.rename_absolute(to_copy, to_copy_to)

I think this just does a move and not a copy. When it comes to files you may need to individually copy to the new folder. First make the folder using make_dir_recursive_absolute or one of the variants.