Skip to content

Instantly share code, notes, and snippets.

@jenningsb2
Last active April 18, 2022 05:29
Show Gist options
  • Save jenningsb2/b16f2bcda8bf14d0ab604b80a38970b0 to your computer and use it in GitHub Desktop.
Save jenningsb2/b16f2bcda8bf14d0ab604b80a38970b0 to your computer and use it in GitHub Desktop.
Small script that loops through all of the files in your graphs asset folder, then searches your pages and journals files for references to them. If the asset file isn't referenced anywhere, it is deleted.
from multiprocessing.connection import wait
from os import listdir
import os
from os.path import isfile, join
from time import time
mygraphpath = "/Users/baileyjennings/Dropbox/Mac/Documents/baileys-mind-new-local"
# Get all the files in the assets folder
# capture file names as a list of strings
assets = []
for f in listdir(mygraphpath + "/assets"):
if isfile(join(mygraphpath + "/assets", f)):
assets.append(f)
# search the files text to see if is contain the asset file name
# if it does, list the asset file name in assetswithreference
assetswithreference = []
for asset in assets:
for journal in listdir(mygraphpath+"/journals"):
if asset in open(mygraphpath+ "/journals" + "/" + journal).read():
assetswithreference.append(asset)
for page in listdir(mygraphpath+"/pages"):
if asset in open(mygraphpath+"/pages" + "/" + page).read():
assetswithreference.append(asset)
# remove the assets that are not in the assetswithreference list from the assets folder
deleted_file_count = 0
for asset in assets:
if asset not in assetswithreference:
os.remove(mygraphpath + "/assets" + "/" + asset)
deleted_file_count += 1
print("DONE :D")
print("total_files_deleted: " + str(deleted_file_count))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment