One of the big problems over the last few years is an issue I have been having with Windows Print Servers where the print job gets stuck in the Queue. To resolve this I have to stop the services, delete the files, and start the services up again a very manual process.
Cause
This happened in one of two scenarios. When printing Pictures (JPG) from Widows 10 on the default picture viewer. Secondly printing Adobe Files (PDF) from the Windows 10 Edge Browser.
By Default, this is something that gets changed when setting up a new PC but it does happen with new updates that the defaults sometimes revert back.
Solution
As this happens regularly enough and after various other attempts to resolve the issue. The last resort was to create a script that will do the work.
The aim was simply to check if a file was in the print spool file for more than an X amount of time and it if was Delete the file to prevent it from holding up the queue. As this is normally for an 8:00 to 5:00 environment we did not have to work with Dates as there was no chance for a rollover from the next day.
Script
echo Off & SetLocal EnableExtensions EnableDelayedExpansion
echo #### %Time% #### >> C:\Temp\print.txt
::###Delete Files Older than xx Minutes Stuck in Print Spooler >> C:\Temp\Print.txt
Set MinToCheck=15
::###Set Print Spool Path
Set SPath=C:\Windows\System32\spool\PRINTERS
::###Get Current Time (CT)
set CT=%Time:~0,5%
::###Get Deletion Time Hours (DTH)
set DTH=%CT:~0,2%
::###Get Deletion Time Minutes (DTM)
set /a DTM=%CT:~3,2%
::###Convert Hours to Minutes
Set /a DTH=%DTH%*60
::###Add All Minutes Together and subtract MinToCheck. Total Minute to Delete (TMTD)
set /a TMTD=%DTH%+%DTM%-%MinToCheck%
::###Loop Through Files in Directory get File Time and Generate file List
For /F "tokens=1 delims= " %%G in ('DIR %Spath% /B') do (
::###Loop Through Each File To Get Time Stamp
For /F "tokens=1,2 delims= " %%H in ('DIR %SPath%\%%G ^| findstr %%G') do (
::###Split Hours and Minutes
For /F "tokens=1,2 delims=:" %%J in ("%%I") Do (
::###Set Far for File Hours
set FHR=%%J
::Extract First Char Set /a FHR=!FHR:~0,1!
::Check if First Char is 0
IF !FHR! EQU 0 (
set /a FHR=%%J
Set /a FHR=!FHR:~1,1!
) Else (set /a FHR=%%J)
::### Convert Hours to Minutes
set /a FHR=!FHR!*60
::###Count Add hours and Minutes
Set /a TFT=!FHR!+%%K
::###What is happening with each File
Echo System Time in Minutes: %TMTD% . File Name: %%G . File Time in Minutes: !TFT! >> C:\Temp\print.txt
::### If Total-Time-to-Delete is more than Total-File-Hours, Delete the File.
If %TMTD% gtr !TFT! (del %SPath%\%%G /q)
If %TMTD% gtr !TFT! (Echo File %%G Deleted because %TMTD% gtr !TFT! >> C:\Temp\print.txt)
::###If Total-Time-to-Delete is more than Total-File-Hours, and the file still Exists
::###Stop the Print Spooler, Delete the File and Start the Print Spooler
If %TMTD% gtr !TFT! (if EXIST %SPath%\%%G (
Echo The File does not exsist >> C:\Temp\print.txt
net stop spooler
del %SPath%\%%G /q
net start spooler )))))
Explanation
For the Explanation on the script. See my YouTube video below.