I have the below script. Basically, it loops through files in a specific directory, prints it, and then moves it to another directory.
I have a
.bat file that executes this script and does exactly as needed when executed manually.
However, when I setup a Task Scheduler to run the
.bat file, it runs the script exactly as needed except doing the actual printing. It sets the default printer, moves the files, and then at the end, sets the printer to another default.
I have set the user account to be a network administrator, it has local administrator rights on the client computer. In the schedule, I have highest permission enabled.
Any ideas on what to check are greatly appreciated!!
import os
import time
import shutil
import win32print
path = r'\\tsl-files\Share\_PostingJournal\Queue'
auditpath = r'\\tsl-files\Share\_PostingJournal\Log'
l_files = os.listdir(path)
for file in l_files:
file_path = f'{path}\\{file}'
win32print.SetDefaultPrinterW("LANIER MP 4055")
if os.path.isfile(file_path):
try:
os.startfile(file_path, 'print')
print(f'Printing {file}')
time.sleep(5)
shutil.move(f'{path}\\{file}', f'{auditpath}\\{file}')
except:
print(f'ALERT: {file} could not be printed! Please check\
the associated softwares, or the file type.')
else:
print(f'ALERT: {file} is not a file, so can not be printed!')
win32print.SetDefaultPrinterW("Generic / Text Only")
print('Task finished!')
What I have tried:
Since the file location specified in the script is a network location, I duplicated my script and set up a new task scheduler to execute directly from a folder on the
C Drive. It does the same exact thing. I was thinking that maybe since the bat file is being opened on the network location that maybe it was trying to start the pdf adobe reader from that location instead? But attempting locally as mentioned debunked that.
I'm not sure what else to test. Any ideas are greatly appreciated!!
Python 3.12.0
Windows 10 Pro