First to clarify this question is aimed to HTTP(s) download .For FTP may be I'll ask (and answer) another question. Here are some similar questions - but I want to be more precise .
Besides excluding external tools I want the solution(s) to be applicable for the widest possible types of windows machines (including XP,Win2003,Vista which still have big enough share). Also as WSH
is one of the possible options I prefer no using of temp files and everything to be packed in a single .bat
file (which is possible with both jscript and vbscript).
What are possible approaches.
To open the BAT file in Notepad, right-click it and choose Show more options > Edit from the menu (or just Edit in some Windows versions). You might find it helpful to use more advanced text editors that support syntax highlighting when editing a BAT file.
The answers.All scripts should be saved with .bat
/.cmd
extensions and can be used directly as batch scripts.
Certutuil (for some reasons in the newest win10 builds this is recognized as trojan thread ):
certutil.exe -urlcache -split -f "https://download.sysinternals.com/files/PSTools.zip" pstools.zip
for easiness a macro can be used:
set "download=certutil.exe -urlcache -split -f" %download% "https://download.sysinternals.com/files/PSTools.zip" pstools.zip
CertUtil command can be abused to download a file from internet.Available by default in windows since Vista.For WinXP Server 2003 Administration Tools are needed.
simplest possible way to use it
bitsadmin /transfer myDownloadJob /download /priority normal http://downloadsrv/10mb.zip c:\10mb.zip
with macro:
set "dnld=bitsadmin /transfer myDownloadJob /download /priority normal" %dnld% "https://download.sysinternals.com/files/PSTools.zip" %cd%\pstools.zip
or with bitsDownloader.bat
call bitsDownloader.bat "https://download.sysinternals.com/files/PSTools.zip" pstools.zip
call winhhtpjs.bat "https://example.com/files/some.zip" -saveTo "c:\somezip.zip"
XMLHTTPDownloadJS.bat is bat file that uses MSXML2.XMLHTTP object to download a file . Does not offer so rich options as winhhtpjs.bat , though is still an option.
call XMLHTTPDownloadJS.bat "https://download.sysinternals.com/files/PSTools.zip pst2.zip" pstools.zip
WebClientDownload.bat uses the .NET System.Net.WebClient class. It creates a small exe file in order to selfcompile itself and requires an installed a .net framework. As the class is presented in the very earlier versions of .net it is backward compatible enough
call webclientDownload.bat "https://download.sysinternals.com/files/PSTools.zip" pstools.zip
With the latest builds of windows 10 we have the CURL command ,though this is not so backward compatible option. Mind that only the newest versions of windows has CURL installed by default.
curl "https://download.sysinternals.com/files/PSTools.zip" --output pstools.zip
There's a utility (resides with CMD) on Windows which can be run from CMD (if you have write access):
set url=https://www.nsa.org/content/hl-images/2017/02/09/NSA.jpg set file=file.jpg certutil -urlcache -split -f %url% %file% :also certutil.exe -verifyctl -f -split %url% %file%
Cmdlets in Powershell:
$url = "https://www.nsa.org/content/hl-images/2017/02/09/NSA.jpg" $file = "file.jpg" $ProgressPreference = "SilentlyContinue"; Invoke-WebRequest -Uri $url -outfile $file
.Net under PowerShell:
$url = "https://www.nsa.org/content/hl-images/2017/02/09/NSA.jpg" $file = "file.jpg" # Add the necessary .NET assembly Add-Type -AssemblyName System.Net.Http # Create the HttpClient object $client = New-Object -TypeName System.Net.Http.Httpclient $task = $client.GetAsync($url) $task.wait(); [io.file]::WriteAllBytes($file, $task.Result.Content.ReadAsByteArrayAsync().Result)
C# Command-line build with csc.exe:
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-options/command-line-building-with-csc-exe
using System; using System.IO; using System.Net.Http; using System.Threading.Tasks; namespace DownloadImage { class Program { static async Task Main(string[] args) { using var httpClient = new HttpClient(); var url = "https://www.nsa.org/content/hl-images/2017/02/09/NSA.jpg"; byte[] imageBytes = await httpClient.GetByteArrayAsync(url); using var fs = new FileStream("file.jpg", FileMode.Create); fs.Write(imageBytes, 0, imageBytes.Length); } } }
Built in Windows applications. No need for external downloads.
Tested on Win 10
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With