4

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="./address.var.varapplication-varapplication-varwebservice-05.05.07-SNAPSHOT.var">address.var.varapplication-varapplication-varwebservice-05.05.07-SNAPSHOT.var</a></td><td class="fileSize">133.49 MB</td><td><a href="./address.var.varapplication-varapplication-varwebservice-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="./address.var.varapplication-varapplication-varwebservice-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%
1
  • Welcome to StackOverflow! You are asking your question the right way, including sample data, the code you've tried to parse it, and clearly explaining the output you desire. Well done! Commented Jul 28, 2016 at 15:34

2 Answers 2

1

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);
    }
}
Sign up to request clarification or add additional context in comments.

15 Comments

The first code works perfect, but the second, which I'm very interested, displayed the following error: code url.bat(13, 1) Microsoft JScript runtime error: Object doesn't support this property or method
I appreciate if you will put some comments to understand the code (I don't have any experience with JScript). Thanks!
I think JScript stop working so I added a code (find it on stackoverflow): //to trigger the error: throw new FatalError("Something went badly wrong!"); and error message displayed is: test.bat(13, 1) Microsoft JScript runtime error: Something went badly wrong! Any ideas why this is happening?
@Deco LOL I'm sorry. I had a typo on line 13. I said CreateOjbect. I'll fix it. I didn't feel like spinning up a web server to test, so let me know if you find any more problems.
I'm not sure what was happening because also the first script stop working. I restarted the Windows but the problem remain. Could be a problem with Java? Should I reinstall the Java?
|
0

Try this:

findstr /ic:"webservice" a.html

2 Comments

Unfortunately is displaying: "< was unexpected at this time."
@ser2956477: I modified the script according with your suggestion and I also added "{" but still no useful result: for /F "delims=" %%a in ('findstr /ic:"webservice" test.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%

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.