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.

1
  • run for /f to see what is supported. Looks like http was not on the list
    – phuclv
    Jul 15, 2018 at 16:44

4 Answers 4

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.

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.

9
  • This requires PowerShell 3.0 or newer so it does not work on eg. Windows 7. Jun 28, 2018 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, 2018 at 16:40
  • @KonradBotor Things may need to be upgraded, but Windows 7 SP1 supports PowerShell versions 3.0 - 5.1 per Microsoft. Jun 28, 2018 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. Jun 28, 2018 at 18:03
  • @lotpings why? Aliases exist to make life easier
    – undo
    Jun 28, 2018 at 20:33
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

4
  • 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, 2018 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, 2018 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, 2018 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, 2018 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
::-------------------------------------------
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.