Skip to content

Instantly share code, notes, and snippets.

@jakerieger
Created January 23, 2024 07:59
Show Gist options
  • Save jakerieger/03ce4dd4f32085c5af062e1131b338f7 to your computer and use it in GitHub Desktop.
Save jakerieger/03ce4dd4f32085c5af062e1131b338f7 to your computer and use it in GitHub Desktop.
Directory Cleanup Script
# 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)
@jakerieger
Copy link
Author

Assumes a bash terminal, tested with Git Bash on Windows 11

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment