0 votes

Hi, I want to move the game folder to this location: C:\Users\"userofthePC"\AppData\Roaming
and my folder name is : C:\Users\"userofthePC\Desktop\Prototype
also I want to make it detect if the folder it already in the location.

Thank you for helping me!!!

in Engine by (77 points)

1 Answer

+1 vote

The OSX version works, but I don't currently have access to my computer running Windows, so I haven't tested that part

func get_os_username():
    var env_var = "USER"

    match OS.get_name().to_lower():
            env_var = "USERNAME"

    return OS.get_environment(env_var)

func move_directory_windows(original_path : String, new_path : String) -> bool:
    # see https://support.microsoft.com/en-us/help/323007/how-to-copy-a-folder-to-another-folder-and-retain-its-permissions
    # to decide which flags you actually want
    var arguments = [original_path, new_path, "/O", "/X", "/E", "/H", "/K"]
    OS.execute("xcopy", arguments, true)

    return (Directory.new()).dir_exists(new_path)

func move_directory_unix(original_path : String, new_path : String) -> bool:
    var arguments = [original_path, new_path]
    OS.execute("mv", arguments, true)

    return (Directory.new()).dir_exists(new_path)

func move_directory(original_path : String, new_path : String) -> bool:
    var dir : Directory = Directory.new()

    if not dir.dir_exists(original_path):
        print_debug("Source directory does not exist [%s]" % original_path)
        return false

    if dir.dir_exists(new_path):
        print_debug("Target directory exists [%s]" % new_path)
        return false

    match OS.get_name().to_lower():
            return move_directory_windows(original_path, new_path)
            return move_directory_unix(original_path, new_path)

And then use it like so:

var user = get_os_username()
var original_path = "C:\\Users\\%s\\AppData\\Roaming" % user
var new_path = "C:\\Users\\%s\\Desktop\\Prototype" % user

move_directory(original_path, new_path)
by (1,601 points)
edited by
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 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.