moving folder whit a line of code

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

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!!!

:bust_in_silhouette: Reply From: Eric Ellingson

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():
		"windows": 
			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():
		"windows":
			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)