HTML from DOS batch

Instead of echoing plain text to an output file, echo HTML code which can be loaded into a Web page on the fly.

This tip was submitted to the SearchWin2000.com tip exchange by member Mac McNeil. Please let other users know how useful it is by rating it below.


Do you have any DOS batch jobs that run on a regular basis, maybe for server monitoring or performing housekeeping duties? Do you need to know the realtime status of jobs when they've completed or, even better, while they're still running? Sure you do!

The standard method of accomplishing this would be to redirect an ECHO command to a file that contains the status, as in:

ECHO job finished with no errors at %date% %time% >> Status.txt

Wouldn't it be useful if you could view such a status on a Web page? Well, you can. Instead of echoing plain text to an output file, echo HTML code which can be loaded into a Web page on the fly. All you need to know is a few tips and tricks:

  • Remember that ECHO some text >status.htm initiates the output file and ECHO some text >>status.htm appends to it on a new line.

  • Generate a dummy page in your favorite HTML editor to get the look you want, then study the generated HTML code. That's what you want to replicate using the ECHO command in DOS batch.

    o Use three files -- a header, a footer and the data -- and then combine them when the final HTML file is to be created. This allows simple creation of color schemes, text formatting, etc.

  • To generate a > or a <, precede the charater with a carat as in ^> echo ^<p^>^<font face="Comic Sans MS" size="3"^>No errors detected since the job started.^</font^>^</p^> >>status.htm

  • The tilde character truncates output as in %date:~0,9% %time:~0,5% truncates to "ddd dd/mm hh:mm."

    When specifying a percentage as in <td width="33%">, you must enter two %% -- e.g. ^<td width="33%%"^>

    Example code

    
    
    set weblog = "c:datastatus.htm"
    set bggrn="#00FF00"
    set txtgrn="#000080"
    
    :: totcount, failcount and servcount are incremented elsewhere in the code
    
    echo ^<html^> >%weblog%
    echo ^<head^> >>%weblog%
    echo ^<meta http-equiv="refresh" content="15"^> >>%weblog%
    echo ^<title^>Final Status^</title^> >>%weblog%
    echo ^</head^> >>%weblog%
    echo ^<body bgcolor=%bggrn% text=%txtgrn%^> >>%weblog%
    echo ^<p^>^<font face="Comic Sans MS" size="3"^>No incidents
    detected on the most recent pass of job ^<u^>%webname%^</u^>^</font^>^<br^> >>%weblog%
    echo ^<font face="Comic Sans MS" size="2"^>Checked %servcount% 
    
    Servers in this pass which completed on: %date% at %time%.^<br^> >>%
    weblog% 
    echo 
    
    Since %sdate% at %stime% we have checked %totcount% servers and detected %
    failcount% failures.^</font^>^</p^> >>%weblog% 
    
    echo ^</body^> >>%weblog% 
    echo ^</html^> >>%weblog%
    

  • This was first published in April 2003

    Dig deeper on Windows Operating System Management

    Pro+

    Features

    Enjoy the benefits of Pro+ membership, learn more and join.

    0 comments

    Oldest 

    Forgot Password?

    No problem! Submit your e-mail address below. We'll send you an email containing your password.

    Your password has been sent to:

    -ADS BY GOOGLE

    SearchServerVirtualization

    SearchCloudComputing

    SearchExchange

    SearchSQLServer

    SearchWinIT

    SearchEnterpriseDesktop

    SearchVirtualDesktop

    Close