6

I want to read the contents of a text file line by line being hosted on a website with a batch file. Ex: from example.com/text.txt

I had tried

@echo off
for /f "tokens=*" %%A in (http://example.com/text.txt) do (echo%%A)

But it looks for a file named that not the url.

4 Answers4

7

You need to download the file first.

Assuming you are on a Windows that supports powershell and you are using batch (otherwise just use Powershell and directly use the command in quotes):

@echo off
powershell -Command "(New-Object Net.WebClient).DownloadFile('http://www.example.com/text.txt', 'txtfile.txt')"
for /f "tokens=*" %%A in (txtfile.txt) do (echo%%A)

The 2nd line will download the text.txt file from the specified link and save it as txtfile.txt in the same directory.

EDIT: If you have a newer Windows version that supports PowerShell 3.0 and up, check @rahuldottech's answer, it's simpler and faster.

Fanatique
  • 4,648
3

You can use the following code:

powershell wget http://example.com/file.txt -OutFile file.txt
for /f "tokens=*" %%A in (file.txt) do (echo%%A)

We use powershell to download the file, and then process it in batch. Using wget is simpler than creating an object and then downloading the file.

undo
  • 6,071
  • This requires PowerShell 3.0 or newer so it does not work on eg. Windows 7. – Konrad Botor Jun 28 '18 at 12:06
  • Since wget is only an alias Get-Alias -Definition Invoke-WebRequest for Invoke-WebRequest I'd use the real/underlying cmdlet name. – LotPings Jun 28 '18 at 16:40
  • @KonradBotor Things may need to be upgraded, but Windows 7 SP1 supports PowerShell versions 3.0 - 5.1 per Microsoft. – Anaksunaman Jun 28 '18 at 17:22
  • It supports newer versions of PowerShell, but comes preinstalled with version 2.0 and I doubt somebody who wants to use a batch script has upgraded it to newer version. – Konrad Botor Jun 28 '18 at 18:03
  • @lotpings why? Aliases exist to make life easier – undo Jun 28 '18 at 20:33
  • Because things change? There could be a wget.exe in the path, or MS includes curl.exe in the very latest windows 10 version, scripts run in different environments. On Linux or MacOs in powershell there is no more a ls alias etc. etc. Also the parameters for Invoke-WebRequest are different from real wget.exe. So life may get easier only in the short run. – LotPings Jun 28 '18 at 20:40
  • @LotPings The built-in "curl" needs to be executed from the special bash shell, and if you have wget.exe, you should just be using that in cmd, instead of calling powershell to download the file, and powershell gives aliases priority over executables in the path anyway iirc. And this question is tagged [batch-file] which only run on Windows and thus the Linux and MacOS concerns are irrelevant here. – undo Jun 29 '18 at 10:18
  • You and me know of the precedence of aliases, implications of order in path etc. but others don't. Moving around all the environments it seems more important me - let's agree to disagree on the issue. BTW I can use C:\Windows\System32\curl.exe pretty normal in Win10Pro in cmd or PowerShell. – LotPings Jun 29 '18 at 10:34
  • @LotPings My bad, I thought you were talking about the Bash-on-Windows curl, not https://blogs.technet.microsoft.com/virtualization/2017/12/19/tar-and-curl-come-to-windows/ – undo Jun 29 '18 at 19:29
2

you need first to download it.For this you can use winhttpjs.bat:

call winhhtpjs.bat http://example.com/text.txt -saveTo c:\text.txt
for /f "usebackq tokens=*" %%A in ("c:\text.txt") do (echo%%A)

More on how you can download a file with a batch file

npocmaka
  • 1,263
  • 1
    While this technically works, it may cause issues with HTTPS and unusual headers. It's better to use powershell (which is built-in) because it has commands designed specifically to download files and there's no hackery involved – undo Jun 28 '18 at 12:05
  • @rahuldottech - powershell is not installed by default on XP,Vista and Windows Server 2003. In these cases using the activex objects for internet download is probably the only option. – npocmaka Jun 28 '18 at 12:12
  • 1
    @nlocmaka in those cases, you can use VBS. Plenty of scripts on the internet. That said, using an unsupported version of Windows is highly discouraged – undo Jun 28 '18 at 12:13
  • 1
    @rahuldottech - this is basically VBS (well... jscript which is also WSH language) and is pretty backward compatible. Though at the end it is matter of personal preferences what approach will be used. Probably I'll prefer powershell too in the most of cases as it ...powerful :) – npocmaka Jun 28 '18 at 12:18
1

You can download and read file contents using Certutil command line like this batch script :

@echo off
Color 0A
Title Download and read file contents with Certutil line command by Hackoo 2018
set "URL=http://www.example.com/text.txt"
for %%a in (%URL%) do Set "File=%%~nxa"
Set "DownloadFolder=%Temp%"
Call :Download "%URL%" "%DownloadFolder%\%File%"
Rem To read the contents of the text file downloaded in temp folder
for /f "tokens=*" %%A in ('Type "%DownloadFolder%\%File%"') do (echo %%A)
pause>nul & Exit
::--------------------------------------------
:Download <Url> <File>
certutil.exe -urlcache -split -f %1 %2 >nul
exit /b
::-------------------------------------------
Hackoo
  • 1,404
  • A much better solution, than others as this will work in everything greater than XP, (And including XP if you install the Windows 2003 Administration pack found here: https://www.microsoft.com/en-us/download/details.aspx?id=16770) – sparks Sep 20 '18 at 03:48