I intended to, but text files can’t get uploaded here. I’ll just paste the current contents, but it’s not in the exact state at which the screenshots were taken:
from __future__ import print_function
import os
def quotePath(path):
return "'" + path.replace("'", "\\'") + "'"
def shouldIgnoreItem(itemName):
return itemName.startswith('.') or \
itemName.endswith('~') or \
itemName == 'cvs'
def shouldIgnoreFile(fileName):
return fileName.endswith == '.tmp' or shouldIgnoreItem(fileName)
def addPaths(resultSet, rootPath, itemNames):
resultSet.update([os.path.join(rootPath, itemName) for itemName in itemNames])
def report(header, filePaths):
if not filePaths:
return
print(header)
for filePath in filePaths:
print(' ', filePath)
emptyDirPaths = []
dirPathsWithMissingMetas = set()
filePathsWithMissingMetas = set()
orphanedMetaFilePaths = set()
rootPath = r'D:\some\path'
for dirPath, dirNamesList, fileNamesList in os.walk(rootPath):
if not fileNamesList and not dirNamesList:
emptyDirPaths.append(dirPath)
# proactively flag this, since it will be orphaned
orphanedMetaFilePaths.add(dirPath + '.meta')
continue
for dirName in dirNamesList:
if shouldIgnoreItem(dirName):
dirNamesList.remove(dirName)
dirNames = set(dirNamesList)
metaNamesWithoutExt = set()
fileNames = set()
for fileName in fileNamesList:
if shouldIgnoreFile(fileName):
continue
name, ext = os.path.splitext(fileName)
if ext.lower() == '.meta':
metaNamesWithoutExt.add(name)
else:
fileNames.add(fileName)
orphanedMetaNamesWithoutExt = metaNamesWithoutExt - (dirNames.union(fileNames))
orphanedMetaNames = set([mn + '.meta' for mn in orphanedMetaNamesWithoutExt])
addPaths(dirPathsWithMissingMetas, dirPath, dirNames - metaNamesWithoutExt)
addPaths(filePathsWithMissingMetas, dirPath, fileNames - metaNamesWithoutExt)
addPaths(orphanedMetaFilePaths, dirPath, orphanedMetaNames)
report('emptyDirPaths', emptyDirPaths)
report('orphanedMetaFilePaths', orphanedMetaFilePaths)
report('dirPathsWithMissingMetas', dirPathsWithMissingMetas)
report('filePathsWithMissingMetas', filePathsWithMissingMetas)
for dirPath in emptyDirPaths:
print("Removing dir %s" % quotePath(dirPath))
os.rmdir(dirPath)
for filePath in orphanedMetaFilePaths:
print("Removing file %s" % quotePath(filePath))
os.unlink(filePath)