批量检查文件大小,然后再继续打印 [英] Batch to check file size before proceed to printing
问题描述
该批次当前用于从网站打印我们的每日报告,然后将其另存为pdf(作为备份)并打印一份纸质副本.
在保存pdf的过程中,每天会生成2种不同大小的pdf文件(仅1天1 pdf).某些pdf的大小将超过20kb,其中包含我们的每日报告,而某些pdf的大小将在9kb(8485字节)左右(空报告,因为那天没有销售).
我现在的目标是通过防止那些空报告来节省纸张,这一天将不会打印出来.因此,如何在当前代码中添加一些代码,以便在打印之前,批处理将在进行打印之前检查文件大小.如果可能的话,我也想创建一个msgbox,以便在由于报告为空而无法打印pdf时提示用户.
This batch currently im using to print our daily report from a website then saved it as pdf (as a backup) and print one hard copy.
During saving the pdf, there will be 2 different size of pdf files generated daily(1 day 1 pdf only). Size of some pdf will be more than 20kb which contain our daily report and some will be around 9kb (8485bytes) (empty report because that day no sales).
My target now to save paper by preventing those empty report will not be print on that day. So how to add some code into my current code so that before printing, batch will check for file size before go to printing job. If possible, I want to create a msgbox too so that user will be prompt when no pdf will be print because of report empty.
请参阅下面的代码:
- 设置日期参数
- 创建文件夹和子文件夹
- 使用wkhtmltopdf将网页转换为PDF
- 在打印之前检查文件大小 这就是我要添加的内容.如果生成的pdf大小超过9kb,则自动进入第5号和第6号.否则,使用msgbox提示用户今天没有报告,请按OK关闭计算机"
- 打印
- 关闭计算机
- Setting date parameter
- Creating folder and subfolders
- Using wkhtmltopdf to webpage to PDF
- Check file size before printing This is what I want to add. If generated pdf size more that 9kb, proceed to No 5 and No 6 automatically. Else, prompt user using msgbox "No report for today, Press OK to shutdown computer"
- Printing
- Shutting down computer
@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
推荐答案
下面是一个示例批处理文件,它试图模仿您发布的示例,并进行了一些更改,主要是Set
ting日期变量.
Here's an example batch-file which tries to mimic your posted example and which includes some changes, predominantly the Set
ting of date variables.
@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
能够在运行批处理脚本时从当前目录运行.
Note: This script assumes that the executable wkhtmltopdf
is able to run from the current directory at the time the batch script is run.
这篇关于批量检查文件大小,然后再继续打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!