I am using batch file to send uploaded pdf document from my mobile to usb printer using dropbox folder. I use free PDFtoPrinter program to print and scheduled task to periodically run a batch file. The batch file contains following code.
"C:\Dropbox\PDFtoPrinter" "C:\Dropbox\Sent files\*.pdf"
move "c:\Dropbox\Sent files\*.pdf" "C:\Dropbox\printed"
The script function well if sent files folder contains pdf file but if it is empty, it gets stuck. I would like to use if statement to check if folder contains pdf file. The script should exist if none is found. I am not a programmer. Kindly help
"C:\Dropbox\PDFtoPrinter" "C:\Dropbox\Sent files\*.pdf"
move "c:\Dropbox\Sent files\*.*" "C:\Dropbox\printed"
Problem : if sent file folder is empty, the script gets stuck. Scheduled task next time does not work
CodePudding user response:
Here's my comments put together as a batch file.
@If Not Exist "C:\Dropbox\Sent files\*.pdf" Exit 1
@For %%G In ("C:\Dropbox\Sent files\*.pdf") Do @(
"C:\Dropbox\PDFtoPrinter.exe" "%%G"
If Not ErrorLevel 1 Move /Y "%%G" "C:\Dropbox\printed"
)
Alternatively, (as per Stephan's comment):
@If Exist "C:\Dropbox\Sent files\*.pdf" (
For %%G In ("C:\Dropbox\Sent files\*.pdf") Do @(
"C:\Dropbox\PDFtoPrinter.exe" "%%G"
If Not ErrorLevel 1 Move /Y "%%G" "C:\Dropbox\printed"
)
)
CodePudding user response:
if exist "C:\Dropbox\Sent files\*.pdf" "C:\Dropbox\PDFtoPrinter" "C:\Dropbox\Sent files\*.pdf"
move "c:\Dropbox\Sent files\*.*" "C:\Dropbox\printed" 2>nul
The if exist
will only execute the pdftoprinter
executable if there are .pdf
files present in the directory.
The 2>nul
will suppress error messages should there be no files in sent files
ALthough - perhaps the move
should use a filemask of *.pdf
(all .pdf
files) in place of *.*
(all files)