0 votes

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 = Directory.new()
    var dirErr = directory.open(basePath)
    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

Godot version v3.2.1.stable.official
in Engine by (38 points)

1 Answer

0 votes

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)
by (1,967 points)

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.