I’ve recently made a push to expand my understanding of Python. Here is my first program. I’m sure it’s crude and needs refactoring, but hey, it works.
Problem: My wireless Brother AIO only scans to an active logged in machine when using the “scan to” button on the printer. This is inconvenient because I don’t always have the target machine running. I do run a home server so why
Solution: I do run a home server so why not make it the receiver and then get the files from there or even better, store them on my USB flash drive on the router? This script accomplishes that with some useful features:
Method: It renames the file to my preferred date format from the Brother standard “CCF_01132017.pdf”. The problem is that this file name cannot be sorted by name easily to determine the age of scans. The convention I chose is “20170113_0656_0.pdf” which does allow scans to be sorted and the tail of the filename is the iterator of files processed.
The script is triggered by windows task scheduler once per minute. This could just as easily be handled by python using the timedelta function and leaving the script running from startup. I don’t really know which would be more efficient.
UPDATE 1: The desktop.ini hidden file is read by python and will be processed as a file the others. There are two approaches to solving this: turn off the desktop.ini in the registry or build an exclusion in the code to eliminate processing of this file. You could also only apply the script to PDF files which is probably the best solution, but likely not as elegant.
1 # Scanned PDF renamer 2 # Geoffrey Todd Lamke, MD 3 # Python 3.6 4 # 2017-01-08 5 6 7 #Get Date in a format for filename 8 import datetime, os, shutil 9 now = datetime.datetime.now() 10 now = now.strftime("%Y%m%d_%M_") 11 iterator = 0 12 13 #Get CWD for rename operation 14 curDir = os.getcwd() 15 16 #Get Filelist from CWD 17 filelist= os.listdir(curDir) 18 19 #for loop renaming files 20 for fn in filelist: 21 newName = now + str(iterator)+".pdf" 22 try: 23 if fn != "pdfrenamer.py": 24 if os.path.isfile(newName) == False: 25 shutil.copy(fn, newName) 26 shutil.move(fn, "\\\\10.10.xx.xx\\asus\\ScannedPDF\\" + newName) 27 iterator += 1 28 print(iterator) 29 if os.path.isfile("\\\\10.10.xx.xx\\asus\\ScannedPDF\\" + newName) == True: 30 31 else: 32 print(fn + " Exists - Not Renaming") 33 except: 34 iterator +=1 35 print(iterator) 36 37 38 #Debug 39 #print("CWD = " + curDir) 40 #print("Files = " + str(filelist)) 41 #print(now) 42 43