1

I'm using a batch file to check the status of a mysql site. Right now everything works great on the sites side so that when I connect to it it'll say something like User is Online or Offline. In need a batch file to call the url and save the output of the page to a text file. This post does what I am asking without saving the output: Open a URL without using a browser from a batch file . It can be a vbscript as long as it can be called via a batch file. Thanks a bunch!

Origional Script from post above:

@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"=="" (
        wscript //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();

    // All done. Exit
    WScript.Quit(0);
PeterMader
  • 6,987
  • 1
  • 21
  • 31
Luke Deven
  • 66
  • 1
  • 8
  • Capture the output of `wscript //E:JScript "%~dpnx0" "%~1"` by a [`for /F` loop](http://ss64.com/nt/for_cmd.html): `for /F "delims=" %%I in ('wscript //E:JScript "%~dpnx0" "%~1"') do (echo one line of output: %%I)` – aschipfl Jul 02 '17 at 16:40

1 Answers1

3

If you don't mind using powershell from the batch:

:: WebResponse.cmd
@Echo off
For /f "delims=" %%A in (
  'powershell -NonI -NoP -C "(Invoke-Webrequest "%~1").content"'
) Do Echo one line of Webresponse %%A

Sample output with an URL to get your current external IP

> WebResponse.cmd "http://api.ipify.org"
one line of Webresponse 93.226.203.xxx
  • How could I make this work with a url that contains a question mark? I'm calling this URL: `http://Website.com/teststat.php?username=Lukaka`. It keeps replying: `one line of Webresponse Invoke-WebRequest : A positional parameter cannot be found that accepts argument 'Lukaka'.` – Luke Deven Jul 03 '17 at 13:19
  • 1
    Either quote the URL `"http://Website.com/teststat.php?username=Lukaka"`or escape the equal sign `^=` (cmd splits the argument at the `=`) –  Jul 03 '17 at 13:22
  • okay thanks. I'll try that out. Edit: I tried it out, and it works! Thanks a bunch and thanks for the speedy reply! – Luke Deven Jul 03 '17 at 13:43