【发布时间】:2019-08-22 10:01:14
【问题描述】:
这批目前我用来从网站打印我们的每日报告,然后将其保存为 pdf(作为备份)并打印一份硬拷贝。
在保存 pdf 期间,每天会生成 2 个不同大小的 pdf 文件(仅限 1 天 1 pdf)。一些 pdf 的大小将超过 20kb,其中包含我们的每日报告,而一些将在 9kb(8485 字节)左右(空报告,因为那天没有销售)。
我现在的目标是通过防止那天不会打印那些空报告来节省纸张。那么如何在我当前的代码中添加一些代码,以便在打印之前,批处理将在进行打印作业之前检查文件大小。如果可能,我也想创建一个 msgbox,以便在由于报告为空而没有打印 pdf 时提示用户。
参考以下代码:
- 设置日期参数
- 创建文件夹和子文件夹
- 使用wkhtmltopdf将网页转成PDF
- 打印前检查文件大小 这就是我要补充的。如果生成的 pdf 大小超过 9kb,则自动转到第 5 和第 6。否则,使用 msgbox 提示用户“今天没有报告,按 OK 关闭计算机”
- 打印
- 关闭计算机
@echo off
title Daily Report to PDF
REM ---------------------------------------------------------------------------
REM 1. Setting date parameter
for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set date=%%a
set month=%date:~4,2%
if %month%==01 set monthname=January
if %month%==02 set monthname=Febuary
if %month%==03 set monthname=March
if %month%==04 set monthname=April
if %month%==05 set monthname=May
if %month%==06 set monthname=June
if %month%==07 set monthname=July
if %month%==08 set monthname=August
if %month%==09 set monthname=September
if %month%==10 set monthname=October
if %month%==11 set monthname=November
if %month%==12 set monthname=December
for /f %%i in ('wmic path win32_localtime get dayofweek ^| findstr [0-9]') do set dayofweek=%%i
if %dayofweek%==1 set dow=(Mon)
if %dayofweek%==2 set dow=(Tue)
if %dayofweek%==3 set dow=(Wed)
if %dayofweek%==4 set dow=(Thu)
if %dayofweek%==5 set dow=(Fri)
if %dayofweek%==6 set dow=(Sat)
if %dayofweek%==7 set dow=(Sun)
REM ---------------------------------------------------------------------------
REM 2. Creating folder and subfolders
set dir1="%USERPROFILE%\Desktop\TEST"
set day=%date:~6,2%
set months=%monthname:~0,3%
set year=%date:~0,4%
set fulldate=%day%%months%%year%%dow%
md %dir1%\"YEAR %year%"\%monthname% 2>NUL
cd %dir1%\"YEAR %year%"\%monthname%\
md a b c 2>NUL
REM ---------------------------------------------------------------------------
REM 3. Using wkhtmltopdf to webpage to PDF
set dir2="%USERPROFILE%\Desktop\TEST\YEAR %year%\%monthname%"
echo.
echo Saving report as %fulldate%.pdf
wkhtmltopdf -q -g https://www.google.com %dir2%\c\%fulldate%.pdf
echo.
REM ---------------------------------------------------------------------------
REM 4. Check file size before printing
REM ---------------------------------------------------------------------------
REM 5. Printing
echo Printing %fulldate%.pdf
echo.
echo "C:\\Program Files (x86)\Foxit Software\Foxit Reader\Foxit Reader.exe" /t %dir2%\c\%fulldate%.pdf
REM ---------------------------------------------------------------------------
REM 6. Shutting down computer
shutdown -s -t 60 -c
PAUSE
::EXIT
【问题讨论】:
标签: batch-file msgbox