0

My batch script is performing an HTTP request by using CScript with Javascript syntax like in my example.

Using this approach (also seen here) and some help on escaping i tried the following:

@if (@This==@IsBatch) @then
@echo off
rem **** batch zone *********************************************************
    setlocal enableextensions EnableDelayedExpansion

    set OWNPATH="%~dpnx0"

    if not "%~11"=="" (
        FOR /F "usebackq tokens=*" %%r in (`cscript //E:JScript %OWNPATH%`) DO SET RESULT=%%r
        ECHO %RESULT%
    )

    exit /b

@end
// **** JScript zone *****************************************************
// Instantiate the needed component to make url queries
var http = WScript.CreateObject('Msxml2.XMLHTTP.6.0');

// perform request
var requestURL = "http://myserver/api";

// Make the request 
http.open("GET", requestUrl, false);
http.send();

WScript.Echo(http.ResponseText);

// All done. Exit
WScript.Quit(0);

Unfortunately i get a message that "ECHO is off." and not the string in %RESULT%.

The script is run on a Windows 2008 R2 server.

4
  • 4
    you need delayed expansion Commented Apr 26, 2017 at 15:05
  • Thanks, got it! echo !RESULT! works! Commented Apr 26, 2017 at 15:08
  • Possible duplicate of Variables in batch not behaving as expected Commented Apr 26, 2017 at 15:55
  • 1
    Also consider the /nologo switch for cscript. Commented Apr 26, 2017 at 19:01

1 Answer 1

1

Thanks to @Stephan it works when using

echo !RESULT!

due to the delayed expansion

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.