import sys, os, shutil, filecmp, configparser config = configparser.ConfigParser() if os.path.isfile("config.ini"): config.read("config.ini") else: config["Settings"] = { "sourceDir": "C:\\somepath", "copyToDir": "C:\\someotherpath", "foldersToCopy": "life1960,life1960_map,life1960_code" } with open("config.ini", "w") as configfile: config.write(configfile) print("Config created, please update it before running again.") sys.exit(0) sourceDir = config["Settings"]["sourceDir"] copyToDir = config["Settings"]["copyToDir"] if not os.path.exists(sourceDir): print("Invalid source directory") sys.exit(0) if not os.path.exists(copyToDir): print("Invalid destination directory") sys.exit(0) print("Copying files from: " + sourceDir) foldersToCopy = tuple(config["Settings"]["foldersToCopy"].split(",")) print(foldersToCopy) print("Copying files to: " + copyToDir) fileExtensionsToCopy = ( ".acp", ".adeb", ".ae", ".afm", ".agf", ".agr", ".anm", ".asc", ".asi", ".ast", ".asy", ".aw", ".bt", ".bterr", ".bttile", ".c", ".conf", ".ct", # ".dds", ".desc", ".edds", ".emat", ".ent", ".et", # ".fbx", ".fnt", ".gamemat", ".gproj", ".imageset", ".layer", ".layout", ".meta", ".nmn", ".pak", ".pap", ".physmat", ".pre", ".ptc", ".ragdoll", ".rdb", ".sig", ".siga", ".smap", ".st", ".stars", ".styles", ".terr", ".topo", ".ttf", ".ttile", ".txa", ".txo", ".vhcsurf", ".wav", ".xob" ) # Copy files to destination but compare to make sure not wasting time copying totalFiles = 0 totalFilesCopied = 0 totalFilesAlreadyValid = 0 for folder in foldersToCopy: for root, dirs, files in os.walk(sourceDir + "\\" + folder): for file in files: totalFiles = totalFiles + 1 if file.endswith(fileExtensionsToCopy): pathname = root + "\\" + file relname = pathname.replace(sourceDir, "") destpath = copyToDir + relname if os.path.isfile(pathname): if os.path.isfile(destpath) and filecmp.cmp(pathname, destpath, shallow=True): #print("Skipping " + pathname) totalFilesAlreadyValid = totalFilesAlreadyValid + 1 continue os.makedirs(os.path.dirname(destpath), exist_ok=True) shutil.copy2(pathname, destpath) totalFilesCopied = totalFilesCopied + 1 print("Copying " + relname) # purge files in destination which do not exist in source totalFilesPurged = 0 for folder in foldersToCopy: for root, dirs, files in os.walk(copyToDir + "\\" + folder): for file in files: pathname = root + "\\" + file relname = pathname.replace(copyToDir, "") sourcepath = sourceDir + relname if not os.path.isfile(sourcepath): totalFilesPurged = totalFilesPurged + 1 os.remove(pathname) print("Deleting " + relname + " from destination because it doesn't exist in the source!") print(str(totalFiles) + " total files. " + str(totalFilesCopied) + " copied. " + str(totalFilesAlreadyValid) + " were not copied because they are identical. " + str(totalFilesPurged) + " files were purged from destination.")