Deco Deco - 4 months ago 25
HTML Question

Windows batch file to find variable string in a html file

I am trying to write a Windows batch file that will look through a specific html file that looks something like this (simplified):

<input name="pattern" value="*.var" type="text" /><img style="width: 16px; height: 16px; vertical-align:middle; cursor:pointer" onclick="this.parentNode.submit()" class="icon-go-next icon-sm" src="/static/474743c8/images/16x16/go-next.png" /></form></div><table class="fileList"><tr><td><img style="width: 16px; height: 16px; " class="icon-text icon-sm" src="/static/474743c8/images/16x16/text.png" /></td><td><a href="./varapplication-webservice-05.05.07-SNAPSHOT.var">varapplication-webservice-05.05.07-SNAPSHOT.var</a></td><td class="fileSize">3.49 MB</td><td><a href="./varapplication-webservice-05.05.07-SNAPSHOT.var/*fingerprint*/"><img style="width: 16px; height: 16px; " class="icon-fingerprint icon-sm" src="/static/474743c8/images/16x16/fingerprint.png" /></a> <a href="./varapplication-webservice-05.05.07-SNAPSHOT.var/*view*/">view</a></td></tr><tr><td style="text-align:right;" colspan="3"><div style="margin-top: 1em;"><a href="./*.var/*zip*/target.zip"><img style="width: 16px; height: 16px; " class="icon-package icon-sm" src="/static/474743c8/images/16x16/package.png" />


and use the build version (e.g. 05.05.07-SNAPSHOT - next time will be another version but the format remain the same) as variable for another batch file.
I have tried with findstr but no success:

for /F "delims=" %%a in ('findstr /ic "webservice" a.html') do set "line=%%a"
set "line=%line:*webservice=%"
for /F "delims=" %%a in ("%line%") do set string=%%a
for %%b in ("%line%") do @ set "var=%%b"
SET build=%var:~-11,8%
ECHO. %build%

Answer

When parsing structured markup, it's better to treat it as a hierarchical object than as flat text. Not only is it easier to navigate as a hierarchy than trying to match strings with tokens or a regexp, but an object-oriented approach is also more resistant to changes in formatting (whether the code is minified, beautified, line breaks are introduced, whatever).

With that in mind, I suggest using a querySelector to select anchor tags that are children of table elements whose classname is "fileList". Then use a regex to scrape the version info from the anchor tag's href attribute.

@if (@CodeSection == @Batch) @then
@echo off & setlocal

set "html=test.html"

for /f "delims=" %%I in ('cscript /nologo /e:JScript "%~f0" "%html%"') do set "%%I"

echo %build%

goto :EOF
@end // end batch / begin JScript hybrid code

var htmlfile = WSH.CreateObject('htmlfile'),
    fso = WSH.CreateObject('Scripting.FileSystemObject'),
    file = fso.OpenTextFile(WSH.Arguments(0), 1),
    html = file.ReadAll();

file.Close();
htmlfile.write('<meta http-equiv="x-ua-compatible" content="IE=9" />' + html);

var anchors = htmlfile.querySelectorAll('table.fileList a');

for (var i = 0; i < anchors.length; i++) {
    if (/webservice-((\d+\.)*\d.+)\.var$/i.test(anchors[i].href)) {
        WSH.Echo('build=' + RegExp.$1);
        WSH.Quit(0);
    }
}

What's even cooler is, if the HTML file you're scraping is served by a web server, you can also use the Microsoft.XMLHTTP methods to retrieve the HTML without having to rely on wget or curl or similar. This only requires a few minor changes to the code above.

@if (@CodeSection == @Batch) @then
@echo off & setlocal

set "URL=http://www.domain.com/file.html"

for /f "delims=" %%I in ('cscript /nologo /e:JScript "%~f0" "%URL%"') do set "%%I"

echo %build%

goto :EOF
@end // end batch / begin JScript hybrid code

var xhr = WSH.CreateObject('Microsoft.XMLHTTP'),
    htmlfile = WSH.CreateObject('htmlfile');

xhr.open('GET', WSH.Arguments(0), true);
xhr.setRequestHeader('User-Agent', 'XMLHTTP/1.0');
xhr.send('');
while (xhr.readyState != 4) WSH.Sleep(50);

htmlfile.write('<meta http-equiv="x-ua-compatible" content="IE=9" />' + xhr.responseText);

var anchors = htmlfile.querySelectorAll('table.fileList a');

for (var i = 0; i < anchors.length; i++) {
    if (/webservice-((\d+\.)*\d.+)\.var$/i.test(anchors[i].href)) {
        WSH.Echo('build=' + RegExp.$1);
        WSH.Quit(0);
    }
}
Comments