1. An example of building HTML out of a batch file
2. Because I like tweaking and improving and would like some input on how I could improve the script as a whole. The script runs fine and after going through somewhere around 2000 files it only takes about 10-15 minutes usually on an old XP box, but I know it could probably look cleaner and be more command efficient. It was written before I had much experience with delayed expansion and long before I found this site.
What the script does:
We generate reports that are e-mailed out via custom application and then stored in a \sent folder. They pile up after a while, but occasionally for trouble-shooting purposes we have to go back a few days and verify that they were sent out or at least made it this far.
The script keeps the last 7 days worth of reports in dated sub-folders and cleans out the oldest. When it puts yesterday's files in yesterday's folder, it also goes through all of the files and builds an HTML index of the files it puts there. The e-mail address is always the 1st line of the file, the report type is always the second line of the file.
WORKDIR = The directory the files originate in.
The index reads like this:
blahblahblah.txt Report_Type E-mail_address@itwassenthere.com
The txt file is a link to the text file that opens it up in IE.
Code: Select all
@ECHO OFF
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::.
:: ArchiveMail.BAT
::
:: Batch file to archive and delete E-mails Daily
:: Keeps NUMBDAYS worth of Files in dated folders
:: And deletes the Oldest Folder after NUMBDAYS
::
:: Also produces an HTML Index of files e-mailed
:: Lists filename, email address and report type
::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::.
SETLOCAL
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::: Configurable Options ::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Working Directory:
SET WORKDIR=C:\Test\mail
:: Number of Days to Keep Files:
SET NUMBDAYS=7
::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::: Main Program ::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::
CD "%WORKDIR%"
ECHO Executing Archive.Bat
ECHO Parsing Today's Date
FOR /F "tokens=1 delims= " %%A IN ('DATE/T') DO SET WEEKDAY=%%A
SET MONTH=%DATE:~4,2%
SET YEAR=%DATE:~10,4%
SET DAY=%DATE:~7,2%
:: Used for Testing::
::SET MONTH=04
::SET YEAR=2011
::SET DAY=10
:MainLoop
ECHO Today's Date is %MONTH%/%DAY%/%YEAR%
ECHO %DATE%
CALL :GetYesterday
SET FILEDATE=%MONTH%/%DAY%/%YEAR%
ECHO Preparing to Copy Files Dated %FILEDATE%
SET NEWDIR=%MONTH%-%DAY%-%YEAR%
ECHO Creating New Archive Folder
MKDIR "%NEWDIR%"
FOR /F "tokens=5 delims= " %%A IN ('DIR /ON ^|FIND /V "DIR" ^|FIND "%FILEDATE%"') DO CALL :MoveFiles %%A
CALL :BackTrack
ECHO Preparing to Delete Folder %OLDDIR%
SET OLDDIR=%MONTH%-%DAY%-%YEAR%
CALL :Verify
RD /S /Q %OLDDIR%
ECHO Finished Processing Files for %FILEDATE%
ECHO.
ECHO Processing Index
CD %WORKDIR%\%NEWDIR%
CALL :BuildIndex
ENDLOCAL
GOTO:EOF
:::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::: Subroutines ::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::: BuildIndex - Sets up an index of E-mails sent
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:BuildIndex
::Building HTML File Header Information
SET ROWCOLOR=ffffff
SET FILENUMBER=0
ECHO Building HTML Header
ECHO.
ECHO. > "%WORKDIR%\%NEWDIR%\index.htm"
ECHO ^<html style="color:#202020; font-weight:normal; font-size: 12; font-family:Arial;"^> >> "%WORKDIR%\%NEWDIR%\index.htm"
ECHO ^<head^> >> "%WORKDIR%\%NEWDIR%\index.htm"
ECHO ^<title^>Email Index^</title^> >> "%WORKDIR%\%NEWDIR%\index.htm"
ECHO ^</head^> >> "%WORKDIR%\%NEWDIR%\index.htm"
ECHO ^<body^> >> "%WORKDIR%\%NEWDIR%\index.htm"
ECHO ^<table width=100%%^> >> "%WORKDIR%\%NEWDIR%\index.htm"
ECHO ^<tr bgcolor=#e0e0e0^> >> "%WORKDIR%\%NEWDIR%\index.htm"
ECHO ^<td width=100%% style="border-bottom: #e34147 1px solid;"^> >> "%WORKDIR%\%NEWDIR%\index.htm"
ECHO ^<center^> >> "%WORKDIR%\%NEWDIR%\index.htm"
ECHO ^<b^>^ Email Index %NEWDIR% -^</b^> >> "%WORKDIR%\%NEWDIR%\index.htm"
ECHO ^<b^>^ HTML Generated by ArchiveMail.bat^</b^> >> "%WORKDIR%\%NEWDIR%\index.htm"
ECHO ^</center^> >> "%WORKDIR%\%NEWDIR%\index.htm"
ECHO ^</td^> ^</tr^> ^</table^> ^<p^> ^<center^> >> "%WORKDIR%\%NEWDIR%\index.htm"
ECHO ^<table border=0 cellspacing=0^> >> "%WORKDIR%\%NEWDIR%\index.htm"
ECHO ^<tr bgcolor=#426792^> >> "%WORKDIR%\%NEWDIR%\index.htm"
ECHO ^<td width=5^>^ ^</td^> >> "%WORKDIR%\%NEWDIR%\index.htm"
ECHO ^<td^>^<font color=#ffffff^>^<b^>Report Name^</b^>^</font^>^</td^> >> "%WORKDIR%\%NEWDIR%\index.htm"
ECHO ^<td^>^<font color=#ffffff^>^<b^>Report Type^</b^>^</font^>^</td^> >> "%WORKDIR%\%NEWDIR%\index.htm"
ECHO ^<td^>^<font color=#ffffff^>^<b^>Email Address^</b^>^</font^>^</td^> >> "%WORKDIR%\%NEWDIR%\index.htm"
ECHO ^</tr^> >> "%WORKDIR%\%NEWDIR%\index.htm"
ECHO Processing Index Table FOR each File
:: Parsing Table Information
FOR /F "tokens=5 delims= " %%A IN ('DIR /ON ^|FIND "TXT"') DO CALL :Index1 %%A
:: Building HTML Footer
ECHO Building HTML Footer
ECHO.
ECHO ^</table^> ^</center^> ^<tr^> >> "%WORKDIR%\%NEWDIR%\index.htm"
ECHO ^<td valign=bottom bgcolor=#ffffff^> >> "%WORKDIR%\%NEWDIR%\index.htm"
ECHO ^<table border=0 width=100%% cellpadding=0 cellspacing=0^> >> "%WORKDIR%\%NEWDIR%\index.htm"
ECHO ^<tr^> >> "%WORKDIR%\%NEWDIR%\index.htm"
ECHO ^<td bgcolor=#123759 height=40^> >> "%WORKDIR%\%NEWDIR%\index.htm"
ECHO ^<table border=0 width=100%% cellpadding=0 cellspacing=0^> >> "%WORKDIR%\%NEWDIR%\index.htm"
ECHO ^<tr^> >> "%WORKDIR%\%NEWDIR%\index.htm"
ECHO ^<td width=10^>^ ^</td^> >> "%WORKDIR%\%NEWDIR%\index.htm"
ECHO ^<td valign=center^>^<font color=#ffffff^>^<b^>Email Index^</b^>^</font^>^</td^> >> "%WORKDIR%\%NEWDIR%\index.htm"
ECHO ^<td align=right^>^<font color=#ffffff^>^© Site_Name, Inc.^</font^>^<br^> >> "%WORKDIR%\%NEWDIR%\index.htm"
ECHO ^<td width=10^>^ ^</td^> >> "%WORKDIR%\%NEWDIR%\index.htm"
ECHO ^</tr^> ^</table^> ^</td^> ^</tr^> ^</table^> ^</td^> ^</tr^> ^</table^> >> "%WORKDIR%\%NEWDIR%\index.htm"
ECHO ^</body^> >> "%WORKDIR%\%NEWDIR%\index.htm"
ECHO ^</html^> >> "%WORKDIR%\%NEWDIR%\index.htm"
ECHO.
GOTO:EOF
::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::: Subroutine - Verify displays all used Variables
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:Verify
ECHO.
ECHO ...........Variables
ECHO ....................
ECHO DATE=%DATE%
ECHO MONTH=%MONTH%
ECHO YEAR=%YEAR%
ECHO DAY=%DAY%
ECHO WEEKDAY=%WEEKDAY%
ECHO WORKDIR=%WORKDIR%
ECHO NUMBDAYS=%NUMBDAYS%
ECHO FILEDATE=%FILEDATE%
ECHO NEWDIR=%NEWDIR%
ECHO OLDDIR=%OLDDIR%
ECHO LOGFIL=%LOGFILE%
ECHO ....................
ECHO ...........Variables
ECHO.
GOTO:EOF
::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::: Subroutine - MoveFiles Copies and Deletes files
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:MoveFiles
ECHO Copying File %1
COPY /Y %1 "%WORKDIR%\%NEWDIR%\%1"
IF EXIST "%WORKDIR%\%NEWDIR%\%1" DEL /F /Q "%WORKDIR%\%1"
ECHO File %1 Moved to %WORKDIR%\%NEWDIR%\
GOTO:EOF
::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::: Subroutine - BackTrack Rewind Date to NUMBDAYS
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:BackTrack
(
SET /A NUMBDAYS=%NUMBDAYS% - 1
CALL :GetYesterday
)
IF %NUMBDAYS% GTR 0 GOTO:BackTrack
GOTO:EOF
::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::: Index1 - Getting filename & E-mail Address
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:Index1
:: Index Builder Section 1 - Getting E-mail Address
:: Building the table and adding all of the wave files to the table
FOR /F "delims= " %%A IN (%1) DO (
CALL :Index2 %1 %%A
EXIT /b
)
GOTO:EOF
:: Index Builder Section 2 - Getting Report Type / Subject
:Index2
FOR /F "skip=1 delims= " %%A IN (%1) DO (
CALL :Index3 %1 %2 %%A
EXIT /b
)
GOTO:EOF
:: Index Builder Section 3 - Putting it All Together
:Index3
::ECHO %1 %2 %3
SET /A FILENUMBER=%FILENUMBER% + 1
IF %ROWCOLOR%==ffffff (SET ROWCOLOR=eef2fe) ELSE (SET ROWCOLOR=ffffff)
::ECHO INDEXING FILE: %FILENUMBER%
ECHO ^<tr bgcolor=#%ROWCOLOR%^>^<td^>^</td^>^<td^>^<a href="%1"^>%1^</a^>^</td^> >> "%WORKDIR%\%NEWDIR%\index.htm"
ECHO ^<td^>%3^</td^>^<td^>%2^</td^>^</tr^> >> "%WORKDIR%\%NEWDIR%\index.htm"
GOTO:EOF
::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::: Subroutine - GetYesterday Sets Yesterday's Day
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:GetYesterday
SET DayS=%DAY%
IF %DAY:~0,1%==0 SET DayS=%DAY:~1%
IF %DayS% EQU 1 (
CALL :RollMonth
) ELSE (
SET /A DAY=%DayS% - 1
)
IF %DAY% LSS 10 SET DAY=0%DAY%
GOTO:EOF
::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::: Subroutine - RollMonth Gets Last Month
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:RollMonth
IF %MONTH%==01 (
SET DAY=31
SET YESTERM=12
SET /A THISYEAR = %YEAR% - 1
) ELSE (
IF %MONTH%==03 (
SET DAY=28
CALL :LeapYear
) ELSE (
IF %MONTH%==02 SET DAY=31
IF %MONTH%==04 SET DAY=31
IF %MONTH%==05 SET DAY=30
IF %MONTH%==06 SET DAY=31
IF %MONTH%==07 SET DAY=30
IF %MONTH%==08 SET DAY=31
IF %MONTH%==09 SET DAY=31
IF %MONTH%==10 SET DAY=30
IF %MONTH%==11 SET DAY=31
IF %MONTH%==12 SET DAY=30
)
SET THISYEAR=%YEAR%
IF %MONTH:~0,1%==0 SET MONTH=%MONTH:~1%
)
IF %THISYEAR%==%YEAR% (SET /A YESTERM=%MONTH% - 1) ELSE SET YEAR=%THISYEAR%
IF %YESTERM% LSS 10 SET YESTERM=0%YESTERM%
SET MONTH=%YESTERM%
GOTO:EOF
::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::: Subroutine - LeapYear Calculates Leapyear for Feb
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:LeapYear
:: If the year is divisible by 4 then it is a leapyear
SET /A LeapYear = %YEAR% / 4
SET /A LeapYear = %LeapYear% * 4
IF %LeapYear% EQU %YEAR% SET DAY=29
:: Unless the year is also divisible by 100
SET /A LeapYear = %YEAR% / 100
SET /A LeapYear = %LeapYear% * 100
IF %LeapYear% EQU %YEAR% SET DAY=28
:: Except when it is also divisible by 400
SET /A LeapYear = %YEAR% / 400
SET /A LeapYear = %LeapYear% * 400
IF %LeapYear% EQU %YEAR% SET DAY=29
GOTO:EOF
::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::