How can I get directory.copy to work when using Acces File system

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By michaelpbl

Here’s a simple script to demonstrate the problem

    extends Node2D

var basePath = "D://Documents/Dropbox/MichaelsDocuments/GodotProjects/fileCopy"
var localPath = "saves"

func _ready():

func autoSave():
	var directory =
	var dirErr =
	if dirErr != OK:
		printerr("Couldn't open directory " + basePath)
	dirErr = directory.change_dir(localPath)
	if dirErr != OK:
		printerr("Couldn't change directory to" + basePath + "/" + localPath)
	print("Current directory is " + directory.get_current_dir())
	var fName = "savefile.txt"
	if directory.file_exists(fName):
		print(fName + " does exist")
		# old backup automatically overwritten if it exists
		var backupName = "savefile-backup.txt"
		var reName = "savefile-renamed.txt" 
		print("Trying to copy " + fName + " to " + backupName)
		directory.copy(fName, backupName)
		print("Trying to rename " + fName + " to " + reName)

And here’s the console output

Current directory is D:/Documents/Dropbox/MichaelsDocuments/GodotProjects/fileCopy/saves
savefile.txt does exist
Trying to copy savefile.txt to savefile-backup.txt
ERROR: copy: Failed to open savefile.txt
   At: core/os/dir_access.cpp:288
Trying to rename savefile.txt to savefile-renamed.txt

The copy fails but the rename command works. To me that says there is nothing wrong with my setup but there is a problem with the copy command, at least insofar as working with Access File system. After considerable trial and effort, I have adopted Access File system as being best for my purposes and am not willing to change it.

I’m using window 10 and Godot v3.2.1.stable.official

:bust_in_silhouette: Reply From: deaton64

No idea why it doesn’t work, as the help states Both arguments should be paths to files, either relative or absolute for copy and rename.

If you do this: print(directory.copy(fName, backupName)) you get error 7, which is file not found.
It looks like it wants the full path.
This works for me:

var source = basePath + "/" + localPath + "/" + fName
var destination = basePath + "/" + localPath + "/" + backupName
directory.copy(source, destination)

Thank-you. I should have thought of that. I’ll use that as a workaround but it definitely looks like a bug.

michaelpbl | 2021-04-17 17:51