Q:

Batch File to check folder contents and restart print spooler not working

We have an issue where a 24in plotter keeps freezing up and the only way to fix it is to stop the print spooler, delete the printer contents, then restart the spooler. For non-technical people this is quite difficult so I thought I'd write a simple batch file (below) to assist...
@echo off
echo Press any key to fix Plotter frozen print queue.
pause
sc stop Spooler
echo Spooler service stopped, please wait.
timeout 5
if exist "C:\Windows\System32\spool\PRINTERS\*.*" (
echo Files are in the print queue
echo **check no other printer is being used before continuing**
pause
del "C:\Windows\System32\spool\PRINTERS\*.*"
) else (
echo There are no files in any print queues
)
sc start Spooler
echo Spooler service restarting, please wait.
timeout 5
pause

Now this works all apart from the feedback from the folder... if the folder is empty it still says that there is something there!?
I know it's a question within a question but I can't also see a way of checking the spooler status when it is running... so I've used timeout to delay things
The service has states but I don't know how to check these in a batch file, I also don't know the states of running and stopped :/
STATE              : 2  START_PENDING
STATE              : 3  STOP_PENDING

Thanks in advance

A:

To know, if there are files in the folder:
dir /b "C:\Windows\System32\spool\PRINTERS\*" | findstr "^" 2>nul && (
  echo files found 
) || (
  echo no files found
)

To get the spooler state:
for /f "tokens=3,4" %%a in ('sc query spooler ^|find "STATE"') do set "state=%%a %%b"
echo Spooler state: %state%

(probably you need %%a only for numerical comparison or %%b only for string comparison)

