Batch to check file size before proceed to printing

谁都会走 提交于 2019-12-11 15:54:37

问题


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.

Refer to code below:

  1. Setting date parameter
  2. Creating folder and subfolders
  3. Using wkhtmltopdf to webpage to PDF
  4. 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"
  5. Printing
  6. 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

回答1:


Here's an example batch-file which tries to mimic your posted example and which includes some changes, predominantly the Setting 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

Note: This script assumes that the executable wkhtmltopdf is able to run from the current directory at the time the batch script is run.



来源:https://stackoverflow.com/questions/57606785/batch-to-check-file-size-before-proceed-to-printing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!