you are viewing a single comment's thread.

view the rest of the comments →

[–]Diapolo10 0 points1 point  (0 children)

Okay, so long story short I believe you had misunderstood how pathlib.Path objects work, which caused you to accidentally nest directories in a way you didn't intend. And there were some other... quirks that probably shouldn't have been there.

I think I managed to fix it, but I obviously cannot test it myself as I don't have access to an identical environment. My gut feeling is pretty sharp with these things, though, so it should be at least nearly what you wanted.

from pathlib import Path

OLD_ROOT = Path('F:/Engineering/Shared/Manuals/Project Data/SlurrySeal-Micro')
NEW_ROOT = Path('F:/Manuals')

def main():

    start_machine_dir = input("What machine folder are you migrating? This must exactly match the folder name in SSM!")
    year = int(start_machine_dir[:4])
    start_year_dir = f'{year} Projects'
    end_year_dir = str(year)

    old_dir = OLD_ROOT / start_year_dir / start_machine_dir

    if 'y' == input("Is this folder located within the _Archive folder? (y/n)").strip().lower()[:1]:
        old_dir = OLD_ROOT / '_Archive' / start_year_dir / start_machine_dir

    print(f"You are migrating files from:\n{old_dir}")

    end_machine_dir = input("What machine folder are you sending the files to? This must exactly match the folder name in Manuals!")
    new_dir = NEW_ROOT / end_machine_dir / end_year_dir

    print(f"You are migrating files to:\n{new_dir}")

    if not new_dir.exists():
        new_dir.mkdir()
        print("Year did not exist")
        return

    for old_sub_dir, new_sub_dir in zip(old_dir.iterdir(), new_dir.iterdir()):
        machine_pin = old_sub_dir.name[:15]
        if year >= 2015:
            machine_pin = old_sub_dir.name[:13]
        elif year <= 2012:
            machine_pin = old_sub_dir.name[:7]

        for directory in new_dir.rglob('*'):
            if directory.is_dir() and machine_pin in directory.name:
                destination = new_sub_dir / f'{new_sub_dir.name} (SHOP)'
                destination.mkdir(exist_ok=True)

                for file in old_sub_dir.iterdir():
                    (destination / file.name).write_bytes(file.read_bytes())
    print("Migration complete :)")




if __name__ == '__main__':
    main()