1

I want to get content of this page and save it as a batch variable.

http://ip-api.com/line?fields=2

I just want to do it with batch without any external apps or PowerShell commands if possible.

The above URL will return country code of the current IP Address.

I already did it with this batch file, But I don't want PowerShell codes:

@echo off
for /f "tokens=*" %%i in ('powershell /command "(Invoke-WebRequest -Uri 'http://ip-api.com/line?fields=2').Content"') do set return=%%i
echo "%return%"
pause
Aria
  • 75
  • 9
  • 1
    If you would want to install some external tool such curl or wget, you could do it with it. Not a better alternative to calling powershell, though. You could also use BITS (see: https://superuser.com/questions/299754/wget-curl-alternative-native-to-windows) but It may not be recommended since this warning is shown: `BITSAdmin is deprecated and is not guaranteed to be available in future versions of Windows. Administrative tools for the BITS service are now provided by BITS PowerShell cmdlets.` suggesting you to use powershell. I think you are already using the best option – Julio Aug 14 '18 at 10:38
  • 1
    take a look at this - https://stackoverflow.com/questions/28143160/how-can-i-download-a-file-with-batch-file-without-using-any-external-tools . You can save the result to a file and read it. – npocmaka Aug 14 '18 at 10:49
  • And I have this URL `https://pastebin.com/raw/s5BxuEEw`, I want to get the last line only, How can I do that? Like getting the last number from a page and then save it as an integer so I can do ++ or add 1 to it. For example if the last number is 5 , get it and +1 it and save 6 as a variable. @Julio – Aria Aug 14 '18 at 10:56
  • batch and jscript hybrid, yes. Pure batch, no. – Gerhard Aug 14 '18 at 11:02
  • @GerhardBarnard I'm worried because not many windows versions and editions have `curl` or `wget` , I didnt test `Invoke-WebRequest` tho. Do you have any idea about this? – Aria Aug 14 '18 at 11:03
  • different versions of powershell may cause issues, but jscript you will be compatible across the board really. – Gerhard Aug 14 '18 at 11:04
  • I can write you something, but [here is a link](https://stackoverflow.com/questions/36457549/windows-batch-parse-data-from-html-web-page) to something similar already. – Gerhard Aug 14 '18 at 11:06
  • is it possible with C++ codes? – Aria Aug 14 '18 at 11:08
  • C++ offcourse yes :) – Gerhard Aug 14 '18 at 11:12
  • @GerhardBarnard How can I contact you if you know C++? – Aria Aug 14 '18 at 11:32
  • What is your dislike of using PowerShell? You code requires PowerShell 3.0. PowerShell 2.0 is almost a decade old. – lit Aug 14 '18 at 14:32

0 Answers0