Created
April 4, 2021 12:07
-
-
Save huntfx/2b374dcd4885594aedc8c4cdb0340258 to your computer and use it in GitHub Desktop.
Quick script to mirror folders across multiple computers using Google Drive File Sync.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Quick script to mirror folders using Google Drive File Sync. | |
# Built for transferring VR game saves from a laptop to my main PC. | |
# For example, to keep Firefox in sync, create a "G:/My Drive/Firefox" | |
# directory, copy this script and create two batch files: | |
# upload.bat: py quicksync.py upload %APPDATA%/Mozilla/Firefox | |
# download.bat: py quicksync.py download %APPDATA%/Mozilla/Firefox | |
import sys | |
import os | |
import scandir | |
import filecmp | |
import shutil | |
def main(command, source): | |
assert command in ('upload', 'download'), 'unknown command' | |
source = os.path.normpath(os.path.expandvars(source)) | |
destination = os.path.normpath(os.path.join(os.path.dirname(__file__), 'files')) | |
if command == 'download': | |
source, destination = destination, source | |
# Ensure the output exists | |
if not os.path.exists(destination): | |
os.makedirs(destination) | |
all_source_dirs = set() | |
all_dest_dirs = set() | |
all_source_files = set() | |
all_dest_files = set() | |
# Find which files already exist | |
for root, dirs, files in scandir.walk(destination): | |
all_dest_dirs |= set(os.path.join(root, directory) for directory in dirs) | |
all_dest_files |= set(os.path.join(root, file) for file in files) | |
# Run through each file in the source location | |
for root, dirs, files in scandir.walk(source): | |
dest_root = root.replace(source, destination) | |
# Create new folders | |
for directory in dirs: | |
dest_dir = os.path.join(dest_root, directory) | |
all_source_dirs.add(dest_dir) | |
if not os.path.exists(dest_dir): | |
os.mkdir(dest_dir) | |
# Create new files | |
for file in files: | |
source_file = os.path.join(root, file) | |
dest_file = os.path.join(dest_root, file) | |
all_source_files.add(dest_file) | |
if not os.path.exists(dest_file) or not filecmp.cmp(source_file, dest_file): | |
shutil.copy2(source_file, dest_file) | |
# Delete any extra folders | |
for directory in all_dest_dirs - all_source_dirs: | |
if os.path.exists(directory): | |
shutil.rmtree(directory) | |
# Delete any extra files | |
for file in all_dest_files - all_source_files: | |
if os.path.exists(file): | |
os.remove(file) | |
if __name__ == '__main__': | |
main(sys.argv[1], sys.argv[2]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment