@echo off & setlocal enableextensions enabledelayedexpansion

:: ziptree.bat for citeswood. Revised 30/7/2018. M.J. Dallwitz.
if "%1" equ "/?" (
   echo.
   echo Zips files from the current folder and some subfolders.
   echo The subfolder names are specified in the zip command within ziptree.bat.
   echo The name of the zipfile is the name of the current ^(main^) subfolder,
   echo followed by the current date, e.g. 'sample160628.zip'.
   echo If the current time is before 5 AM, the 'current' date is the day before
   echo the actual date.
   echo    Usage: ZIPTREE [/D [d[/m[/yy]]]]
   echo The optional parameter '/D' includes only files modified on or after the
   echo specified date. If the year, month, and day are not specified, they default
   echo to the 'current' values.
   exit /b 0
   )

:: Check for exclusion file.
echo.
set "pdir=%~dp0"
set "exclusionfile=%pdir%ziptree.exc"
if not exist %exclusionfile% goto noexc

:: Get current subfolder.
call currentpath_ dummy subfolder

:: Get current date.
call today_ year month day
set year2=%year:~-2%

set "zipfile=%subfolder%%year2%%month%%day%.zip"

:: Check for '/d'.
set "p1_=%~1"
if "!p1_!" equ "" goto zip
if /i "!p1_:~0,2!" neq "/d" goto invpar1

:: Get 'from' date.
set "p2_=%~2"
call checkdate_ p2_ year year2 month day || goto usage
set "from=-t%year%-%month%-%day%"

:: Zip files.
:zip
if '%from%' equ '' (echo Zipping all files.) else (
   echo Zipping files modified on or after %day%/%month%/%year2%.)
echo ----------------------------------------------------------------------
if exist "%zipfile%" del "%zipfile%" > nul
zip "%zipfile%" -Do * de\* en\* es\* fr\* %from% -x@%exclusionFile% || goto usage

goto finish

:noexc
echo Exclusions file '%exclusionfile%' not found.
goto abort

:invpar1
echo Invalid parameter: !p1_!
goto usage

:invpar2
echo Invalid parameter: !p2_!

:usage
echo.
echo Enter 'zipdir /?' for help.
echo.

:abort
echo Backup aborted.
exit /b 1

:finish
echo ----------------------------------------------------------------------
call showFileNameSize_ "%zipfile%"

exit /b
