|
| 1 | +import os, re |
| 2 | +from tqdm import tqdm |
| 3 | +__name__ = "__main__" |
| 4 | + |
| 5 | +def scanFolders(inputDirectory): |
| 6 | + foldersFound=[] |
| 7 | + for folder in os.listdir(inputDirectory): |
| 8 | + if os.path.isdir(os.path.join(inputDirectory,folder)): |
| 9 | + foldersFound.append(folder) |
| 10 | + |
| 11 | + # checks if you're pointing it to a folder with subfolders in: |
| 12 | + fullPaths=[] |
| 13 | + print("Checking for subfolders in top level directory:\n",flush=True) |
| 14 | + for folder in tqdm(foldersFound): |
| 15 | + fullpath=inputDirectory+'\\'+folder |
| 16 | + if os.path.isdir(fullpath): |
| 17 | + fullPaths.append(fullpath) |
| 18 | + |
| 19 | + if fullPaths: |
| 20 | + # compile regexs outside of loops |
| 21 | + folderPatternOne=re.compile("(^\d{7}_\d{4}$)",re.MULTILINE) |
| 22 | + folderPatternTwo=re.compile("(^\d{7}_\d{4}_[a-zA-Z]{3}_[a-zA-Z]{3}$)",re.MULTILINE) |
| 23 | + |
| 24 | + print("\nChecking folder name compliance:\n",flush=True) |
| 25 | + with open ('nonCompliantFolders.txt','a+') as badFolders: |
| 26 | + for folder in tqdm(foldersFound): |
| 27 | + matchOne=folderPatternOne.match(folder) |
| 28 | + matchTwo=folderPatternTwo.match(folder) |
| 29 | + if matchOne is None: |
| 30 | + if matchTwo is None: |
| 31 | + badFolders.write(folder+'\n') |
| 32 | + |
| 33 | + return fullPaths |
| 34 | + else: |
| 35 | + print("No subfolders found within this directory!",flush=True) |
| 36 | + return 0 |
| 37 | + |
| 38 | +def scanFiles(pedantic,fullPaths): |
| 39 | + print("\nChecking filename compliance:\n",flush=True) |
| 40 | + with open('nonCompliantFiles.txt','a+') as badFileNames: |
| 41 | + for directoryPath in tqdm(fullPaths): |
| 42 | + filenames=os.listdir(directoryPath) |
| 43 | + filePattern=(os.path.basename(directoryPath))+r'(_\d{4}.tif)' |
| 44 | + if pedantic is True: |
| 45 | + regexPattern=re.compile(filePattern) |
| 46 | + else: |
| 47 | + regexPattern=re.compile(filePattern,re.IGNORECASE) |
| 48 | + for tifFile in filenames: |
| 49 | + match=regexPattern.match(tifFile) |
| 50 | + if match is None: |
| 51 | + outstring=directoryPath+'\\'+tifFile |
| 52 | + badFileNames.write(outstring+'\n') |
| 53 | + |
| 54 | +def setup(): |
| 55 | + pedantic=False |
| 56 | + mode=input("Type Y to run in case-sensitive filename checking mode, or enter to continue in case-insensitive mode:\n") |
| 57 | + if mode is "Y": |
| 58 | + pedantic=True |
| 59 | + else: |
| 60 | + pedantic=False |
| 61 | + if pedantic is True: |
| 62 | + print("Running in case-sensitive mode\n") |
| 63 | + else: |
| 64 | + print("Running in case-insensitive mode\n") |
| 65 | + |
| 66 | + |
| 67 | + inputDirectory=r"\\P12B-NAS1\scandata2\HMD\RAW SCANS\Dave\FMP\Still to deliver to FMP" |
| 68 | + inDir=input("Scan current folder? Y/N:\n") |
| 69 | + if (inDir=="Y" or inDir=="y" or inDir=="Yes" or inDir=="yes"): |
| 70 | + inputDirectoryUser=os.getcwd() |
| 71 | + elif (inDir=="N" or inDir=="n" or inDir=="No" or inDir=="no"): |
| 72 | + inputDirectoryUser=input("Enter full path of folder to scan:\n") |
| 73 | + if inputDirectoryUser=="": |
| 74 | + print("No path entered, using default filepath: {}\n".format(inputDirectory),flush=True) |
| 75 | + inputDirectoryUser=inputDirectory |
| 76 | + else: |
| 77 | + inputDirectoryUser=inputDirectory |
| 78 | + print("Input not recognised, using default filepath: {}\n".format(inputDirectory)) |
| 79 | + print("Checking if directory exists...\n",flush=True) |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | + if os.path.isdir(inputDirectoryUser): |
| 84 | + print("Input directory: {}\ exists!\n".format(inputDirectoryUser)) |
| 85 | + inputDirectory = inputDirectoryUser |
| 86 | + else: |
| 87 | + print("File path not a valid directory, using default path {}\n".format(inputDirectory)) |
| 88 | + |
| 89 | + return pedantic,inputDirectory |
| 90 | + |
| 91 | +def main(): |
| 92 | + |
| 93 | + isPedantic,inputDir=setup() |
| 94 | + filePaths=scanFolders(inputDir) |
| 95 | + scanFiles(isPedantic,filePaths) |
| 96 | + |
| 97 | + |
| 98 | + input("Complete! Press enter to exit") |
| 99 | + |
| 100 | + |
| 101 | +if __name__=="__main__": |
| 102 | + main() |
0 commit comments