A better way to build an HTML Index of files?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Acy Forsythe
Posts: 126
Joined: 10 Jun 2011 10:30

A better way to build an HTML Index of files?

#1 Post by Acy Forsythe » 13 Jul 2011 11:00

I decided to post this for two reasons...

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^>^&nbsp;Email Index %NEWDIR% -^</b^> >> "%WORKDIR%\%NEWDIR%\index.htm"
ECHO ^<b^>^&nbsp;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^>^&nbsp;^</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^>^&nbsp;^</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^>^&copy; Site_Name, Inc.^</font^>^<br^> >> "%WORKDIR%\%NEWDIR%\index.htm"
ECHO ^<td width=10^>^&nbsp;^</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
::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::

jeb
Expert
Posts: 1041
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: A better way to build an HTML Index of files?

#2 Post by jeb » 13 Jul 2011 18:42

Hi Acy,

I would simplify the creation of your html output.

Code: Select all

:: 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 ^</body^> >> "%WORKDIR%\%NEWDIR%\index.htm"
ECHO ^</html^> >> "%WORKDIR%\%NEWDIR%\index.htm"

It's a bit tricky to read it with the carets.
And the >> "%WORKDIR%\%NEWDIR%\index.htm" after each line is also a bit too much

Code: Select all

@echo off
setlocal enableDelayedExpansion
set LF=^


