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():
	autoSave()

func autoSave():
	var directory = Directory.new()
	var dirErr = directory.open(basePath)
	if dirErr != OK:
		printerr("Couldn't open directory " + basePath)
		return
	dirErr = directory.change_dir(localPath)
	if dirErr != OK:
		printerr("Couldn't change directory to" + basePath + "/" + localPath)
		return
	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)
		directory.rename(fName,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

Hi,
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