Is it possible to display the HTML Code of a webpage in a batch file?
This code is from a previous question that only needed to do the query to the server (linked in comments) with the "display" of the page source code added.
@if (@This==@IsBatch) @then
@echo off
rem **** batch zone *********************************************************
setlocal enableextensions disabledelayedexpansion
rem Batch file will delegate all the work to the script engine
if not "%~1"=="" (
cscript //E:JScript "%~dpnx0" %1
)
rem End of batch area. Ensure batch ends execution before reaching
rem javascript zone
exit /b
@end
// **** Javascript zone *****************************************************
// Instantiate the needed component to make url queries
var http = WScript.CreateObject('Msxml2.XMLHTTP.6.0');
// Retrieve the url parameter
var url = WScript.Arguments.Item(0)
// Make the request
http.open("GET", url, false);
http.send();
// If we get a OK from server (status 200), echo data to console
if (http.status === 200) WScript.StdOut.Write(http.responseText);
// All done. Exit
WScript.Quit(0);
It is just an hybrid batch/javascript file. Saved as callurl.cmd
and called as callurl "http://www.google.es"
it will do what you ask for. No error check appart from correct response, no post, just a skeleton.
Open a Web Page in a Windows Batch FIle
You can use the start
command to do much the same thing as ShellExecute
. For example
start "" http://www.stackoverflow.com
This will launch whatever browser is the default browser, so won't necessarily launch Internet Explorer.
Batch script get html site and parse content (without wget, curl or other external app)
I've only ever used wget to fetch web content from a Windows batch script. Using an XHR via JScript was a fantastic idea!
But the script you're trying to plunder appears to be intended for checking whether a web server is responding, not for fetching content.
With some modifications, you can use it to fetch a web page and do whatever processing you need.
@if (@a==@b) @end /*
:: fetch.bat <url>
:: fetch a web page
@echo off
setlocal
if "%~1"=="" goto usage
echo "%~1" | findstr /i "https*://" >NUL || goto usage
set "URL=%~1"
for /f "delims=" %%I in ('cscript /nologo /e:jscript "%~f0" "%URL%"') do (
rem process the HTML line-by-line
echo(%%I
)
goto :EOF
:usage
echo Usage: %~nx0 URL
echo for example: %~nx0 http://www.google.com/
echo;
echo The URL must be fully qualified, including the http:// or https://
goto :EOF
JScript */
var x=new ActiveXObject("Microsoft.XMLHTTP");
x.open("GET",WSH.Arguments(0),true);
x.setRequestHeader('User-Agent','XMLHTTP/1.0');
x.send('');
while (x.readyState!=4) {WSH.Sleep(50)};
WSH.Echo(x.responseText);
Windows batch file to find variable string in a html file
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);
}
}
Add a string of text into a html document using cmd/batch file
Sounds like you already have the bat script written to prompt your dad for the values. So I will focus on how to insert those into your html page.
In your current html file, change the formatting of the end of your table, placing the last </tr> directly in front of the </table>
</tr></table>
in your bat file, replace
(some command here to add it)
<tr style="mso-yfti-irow:34">
<td valign="top" style="width:180;padding-left:3.5pt; padding-right:3.5pt; padding-top:0cm; padding-bottom:0cm" height="5">
<font size="4">%Date%</font></td>
<td valign="top" style="width:500;padding-left:3.5pt; padding-right:3.5pt; padding-top:0cm; padding-bottom:0cm" height="5">
<font size="4">%Location%</font></td>
<td valign="top" style="width:523;padding-left:3.5pt; padding-right:3.5pt; padding-top:0cm; padding-bottom:0cm" height="5">
<font size="4">%Amount%</font></td>
</tr>
with this
powershell -command "(Get-Content Name_of_File.html) -replace '</tr></table>', '<tr style="mso-yfti-irow:34"> <td valign="top" style="width:180;padding-left:3.5pt; padding-right:3.5pt; padding-top:0cm; padding-bottom:0cm" height="5"><font size="4">%Date%</font></td><td valign="top" style="width:500;padding-left:3.5pt; padding-right:3.5pt; padding-top:0cm; padding-bottom:0cm" height="5"><font size="4">%Location%</font></td><td valign="top" style="width:523;padding-left:3.5pt; padding-right:3.5pt; padding-top:0cm; padding-bottom:0cm" height="5"><font size="4">%Amount%</font></td></tr></table>' | Set-Content Name_of_File.html"
Powershell.exe -executionpolicy remotesigned -File replace_quot.ps1
This will execute a PowerShell search and replace command from within your batch script to find </tr></table> and replace it with the new row. Each time your dad adds a new trip to the page, it will add the information to the bottom of the table.
The powershell -command lets batch know to execute everthing between the "double quotes" as powershell. The get-content reads your html file. So replace Name_of_File.html with the name of your HTML page.
The next part of the command performs a search and replace. You must use "; instead " for powershell to read the entire line as a fluid set of actions. It will write the HTML code and use your variables of %Date%, %Location% and %Amount% for the new values.
The file is then written back to the html page using set-content. So make sure the Name_of_File.html is updated here as well with the name of your HTML page.
Save the following line in a different script named replace_quot.ps1 and place this script in the same directory as your bat file.
(Get-Content Name_of_File.html) -replace '"', '"' | set-content Name_of_File.html
This script will be called by your bat file to replace the &qout; with the actual " character in your html file.
Related Topics
Jquery Autocomplete Where The Results Are Links
Image Behavior Within Flexbox (Rows Embedded in Columns)
How to Style Input File Textbox
Svg Line Markers Not Updating When Line Moves in Ie10
Viewport Meta Tags Vs Media Queries
How to Safely Render HTML in React
Table Columns, Setting Both Min and Max Width with CSS
How to Display Openweathermap Weather Icon
What's The Difference Between Meta Name and Meta Property
Webpage Starts Zoomed in on Mobile Devices
How to Set Different Colors in HTML in One Statement
How to Style and Align Forms Without Tables
Yet Another HTML/CSS Layout Challenge - Full Height Sidebar with Sticky Footer
Brackets Displays Wrongly for Right to Left Display Style