cls
(
   for /F "tokens=*" %%a in ("!lf!
   "</table> </center> <tr>"!lf!
   "<td valign=bottom bgcolor=#ffffff>"!lf!
   "<table border=0 width=100%% cellpadding=0 cellspacing=0>"!lf!
   "<tr>"!lf!
   "<td bgcolor=#123759 height=40>"!lf!
   "<table border=0 width=100%% cellpadding=0 cellspacing=0>"!lf!
   "<tr>"!lf!
   "<td width=10>&nbsp;</td>"!lf!
   "<td valign=center><font color=#ffffff><b>Email Index</b></font></td>"!lf!
   "<td align=right><font color=#ffffff>&copy; Site_Name, Inc.</font><br>"!lf!
   "<td width=10>&nbsp;</td>"!lf!
   "</tr> </table> </td> </tr> </table> </td> </tr> </table>"!lf!
   "</body>"!lf!
   ""!lf!
   "</html>"
   ) DO (
      echo(%%~a
   )
) > "%WORKDIR%\%NEWDIR%\index.htm"


jeb

Acy Forsythe
Posts: 126
Joined: 10 Jun 2011 10:30

Re: A better way to build an HTML Index of files?

#3 Post by Acy Forsythe » 13 Jul 2011 20:02

Thanks Jeb...

Question though...

1. Why don't you have to escape the <> in the for loop?

EDIT: Nevermind I figured it out almost as soon as I hit submit... They are surrounded by quotes and the %%~a is then removing the quotes...

There is an un-even number of quotes, why is the 1st one necessary?

jeb
Expert
Posts: 1041
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: A better way to build an HTML Index of files?

#4 Post by jeb » 14 Jul 2011 05:28

The first quote is necessary, as it marks the begin of the complete string.
The FOR-Loop expect one string to process here, I only create this string over multiple lines.
Normally at last there should also be a single quote to balance it, but this causes only problems so I ommit it.

Btw. It was a bad idea of me to use EnableDelayedExpansion as then all exclamation marks will make trouble.

But you could change it to

Code: Select all

@echo off
setlocal DisableDelayedExpansion
set LF=^


set NL=^^^%LF%%LF%^%LF%%LF%
(
   for /F "tokens=*" %%a in ("
   "</table> </center> <tr>"%NL%
   "!!!!"%NL%
   "</body>"%NL%
   "</html>"
   ) DO (
      echo(%%~a
   )
)



jeb

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: A better way to build an HTML Index of files?

#5 Post by dbenham » 14 Jul 2011 12:46

If the HTML can follow these constraints:
- No double quote ", question mark ?, or asterisk * in the HTML elements (use single quote ' for element attributes instead)
- Use &quot; &#42; &#63; for double quote, asterisk and question mark in content

Then the following syntax is even easier (I think Acy's index page meets the above requirements):

Code: Select all

@echo off
cls
set outfile="%WORKDIR%\%NEWDIR%\index.htm"
if exist %outfile% del %outfile%
for %%a in (
  "</table> </center> <tr>"
  "<td valign=bottom bgcolor=#ffffff>"
  "<table border=0 width=100%% cellpadding=0 cellspacing=0>"
  "<tr>"
  "<td bgcolor=#123759 height=40>"
  "<table border=0 width=100%% cellpadding=0 cellspacing=0>"
  "<tr>"
  "<td width=10>&nbsp;</td>"
  "<td valign=center><font color=#ffffff><b>Email Index</b></font></td>"
  "<td align=right><font color=#ffffff>&copy; Site_Name, Inc.</font><br>"
  "<td width=10>&nbsp;</td>"
  "</tr> </table> </td> </tr> </table> </td> </tr> </table>"
  "</body>"
  ""
  "</html>"
) do echo(%%~a>>%outfile%


But for unconstrained output - I like your syntax jeb :!: :D

I suspect I may use it a lot for my own code. I tried to do something similar before but never had success. Up until now I had resigned myself to storing the multiple lines in a single variable and using delayed expansion or else using a simple FOR with the limitation of no * or ? support. Your solution is more straight forward.

Figuring out where to put quotes and what to escape can be tricky. I think the following helps make things clear:

Code: Select all

@echo off
setlocal DisableDelayedExpansion
set LF=^


set NL=^^^%LF%%LF%^%LF%%LF%
for /F "tokens=*" %%a in ("
  The leading unbalanced quote on the FOR line is required and stripped%NL%
  Quotes are not needed if there are no special characters%NL%
  Unquoted/unescaped token delimiters   ,,,;;;=== are compressed to a single space and can cause XP bug%NL%
  "Token delimiters are preserved if quoted    ,,,;;;==="%NL%
  And delimiters also preserved if escaped ^ ^ ^ ^,^,^,^;^;^;^=^=^=%NL%
  "Special characters must be quoted: &<>|)^"%NL%
  Or special chars must be escaped: ^&^<^>^|^)^^%NL%
  He said^, "Hello & Goodbye!" (Non-leading^, balanced quotes option 1^)%NL%
  "He said, "Hello ^& Goodbye!" (Non-leading, balanced quotes option 2)"%NL%
  ""Hello ^& Goodbye!", he said. (Leading, balanced quotes option 1)"%NL%
  ^""Hello & Goodbye!"^, he said. (Leading^, balanced quotes option 2^)^"%NL%
  Non-leading^, unbalanced quote ^" (option 1^)%NL%
  ^"Non-leading^, unbalanced quote " (option 2)"%NL%
  "Non-leading, unbalanced quote " (option 3^)^"%NL%
  "" Leading^, unbalanced quote (option 1^)^"%NL%
  ^"" Leading, unbalanced quote (option 2)"%NL%
  Trailing unbalanced quote on final line is optional and stripped if present"
) DO echo(%%~a

Output:

Code: Select all

The leading unbalanced quote on the FOR line is required and stripped
Quotes are not needed if there are no special characters
Unquoted/unescaped token delimiters are compressed to a single space and can cause XP bug
Token delimiters are preserved if quoted    ,,,;;;===
And delimiters also preserved if escaped    ,,,;;;===
Special characters must be quoted: &<>|)^
Or special chars must be escaped: &<>|)^
He said, "Hello & Goodbye!" (Non-leading, balanced quotes option 1)
He said, "Hello & Goodbye!" (Non-leading, balanced quotes option 2)
"Hello & Goodbye!", he said. (Leading, balanced quotes option 1)
"Hello & Goodbye!", he said. (Leading, balanced quotes option 2)
Non-leading, unbalanced quote " (option 1)
Non-leading, unbalanced quote " (option 2)
Non-leading, unbalanced quote " (option 3)
" Leading, unbalanced quote (option 1)
" Leading, unbalanced quote (option 2)
Trailing unbalanced quote on final line is optional and stripped if present

Info on the XP FOR /F bug can be found here.

Dave Benham

Acy Forsythe
Posts: 126
Joined: 10 Jun 2011 10:30

Re: A better way to build an HTML Index of files?

#6 Post by Acy Forsythe » 15 Jul 2011 12:57

Thanks guys, it definitely cleaned up that section. Doesn't seem to be much slower/faster on test data but it is MUCH easier to read and edit now.

So now the HTML section has been re-done and I used some other tips I found here to change the way the table-data worked, but do you guys have any suggestions on a cleaner way of doing that without breaking out of for-loops with CALL next for loop?

Those are the Index1 - Index3 sections. Previously up until a month or so ago, I was only logging the filename's in the table as links. I learned some new tricks :) and applied them to get the report type and e-mail address, but still don't like the way it looks/works, except that it does work :)

Acy Forsythe
Posts: 126
Joined: 10 Jun 2011 10:30

Re: A better way to build an HTML Index of files?

#7 Post by Acy Forsythe » 15 Aug 2011 15:32

Posting the code here, because I am about to reference it in another thread...

Code: Select all

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
%== Building HTML Header Information ==%
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
SET ROWCOLOR=ffffff
SET FILENUMBER=0
ECHO: > "%WORKDIR%\%NEWDIR%\index.htm"
(
    FOR /F "tokens=*" %%a in ("
    "<html style="color:#202020; font-weight:normal; font-size: 12; font-family:Arial;">"%NL%
    "<head>"%NL%
    "<title>Email Index</title>"%NL%
    "</head>"%NL%
    "<body>"%NL%
    "<table width=100%%>"%NL%
    "<tr bgcolor=#e0e0e0>"%NL%
    "<td width=100%% style="border-bottom: #e34147 1px solid;">"%NL%
    "<center>"%NL%
    "<b>&nbsp;Auto Email Index %NEWDIR% -</b>"%NL%
    "<b>&nbsp;HTML Generated by ArchiveMail.bat</b>"%NL%
    "</center>"%NL%
    "</td> </tr> </table> <p> <center>"%NL%
    "<table border=0 cellspacing=0>"%NL%
    "<tr bgcolor=#426792>"%NL%
    "<td width=5>&nbsp;</td>"%NL%
    "<td><font color=#ffffff><b>Report Name</b></font></td>"%NL%
    "<td><font color=#ffffff><b>Report Type</b></font></td>"%NL%
    "<td><font color=#ffffff><b>Email Address</b></font></td>"%NL%
    "</tr>"
   ) DO (
    echo(%%~a
    )
) >> "%WORKDIR%\%NEWDIR%\index.htm"
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
%== Processing Each File for Indexing ==%
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
FOR /F "tokens=5 delims= " %%A IN ('DIR %WORKDIR%\%NEWDIR% /ON /A-D ^|FIND "%FILEDATE%"') DO (
  FOR /F "delims= " %%e IN (%WORKDIR%\%NEWDIR%\%%A) DO (IF "!EMAIL!"=="" SET EMAIL=%%e)
  FOR /F "skip=1 delims= " %%r IN (%WORKDIR%\%NEWDIR%\%%A) DO (IF "!RPT!"=="" SET RPT=%%r)
  IF !ROWCOLOR!==ffffff (SET ROWCOLOR=eef2fe) ELSE (SET ROWCOLOR=ffffff)
  ECHO ^<tr bgcolor=#!ROWCOLOR!^>^<td^>^</td^>^<td^>^<a href="%%A"^>%%A^</a^>^</td^> >> "%WORKDIR%\%NEWDIR%\index.htm"
  ECHO ^<td^>!RPT!^</td^>^<td^>!EMAIL!^</td^>^</tr^> >> "%WORKDIR%\%NEWDIR%\index.htm"
  SET EMAIL=
  SET RPT=
)
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
%== Building HTML Footer Information ==%
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
(
    FOR /F "tokens=*" %%a in ("
    "</table> </center> <tr>"%NL%
    "<td valign=bottom bgcolor=#ffffff>"%NL%
    "<table border=0 width=100%% cellpadding=0 cellspacing=0>"%NL%
    "<tr>"%NL%
    "<td bgcolor=#123759 height=40>"%NL%
    "<table border=0 width=100%% cellpadding=0 cellspacing=0>"%NL%
    "<tr>"%NL%
    "<td width=10>&nbsp;</td>"%NL%
    "<td valign=center><font color=#ffffff><b>Auto Email Index</b></font></td>"%NL%
    "<td align=right><font color=#ffffff>&copy; Company, Inc.</font><br>"%NL%
    "<td width=10>&nbsp;</td>"%NL%
    "</tr> </table> </td> </tr> </table> </td> </tr> </table>"%NL%
    "</body>"%NL%
    "</html>"
    ) DO (
    echo(%%~a
    )
) >> "%WORKDIR%\%NEWDIR%\index.htm"



But while I am posting here, I'd like to thank Jeb and Dave again for all of their help. I cleaned the whole script up, got rid of all of the function calls, and switched to using the Jdate formulas for adding and subtracting dates as opposed to the very much out-dated Gregorian backtracking.

Also, my solution to the nested indexing calls and having to break out of FOR loops is up there, that took hours and hours to figure out, but in the end, not breaking out of the loops and removing the calls saved more time than parsing through the entire files added.

Now, like many others I'm stuck because the above Header/Footer sections are generating the XP FOR /F Bug. And it's frustrating because I spent a lot of time improving this script in both style and performance and it's blazing fast and sleek looking, and 62 non-blank lines shorter. But... It generates that "The system could not find the file..." error

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: A better way to build an HTML Index of files?

#8 Post by dbenham » 15 Aug 2011 16:28

Your code has two situations where you have unquoted/unescaped token delimiters that are causing the appearance of the XP FOR /F bug.

1) Leading spaces before each quoted string could be causing problems.
- You could indent with only one space. This will work because the %NL% macro will escape the leading space character on the next line. You don't want the lines to start with the quote because then the quote will be escaped and that will lead to complications. (Note that indenting by a single space will not show up when posted to this forum)
- You could also try Ed's trick of replacing the leading spaces with pseudo comments like

Code: Select all

%    %"String goes here"
The %NL% macro will escape the %, which doesn't change its behavior. The % % will be expanded to nothing, so the FOR /F will never see the spaces.

2) The style definitions are not quoted because you have enclosed them in quotes :!:
The quotes are effectively unquoting the style definition because the entire string is already in quotes. This is easily solved by enclosing the style definition by single quotes instead (perfectly valid in HTML):

Code: Select all

%  %"<html style='color:#202020; font-weight:normal; font-size: 12; font-family:Arial;'>"%NL%


But I think the whole XP FOR bug is easier to avoid if you simply follow my earlier suggestion of using a simple FOR loop instead of the FOR /F loop. (You still need to use single quotes for your style definitions)

This should work for the header (untested):

Code: Select all

(
  FOR %%a in ("
    "<html style='color:#202020; font-weight:normal; font-size: 12; font-family:Arial;'>"
    "<head>"
    "<title>Email Index</title>"
    "</head>"
    "<body>"
    "<table width=100%%>"
    "<tr bgcolor=#e0e0e0>"
    "<td width=100%% style='border-bottom: #e34147 1px solid;'>"
    "<center>"
    "<b>&nbsp;Auto Email Index %NEWDIR% -</b>"
    "<b>&nbsp;HTML Generated by ArchiveMail.bat</b>"
    "</center>"
    "</td> </tr> </table> <p> <center>"
    "<table border=0 cellspacing=0>"
    "<tr bgcolor=#426792>"
    "<td width=5>&nbsp;</td>"
    "<td><font color=#ffffff><b>Report Name</b></font></td>"
    "<td><font color=#ffffff><b>Report Type</b></font></td>"
    "<td><font color=#ffffff><b>Email Address</b></font></td>"
    "</tr>"
  ) DO echo(%%~a
) >> "%WORKDIR%\%NEWDIR%\index.htm"
As long as there are no * or ? in the HTML then you should be fine with the simple FOR loop.

--------------------
On an unrelated note - I think your "Processing Each File for Indexing " section can be made faster by moving the append redirection outside of the FOR loop:

(untested)

Code: Select all

(
  FOR /F "tokens=5 delims= " %%A IN ('DIR %WORKDIR%\%NEWDIR% /ON /A-D ^|FIND "%FILEDATE%"') DO (
    FOR /F "delims= " %%e IN (%WORKDIR%\%NEWDIR%\%%A) DO (IF "!EMAIL!"=="" SET EMAIL=%%e)
    FOR /F "skip=1 delims= " %%r IN (%WORKDIR%\%NEWDIR%\%%A) DO (IF "!RPT!"=="" SET RPT=%%r)
    IF !ROWCOLOR!==ffffff (SET ROWCOLOR=eef2fe) ELSE (SET ROWCOLOR=ffffff)
    ECHO ^<tr bgcolor=#!ROWCOLOR!^>^<td^>^</td^>^<td^>^<a href="%%A"^>%%A^</a^>^</td^>
    ECHO ^<td^>!RPT!^</td^>^<td^>!EMAIL!^</td^>^</tr^>
    SET EMAIL=
    SET RPT=
  )
)>>"%WORKDIR%\%NEWDIR%\index.htm"


Dave Benham
Last edited by dbenham on 15 Aug 2011 17:01, edited 2 times in total.

Acy Forsythe
Posts: 126
Joined: 10 Jun 2011 10:30

Re: A better way to build an HTML Index of files?

#9 Post by Acy Forsythe » 15 Aug 2011 16:41

I thought I did use your example... But I used Jebs incase I needed support for ? and *. I will try the % % trick and replace the style quotes with singles and see if that resolves the problem and if not, I will go with the FOR loop.

I thought you could only use FOR with a file set and to utilize a string or command output etc... you had to use the /F option.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: A better way to build an HTML Index of files?

#10 Post by dbenham » 15 Aug 2011 16:57

Acy Forsythe wrote:I thought you could only use FOR with a file set and to utilize a string or command output etc... you had to use the /F option.
Command output - Yes, you need FOR /F
String processing - No, you can use FOR, unless you want to tokenize the string or if the string has * or ? wildcards - then you need FOR /F.

(see my edit in my prior post - you posted while I was editing)

Dave Benham

Acy Forsythe
Posts: 126
Joined: 10 Jun 2011 10:30

Re: A better way to build an HTML Index of files?

#11 Post by Acy Forsythe » 16 Aug 2011 07:21

What I found...

The % % trick should work... But I still encounter the bug.

Indenting to one space and using %NL% to escape it should work... But I still encounter the bug.

Skipping the /F all together and just using FOR %%a IN ( worked, but it hosed my header and footer...

EDIT: Fixed... I removed the unbalanced leading quote, it works without it, bypasses the bug, and generates proper HTML :)

Now I have to get back to adding HTA code and sorting to the whole thing :)

Here is the updated script for any interested, the original was posted in the first post and here is what it's evolved to from time spent on this forum:

Code: Select all

@ECHO OFF &Setlocal DisableDelayedExpansion
CLS
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
%== Setting up Configurable Options ==%
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
SET "WORKDIR=C:\test\mail"              %== Working Directory ==%
SET "NUMBDAYS=7"                        %== Number of days to keep archived files ==%
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
%== Setting LF ==%
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
SET LF=^


:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
%== Defining Macros ==%
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
SET ^"\n=^^^%LF%%LF%^%LF%%LF%^^"


SET NL=^^^%LF%%LF%^%LF%%LF%


SET macro_Call=FOR /f "tokens=1-26" %%a IN


SET macro.GetTime=DO (%\n%
  SetLocal EnableDelayedExpansion%\n%
  SET "t=0"%\n%
  FOR /F "tokens=1-4 delims=:." %%A IN ("!time: =0!") DO SET /a "t=(((1%%A*60)+1%%B)*60+1%%C)*100+1%%D-36610100"%\n%
  FOR %%v IN (!t!) DO ENDLOCAL^&IF "%%~a" neq "" (set "%%~a=%%v") ELSE ECHO:%%v%\n%
)


SET macro.DiffTime=DO (%\n%
  SetLocal EnableDelayedExpansion%\n%
  SET /a "DD=(%%~b)-(%%~a)"%\n%
  IF !DD! lss 0 SET /a "DD+=24*60*60*100"%\n%
  SET /a "HH=DD/360000, DD-=HH*360000, MM=DD/6000, DD-=MM*6000, SS=DD/100, DD-=SS*100"%\n%
  IF "!HH:~1!"=="" set "HH=0!HH!"%\n%
  IF "!MM:~1!"=="" set "MM=0!MM!"%\n%
  IF "!SS:~1!"=="" set "SS=0!SS!"%\n%
  IF "!DD:~1!"=="" set "DD=0!DD!"%\n%
  FOR %%v IN (!HH!:!MM!:!SS!.!DD!) DO ENDLOCAL^&IF "%%~c" neq "" (set "%%~c=%%v") ELSE ECHO:%%v%\n%
)


SET macro.Jdate=DO (%\n%
  SetLocal EnableDelayedExpansion%\n%
  SET DateStr=%%~a^&IF "%%~a"=="" set "DateStr=%DATE%"%\n%
  FOR /F "skip=1 tokens=2-4 delims=(-)" %%A in ('"echo.|date"') do (%\n%
    FOR /F "tokens=1-3 delims=/.- " %%G in ("!DateStr:* =!") do (%\n%
        SET %%A=%%G^&SET %%B=%%H^&SET %%C=%%I))%\n%
  SET /a "yy=10000!yy! %%10000,mm=100!mm! %% 100,dd=100!dd! %% 100"%\n%
  IF !yy! LSS 100 SET /a yy+=2000%\n%
  SET /a "JD=dd-32075+1461*(yy+4800+(mm-14)/12)/4+367*(mm-2-(mm-14)/12*12)/12-3*((yy+4900+(mm-14)/12)/100)/4"%\n%
  FOR %%v IN (!JD!) DO ENDLOCAL^&IF "%%~b" neq "" (set "%%~b=%%v") else echo %%v%\n%
)


SET macro.Jdate2Date=DO (%\n%
  SetLocal EnableDelayedExpansion%\n%
  SET /a "L=(%%~a)+68569,N=4*L/146097,L=L-(146097*N+3)/4,I=4000*(L+1)/1461001"%\n%
  SET /a "L=L-1461*I/4+31,J=80*L/2447,K=L-2447*J/80,L=J/11"%\n%
  SET /a "J=J+2-12*L,I=100*(N-49)+I+L"%\n%
  SET /a "YYYY=I,MM=100+J,DD=100+K"%\n%
  SET MM=!MM:~-2!%\n%
  SET DD=!DD:~-2!%\n%
  SET "DOUT=!MM!/!DD!/!YYYY!"%\n%
  FOR %%v IN (!DOUT!) DO ENDLOCAL^&IF "%%~b" neq "" (set "%%~b=%%v") else echo %%v%\n%
)


SetLocal EnableDelayedExpansion
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
%== Getting Start Time ==%
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
%macro_Call% ("Begin") %macro.getTime%
ECHO:
ECHO: ----- Beginning Report Time: %TIME% -----
ECHO:
ECHO: # Processing Data parameters / Initial Setup
ECHO:
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
%== Setting up Date Parameters ==%
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
SET MONTH=%DATE:~4,2%
SET YEAR=%DATE:~10,4%
SET DAY=%DATE:~7,2%
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
%== Temporary Date Parameters for Testing - Comment out or remove  ==%
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
SET MONTH=05
SET YEAR=2011
SET DAY=10

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
%== Get Yesterday's Date by converting to Julian Format ==%
%== Subtracting 1 and re-converting to Gregorian Format ==%
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
%macro_Call% ("%MONTH%/%DAY%/%YEAR% JD") %macro.Jdate%
SET /a JD-=1
%macro_Call% ("%JD% NEWDATE") %macro.Jdate2Date%

SET FILEDATE=%NEWDATE%
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
%== Getting Execution Time and Displaying ==%
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
%macro_Call% ("StepDone") %macro.getTime%
%macro_Call% ("Begin StepDone Difference") %macro.diffTime%
%macro_Call% ("StepStart") %macro.getTime%
ECHO: ----- Processing Finished in %Difference% -----
ECHO:
ECHO: # Preparing to Copy Files Dated %FILEDATE%
ECHO:
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
SET "MONTH=%NEWDATE:~0,2%"
SET "YEAR=%NEWDATE:~6,4%"
SET "DAY=%NEWDATE:~3,2%"
SET NEWDIR=%MONTH%-%DAY%-%YEAR%

MKDIR "%WORKDIR%\%NEWDIR%"

FOR /F "tokens=5 delims= " %%A IN ('DIR %WORKDIR% /ON /A-D ^|FIND "%FILEDATE%"') DO (
  COPY /Y "%WORKDIR%\%%A" "%WORKDIR%\%NEWDIR%\%%A" > NUL
  IF EXIST "%WORKDIR%\%NEWDIR%\%%A" DEL /F /Q "%WORKDIR%\%%A"
)
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
%== Getting Execution Time and Displaying ==%
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
%macro_Call% ("StepDone") %macro.getTime%
%macro_Call% ("StepStart StepDone Difference") %macro.diffTime%
%macro_Call% ("StepStart") %macro.getTime%
ECHO: ----- Processing Finished in %Difference% -----
ECHO:
ECHO: # Preparing to Delete oldest Archived Folder
ECHO:
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
FOR /L %%I IN (1,1,%NUMBDAYS%) DO (SET /a JD-=1)
%macro_Call% ("%JD% NEWDATE") %macro.Jdate2Date%
SET "MONTH=%NEWDATE:~0,2%"
SET "YEAR=%NEWDATE:~6,4%"
SET "DAY=%NEWDATE:~3,2%"
SET OLDDIR=%MONTH%-%DAY%-%YEAR%
RD /S /Q %WORKDIR%\%OLDDIR%
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
%== Getting Execution Time and Displaying ==%
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
%macro_Call% ("StepDone") %macro.getTime%
%macro_Call% ("StepStart StepDone Difference") %macro.diffTime%
%macro_Call% ("StepStart") %macro.getTime%
ECHO: ----- Processing Finished in %Difference% -----
ECHO:
ECHO: # Indexing Files
ECHO:
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
%== Building HTML Header Information ==%
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
SET ROWCOLOR=ffffff
SET FILENUMBER=0
ECHO: > "%WORKDIR%\%NEWDIR%\index.htm"
(
    FOR %%a in (
    "<html style='color:#202020; font-weight:normal; font-size: 12; font-family:Arial;'>"%NL%
    "<head>"%NL%
    "<title>Email Index</title>"%NL%
    "</head>"%NL%
    "<body>"%NL%
    "<table width=100%%>"%NL%
    "<tr bgcolor=#e0e0e0>"%NL%
    "<td width=100%% style='border-bottom: #e34147 1px solid;'>"%NL%
    "<center>"%NL%
    "<b>&nbsp;Email Index %NEWDIR% -</b>"%NL%
    "<b>&nbsp;HTML Generated by ArchiveMail.bat</b>"%NL%
    "</center>"%NL%
    "</td> </tr> </table> <p> <center>"%NL%
    "<table border=0 cellspacing=0>"%NL%
    "<tr bgcolor=#426792>"%NL%
    "<td width=5>&nbsp;</td>"%NL%
    "<td><font color=#ffffff><b>Report Name</b></font></td>"%NL%
    "<td><font color=#ffffff><b>Report Type</b></font></td>"%NL%
    "<td><font color=#ffffff><b>Email Address</b></font></td>"%NL%
    "</tr>"
   ) DO (
    ECHO(%%~a
    )
) >> "%WORKDIR%\%NEWDIR%\index.htm"
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
%== Processing Each File for Indexing ==%
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
(
  FOR /F "tokens=5 delims= " %%A IN ('DIR %WORKDIR%\%NEWDIR% /ON /A-D ^|FIND "%FILEDATE%"') DO (
    FOR /F "delims= " %%e IN (%WORKDIR%\%NEWDIR%\%%A) DO (IF "!EMAIL!"=="" SET EMAIL=%%e)
    FOR /F "skip=1 delims= " %%r IN (%WORKDIR%\%NEWDIR%\%%A) DO (IF "!RPT!"=="" SET RPT=%%r)
    IF !ROWCOLOR!==ffffff (SET ROWCOLOR=eef2fe) ELSE (SET ROWCOLOR=ffffff)
    ECHO: ^<tr bgcolor=#!ROWCOLOR!^>^<td^>^</td^>^<td^>^<a href="%%A"^>%%A^</a^>^</td^>
    ECHO: ^<td^>!RPT!^</td^>^<td^>!EMAIL!^</td^>^</tr^>
    SET EMAIL=
    SET RPT=
  )
) >> "%WORKDIR%\%NEWDIR%\index.htm"
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
%== Building HTML Footer Information ==%
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
(
 FOR %%a in (
    "</table> </center> <tr>"%NL%
    "<td valign=bottom bgcolor=#ffffff>"%NL%
    "<table border=0 width=100%% cellpadding=0 cellspacing=0>"%NL%
    "<tr>"%NL%
    "<td bgcolor=#123759 height=40>"%NL%
    "<table border=0 width=100%% cellpadding=0 cellspacing=0>"%NL%
    "<tr>"%NL%
    "<td width=10>&nbsp;</td>"%NL%
    "<td valign=center><font color=#ffffff><b>Email Index</b></font></td>"%NL%
    "<td align=right><font color=#ffffff>&copy; Company, Inc.</font><br>"%NL%
    "<td width=10>&nbsp;</td>"%NL%
    "</tr> </table> </td> </tr> </table> </td> </tr> </table>"%NL%
    "</body>"%NL%
    "</html>"
    ) DO (
    ECHO(%%~a
    )
) >> "%WORKDIR%\%NEWDIR%\index.htm"
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
%== Getting Execution Time and Displaying ==%
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
%macro_Call% ("StepDone") %macro.getTime%
%macro_Call% ("StepStart StepDone Difference") %macro.diffTime%
%macro_Call% ("StepStart") %macro.getTime%
ECHO: ----- Processing Finished in %Difference% -----
ECHO:
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
%== Getting endtime and difference ==%
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
%macro_Call% ("End") %macro.getTime%
%macro_Call% ("Begin End Difference") %macro.diffTime%
ECHO: ----------------------------------------------
ECHO: ----- Processes Finished At: %TIME% -----
ECHO: ----- Total Execution time:  %Difference% -----
ECHO: ----------------------------------------------
ECHO:
EndLocal &Exit /b


Timing Macro's are Dave's and so is the macro style used for the other Macros. Both scripts do the exact same thing, but you can really tell the diffference in Syntax and the new script is faster. It's more noticeably faster the more files you parse.

Essentially what the script does is take files (extension is irrelevant, but in my case they are text files) and moves files modified yesterday into a folder dated mm-dd-yyyy. It then goes back and deletes the oldest folder if it's old enough. %NUMBDAYS% specifies the retention.

The Indexing part in my case (and you need to know a little HTML to modify it) Builds a header and sets up a 3 column table alternating row colors between ffffff and eef2fe just for the sake of my eyes. The file processing get's the first two rows out of each file as well as the fully qualified path to the file. The footer section just closes any open HTML tags and adds a bottom row with company info.

What I end up with is each row containing a link to the text file for easy opening, the type of report that the text file contains and the e-mail address the report was sent to.

The same process can be used to catalog, index, and archive any types of files though. I've used it for wav files from voice recording software as well, but instead of opening the files and pulling out information, I used the timestamp from the modified date, linked to the file and just had two columns one that linked and played the wav file and the second column gave you the timestamp of the recording. The HTML section is the only thing that needs too be modified, file extension and file location are not relevant, just set the working directory to the location of the "dump site" and the script will create dated subfolders there.
Last edited by Acy Forsythe on 16 Aug 2011 09:29, edited 3 times in total.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: A better way to build an HTML Index of files?

#12 Post by dbenham » 16 Aug 2011 08:13

:shock: I'm surprised that any of the proposed solutions fail - but all of them :!: :?

Acy Forsythe wrote:The % % trick should work... But I still encounter the bug.

Indenting to one space and using %NL% to escape it should work... But I still encounter the bug.
Are you sure the bug is occurring in the header and or footer sections :?: Did you also fix the style definition? If so, can you post a complete working extract that demonstrates the bug? I'd very much like to see this solved because I thought I understood the parameters of the XP bug.

Acy Forsythe wrote:Skipping the /F all together and just using FOR %%a IN ( worked, but it hosed my header and footer...
Again - this is a surprise. Can you post a working extract of the code you used and post your results.

Dave Benham

Acy Forsythe
Posts: 126
Joined: 10 Jun 2011 10:30

Re: A better way to build an HTML Index of files?

#13 Post by Acy Forsythe » 16 Aug 2011 08:21

Dave, I edited my last post to post the full working version.

If you change the FOR %%a section back to what I posted yesterday and then apply the "fixes" you should see the same thing I saw.

I added % % prior to every line except the following:

Code: Select all

) DO (


And I am certain it was in my header and footer sections. I had an echo right before the header and right before the file processing section. I would get the error almost always right between the two echo and sometimes right before the "Processing Finished" message.

Also yes, I fixed the style line.


EDIT: And if you didn't like that answer... You're not going to like this one either:

I also tried removing the indentation so there are no leading spaces on any of the lines and I still got the bug. It's very reproduceable.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: A better way to build an HTML Index of files?

#14 Post by dbenham » 16 Aug 2011 09:18

Acy Forsythe wrote:EDIT: Fixed... I removed the unbalanced leading quote, it works without it, bypasses the bug, and generates proper HTML
Whew... that's a relief.
The unbalanced leading quote definitely needed to be removed - it was intended only for jeb's FOR /F syntax. The %NL% can be removed from the simple FOR solution - it doesn't hurt, but it doesn't serve any purpose and just clutters up the code.

I'll try to diagnose the XP bug tonight (or next few days any way)

-----------
You moved the append redirection outside the processing loop, but it is still getting executed once for each iteration which is slowing it down. For optimum performance you need one additional level of ():

Code: Select all

(
  FOR ... DO (
    blah blah blah
    echo blah blah blah
  )
)>>"%WORKDIR%\%NEWDIR%\index.htm"


Dave Benham

Acy Forsythe
Posts: 126
Joined: 10 Jun 2011 10:30

Re: A better way to build an HTML Index of files?

#15 Post by Acy Forsythe » 16 Aug 2011 09:30

Thanks again, I edited my code above with the changes :)

Post Reply