【问题标题】:Batch to check file size before proceed to printing在继续打印之前批量检查文件大小
【发布时间】:2019-08-22 10:01:14
【问题描述】:

这批目前我用来从网站打印我们的每日报告,然后将其保存为 pdf(作为备份)并打印一份硬拷贝。
在保存 pdf 期间,每天会生成 2 个不同大小的 pdf 文件(仅限 1 天 1 pdf)。一些 pdf 的大小将超过 20kb,其中包含我们的每日报告,而一些将在 9kb(8485 字节)左右(空报告,因为那天没有销售)。
我现在的目标是通过防止那天不会打印那些空报告来节省纸张。那么如何在我当前的代码中添加一些代码,以便在打印之前,批处理将在进行打印作业之前检查文件大小。如果可能,我也想创建一个 msgbox,以便在由于报告为空而没有打印 pdf 时提示用户。

参考以下代码:

  1. 设置日期参数
  2. 创建文件夹和子文件夹
  3. 使用wkhtmltopdf将网页转成PDF
  4. 打印前检查文件大小 这就是我要补充的。如果生成的 pdf 大小超过 9kb,则自动转到第 5 和第 6。否则,使用 msgbox 提示用户“今天没有报告,按 OK 关闭计算机”
  5. 打印
  6. 关闭计算机

@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


    【解决方案1】:

    这是一个示例,它试图模仿您发布的示例,其中包括一些更改,主要是日期变量的Setting。

    @Echo Off
    Title Daily Report to PDF
    
    Rem     ------------------------------------------------------------------------
    Rem 1. Setting date variables
    
    Set "_WeekDays=Sunday Monday Tuesday Wednesday Thursday Friday Saturday"
    For /F %%A In ('WMIC Path Win32_LocalTime Get /Value^
     ^|FindStr "[0-9]$"')Do Set "_%%A"
    For /F "Tokens=1%_DayOfWeek%" %%A In (". . . . . . . . . %_WeekDays%"
    )Do Set "_DayFullName=%%A"
    Set "_DayShortName=%_DayFullName:~,3%"
    For %%A In (Day Hour Minute Month Second
    )Do Call Set "_%%A=0%%_%%A%%" & Call Set "_%%A=%%_%%A:~-2%%"
    For %%A In (January.01 February.02 March.03 April.04 May.05 June.06
        July.07 August.08 September.09 October.10 November.11 December.12
    )Do If ".%_Month%"=="%%~xA" Set "_MonthFullName=%%~nA"
    Set "_MonthShortName=%_MonthFullName:~,3%"
    Set "_FullDateString=%_Day%%_MonthShortName%%_Year%%_DayShortName%"
    
    Rem     ------------------------------------------------------------------------
    Rem 2. Creating folder and subfolders
    
    Set "_Dir1=%UserProfile%\Desktop\TEST\YEAR %_Year%\%_MonthFullName%"
    
    For %%A In (a b c)Do MD "%_Dir1%\%%A" 2>NUL
    
    Rem     ------------------------------------------------------------------------
    Rem 3. Using wkhtmltopdf to convert webpage to PDF
    
    Echo=
    Echo Saving report as %_FullDateString%.pdf
    wkhtmltopdf -q -g https://www.google.com "%_Dir1%\c\%_FullDateString%.pdf"
    Echo=
    
    Rem     ------------------------------------------------------------------------
    Rem 4. Printing files with sizes over 8485 bytes
    
    Set "_Exe1=%ProgramFiles(x86)%\Foxit Software\Foxit Reader\Foxit Reader.exe"
    
    For %%A In ("%_Dir1%\c\%_FullDateString%.pdf")Do If %%~zA GTR 8485 (
        Echo Printing %%A&Echo=&"%_Exe1%" /t "%_Dir1%\c\%_FullDateString%.pdf")
    
    Rem     ------------------------------------------------------------------------
    Rem 5. Shutting down computer
    
    ShutDown /S /T 60
    

    注意:此脚本假定可执行文件 wkhtmltopdf 在批处理脚本运行时能够从当前目录运行。

    【讨论】:

      猜你喜欢
      • 2012-07-02
      • 2011-12-22
      • 1970-01-01
      • 1970-01-01
      • 2018-01-12
      • 2015-08-15
      • 1970-01-01
      • 2015-02-21
      相关资源
      最近更新 更多