Created
December 1, 2021 21:24
-
-
Save wuye9036/8b39bf051a61660545806b481858cb0a to your computer and use it in GitHub Desktop.
Create a sym copy of OneDrive that don't touch the on-cloud files.
This file contains 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
import pathlib | |
import winerror | |
import os | |
import ctypes | |
from ctypes import windll, wintypes | |
kernel32 = windll.kernel32 | |
def GetCompressedFileSize (filepath): | |
hi = wintypes.DWORD () | |
lo = kernel32.GetCompressedFileSizeW (filepath, ctypes.byref (hi)) | |
if lo == 0xffffffff and ctypes.GetLastError != winerror.NO_ERROR: | |
return -1 | |
else: | |
return lo + (hi.value * 2 << 31) | |
Empty = 0 | |
OnDisk = 1 | |
OnCloud = 2 | |
def CollectStatus(s: pathlib.Path): | |
if s.is_file(): | |
sizeOnDisk = GetCompressedFileSize(str(s)) | |
if sizeOnDisk == 0: | |
status = OnCloud | |
else: | |
status = OnDisk | |
return status, {s: status} | |
selfStatus = Empty | |
descendantStatus = {} | |
for child in s.iterdir(): | |
childStatus, childDescendantStatus = CollectStatus(child) | |
descendantStatus.update(childDescendantStatus) | |
selfStatus |= childStatus | |
descendantStatus[s] = selfStatus | |
return selfStatus, descendantStatus | |
def MakeSymCopy(s: pathlib.Path, d: pathlib.Path, statusDict: dict): | |
if statusDict[s] == OnDisk: | |
if not d.parent.exists(): | |
print(f"Create folder <{str(d.parent)}>.") | |
d.parent.mkdir(parents=True, exist_ok=False) | |
print(f"Create link <{str(d)}>.") | |
os.symlink(s, d, target_is_directory=s.is_dir()) | |
elif statusDict[s] == OnCloud: | |
return | |
else: | |
for child in s.iterdir(): | |
MakeSymCopy(child, d / child.name, statusDict) | |
if __name__ == "__main__": | |
root = pathlib.Path(r"E:\OneDrive\Photos\Archives") | |
target = pathlib.Path(r"E:\Personal\Photos\Archives") | |
_, descendantStatus = CollectStatus(root) | |
MakeSymCopy(root, target, descendantStatus) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment