Created
January 23, 2024 07:59
-
-
Save jakerieger/03ce4dd4f32085c5af062e1131b338f7 to your computer and use it in GitHub Desktop.
Directory Cleanup Script
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
# EXAMPLE | |
''' | |
rootDir = "F:" | |
keepItems = ['some_file.txt', 'some_directory'] | |
Will delete everything in 'F:' that isn't in 'keepItems' | |
''' | |
import pathlib | |
from pathlib import WindowsPath | |
from typing import List | |
rootDir: str = "<root dir>" | |
rootPath: WindowsPath = pathlib.Path(rootDir) | |
keepItems = [] | |
deleteItems: List[str] = [] | |
for item in rootPath.iterdir(): | |
if item.name not in keepItems: | |
deleteItems.append('\"{root}/{stem}\"'.format(root = rootDir, stem = str(item.name))) | |
deleteItemsStr: str = ' '.join(deleteItems) | |
deleteCmd = "rm -rf {}".format(deleteItemsStr) | |
print(deleteCmd) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Assumes a bash terminal, tested with Git Bash on Windows 11