从 URL 下载 Windows 批处理文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4619088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Windows batch file file download from a URL
提问by James
I am trying to download a file from a website (ex. http://www.example.com/package.zip) using a Windows batch file. I am getting an error code when I write the function below:
我正在尝试使用 Windows 批处理文件从网站(例如http://www.example.com/package.zip)下载文件。当我编写下面的函数时,我收到一个错误代码:
xcopy /E /Y "http://www.example.com/package.zip"
The batch file doesn't seem to like the "/" after the http. Are there any ways to escape those characters so it doesn't assume they are function parameters?
批处理文件似乎不喜欢 http 后面的“/”。有什么方法可以转义这些字符,因此它不假定它们是函数参数?
回答by sevenforce
With PowerShell 2.0 (Windows 7 preinstalled) you can use:
使用 PowerShell 2.0(预装 Windows 7),您可以使用:
(New-Object Net.WebClient).DownloadFile('http://www.example.com/package.zip', 'package.zip')
Starting with PowerShell 3.0 (Windows 8 preinstalled) you can use Invoke-WebRequest
:
从 PowerShell 3.0(预装 Windows 8)开始,您可以使用Invoke-WebRequest
:
Invoke-WebRequest http://www.example.com/package.zip -OutFile package.zip
From a batch file they are called:
从批处理文件中,它们被称为:
powershell -Command "(New-Object Net.WebClient).DownloadFile('http://www.example.com/package.zip', 'package.zip')"
powershell -Command "Invoke-WebRequest http://www.example.com/package.zip -OutFile package.zip"
(PowerShell 2.0 is available for installation on XP, 3.0 for Windows 7)
(PowerShell 2.0 可在 XP 上安装,3.0 可用于 Windows 7)
回答by brainwood
There's a standard Windows component which can achieve what you're trying to do: BITS. It has been included in Windows since XP and 2000 SP3.
有一个标准的 Windows 组件可以实现您的目标:BITS。自 XP 和 2000 SP3 以来,它已包含在 Windows 中。
Run:
跑:
bitsadmin.exe /transfer "JobName" http://download.url/here.exe C:\destination\here.exe
The job name is simply the display name for the download job - set it to something that describes what you're doing.
作业名称只是下载作业的显示名称 - 将其设置为描述您正在执行的操作的名称。
回答by David Grayson
This might be a little off topic, but you can pretty easily download a file using Powershell. Powershell comes with modern versions of Windows so you don't have to install any extra stuff on the computer. I learned how to do it by reading this page:
这可能有点偏离主题,但您可以使用Powershell轻松下载文件。Powershell 附带现代版本的 Windows,因此您无需在计算机上安装任何额外的东西。我通过阅读此页面了解了如何做到这一点:
http://teusje.wordpress.com/2011/02/19/download-file-with-powershell/
http://teusje.wordpress.com/2011/02/19/download-file-with-powershell/
The code was:
代码是:
$webclient = New-Object System.Net.WebClient
$url = "http://www.example.com/file.txt"
$file = "$pwd\file.txt"
$webclient.DownloadFile($url,$file)
回答by LostInTheCode
Last I checked, there isn't a command line command to connect to a URL from the MS command line. Try wget for Windows:
http://gnuwin32.sourceforge.net/packages/wget.htm
最后我检查过,没有命令行命令可以从 MS 命令行连接到 URL。为 Windows 尝试 wget:http:
//gnuwin32.sourceforge.net/packages/wget.htm
or URL2File:
http://www.chami.com/free/url2file_wincon.html
或 URL2File:
http://www.chami.com/free/url2file_wincon.html
In Linux, you can use "wget".
在 Linux 中,您可以使用“wget”。
Alternatively, you can try VBScript. They are like command line programs, but they are scripts interpreted by the wscript.exe scripts host. Here is an example of downloading a file using VBS:
https://serverfault.com/questions/29707/download-file-from-vbscript
或者,您可以尝试 VBScript。它们类似于命令行程序,但它们是由 wscript.exe 脚本宿主解释的脚本。以下是使用 VBS 下载文件的示例:https:
//serverfault.com/questions/29707/download-file-from-vbscript
回答by Frank Einstein
Downloading files in PURE BATCH...
正在下载 PURE BATCH 中的文件...
Without any JScript, VBScript, Powershell, etc... Only pure Batch!
没有任何 JScript、VBScript、Powershell 等......只有纯批处理!
Some people are saying it's not possible of downloading files with a batch script without using any JScript or VBScript, etc... But they are definitely wrong!
有些人说在不使用任何 JScript 或 VBScript 等的情况下,不可能使用批处理脚本下载文件……但他们绝对是错误的!
Here is a simple method that seems to work pretty well for downloading files in your batch scripts. It should be working on almost any file's URL. It is even possible to use a proxy server if you need it.
这是一个简单的方法,似乎可以很好地下载批处理脚本中的文件。它应该适用于几乎所有文件的 URL。如果需要,甚至可以使用代理服务器。
For downloading files, we can use BITSADMIN.EXEfrom the Windows system. There is no need for downloading/installing anything or using any JScript or VBScript, etc. Bitsadmin.exeis present on most Windows versions, probably from XP to Windows 10.
对于下载文件,我们可以使用Windows 系统中的BITSADMIN.EXE。无需下载/安装任何内容或使用任何 JScript 或 VBScript 等。Bitsadmin.exe存在于大多数 Windows 版本中,可能从 XP 到 Windows 10。
Enjoy!
享受!
USAGE:
用法:
You can use the BITSADMIN command directly, like this:bitsadmin /transfer mydownloadjob /download /priority FOREGROUND "http://example.com/File.zip" "C:\Downloads\File.zip"
您可以直接使用 BITSADMIN 命令,如下所示:bitsadmin /transfer mydownloadjob /download /priority FOREGROUND "http://example.com/File.zip" "C:\Downloads\File.zip"
Proxy Server:
For connecting using a proxy, use this command before downloading.bitsadmin /setproxysettings mydownloadjob OVERRIDE "proxy-server.com:8080" "<local>"
代理服务器:
要使用代理连接,请在下载前使用此命令。bitsadmin /setproxysettings mydownloadjob OVERRIDE "proxy-server.com:8080" "<local>"
Click this LINKif you want more info about BITSadmin.exe
如果您想了解有关 BITSadmin.exe 的更多信息,请单击此链接
TROUBLESHOOTING:
If you get this error: "Unable to connect to BITS - 0x80070422"
Make sure the windows service "Background Intelligent Transfer Service (BITS)" is enabled and try again. (It should be enabled by default.)
故障排除:
如果您收到此错误:“无法连接到 BITS - 0x80070422”
请确保启用了 Windows 服务“后台智能传输服务 (BITS)”,然后重试。(它应该默认启用。)
CUSTOM FUNCTIONSCall :DOWNLOAD_FILE "URL"
Call :DOWNLOAD_PROXY_ON "SERVER:PORT"
Call :DOWNLOAD_PROXY_OFF
自定义功能Call :DOWNLOAD_FILE "URL"
Call :DOWNLOAD_PROXY_ON "SERVER:PORT"
Call :DOWNLOAD_PROXY_OFF
I made these 3 functions for simplifying the bitsadmin commands. It's easier to use and remember. It can be particularly useful if you are using it multiple times in your scripts.
我做了这 3 个函数来简化 bitsadmin 命令。它更容易使用和记住。如果您在脚本中多次使用它,它会特别有用。
PLEASE NOTE...
Before using these functions, you will first need to copy them from CUSTOM_FUNCTIONS.CMD to the end of your script. There is also a complete example: DOWNLOAD-EXAMPLE.CMD
请注意...
在使用这些函数之前,您首先需要将它们从 CUSTOM_FUNCTIONS.CMD 复制到脚本的末尾。还有一个完整的例子:DOWNLOAD-EXAMPLE.CMD
:DOWNLOAD_FILE "URL"
The main function, will download files from URL.
:DOWNLOAD_FILE "URL"
主要功能,将从 URL 下载文件。
:DOWNLOAD_PROXY_ON "SERVER:PORT"
(Optional) You can use this function if you need to use a proxy server.
Calling the :DOWNLOAD_PROXY_OFF function will disable the proxy server.
:DOWNLOAD_PROXY_ON "SERVER:PORT"
(可选) 如果需要使用代理服务器,可以使用此功能。
调用 :DOWNLOAD_PROXY_OFF 函数将禁用代理服务器。
EXAMPLE:CALL :DOWNLOAD_PROXY_ON "proxy-server.com:8080"
CALL :DOWNLOAD_FILE "http://example.com/File.zip" "C:\Downloads\File.zip"
CALL :DOWNLOAD_PROXY_OFF
例子:CALL :DOWNLOAD_PROXY_ON "proxy-server.com:8080"
CALL :DOWNLOAD_FILE "http://example.com/File.zip" "C:\Downloads\File.zip"
CALL :DOWNLOAD_PROXY_OFF
CUSTOM_FUNCTIONS.CMD
CUSTOM_FUNCTIONS.CMD
:DOWNLOAD_FILE
rem BITSADMIN COMMAND FOR DOWNLOADING FILES:
bitsadmin /transfer mydownloadjob /download /priority FOREGROUND %1 %2
GOTO :EOF
:DOWNLOAD_PROXY_ON
rem FUNCTION FOR USING A PROXY SERVER:
bitsadmin /setproxysettings mydownloadjob OVERRIDE %1 "<local>"
GOTO :EOF
:DOWNLOAD_PROXY_OFF
rem FUNCTION FOR STOP USING A PROXY SERVER:
bitsadmin /setproxysettings mydownloadjob NO_PROXY
GOTO :EOF
DOWNLOAD-EXAMPLE.CMD
下载示例.CMD
@ECHO OFF
SETLOCAL
rem FOR DOWNLOADING FILES, THIS SCRIPT IS USING THE "BITSADMIN.EXE" SYSTEM FILE.
rem IT IS PRESENT ON MOST WINDOWS VERSION, PROBABLY FROM WINDOWS XP TO WINDOWS 10.
:SETUP
rem URL (5MB TEST FILE):
SET "FILE_URL=http://ipv4.download.thinkbroadband.com/5MB.zip"
rem SAVE IN CUSTOM LOCATION:
rem SET "SAVING_TO=C:\FolderMB.zip"
rem SAVE IN THE CURRENT DIRECTORY
SET "SAVING_TO=5MB.zip"
SET "SAVING_TO=%~dp0%SAVING_TO%"
:MAIN
ECHO.
ECHO DOWNLOAD SCRIPT EXAMPLE
ECHO.
ECHO FILE URL: "%FILE_URL%"
ECHO SAVING TO: "%SAVING_TO%"
ECHO.
rem UNCOMENT AND MODIFY THE NEXT LINE IF YOU NEED TO USE A PROXY SERVER:
rem CALL :DOWNLOAD_PROXY_ON "PROXY-SERVER.COM:8080"
rem THE MAIN DOWNLOAD COMMAND:
CALL :DOWNLOAD_FILE "%FILE_URL%" "%SAVING_TO%"
rem UNCOMMENT NEXT LINE FOR DISABLING THE PROXY (IF YOU USED IT):
rem CALL :DOWNLOAD_PROXY_OFF
:RESULT
ECHO.
IF EXIST "%SAVING_TO%" ECHO YOUR FILE HAS BEEN SUCCESSFULLY DOWNLOADED.
IF NOT EXIST "%SAVING_TO%" ECHO ERROR, YOUR FILE COULDN'T BE DOWNLOADED.
ECHO.
:EXIT_SCRIPT
PAUSE
EXIT /B
rem FUNCTIONS SECTION
:DOWNLOAD_FILE
rem BITSADMIN COMMAND FOR DOWNLOADING FILES:
bitsadmin /transfer mydownloadjob /download /priority FOREGROUND %1 %2
GOTO :EOF
:DOWNLOAD_PROXY_ON
rem FUNCTION FOR USING A PROXY SERVER:
bitsadmin /setproxysettings mydownloadjob OVERRIDE %1 "<local>"
GOTO :EOF
:DOWNLOAD_PROXY_OFF
rem FUNCTION FOR STOP USING A PROXY SERVER:
bitsadmin /setproxysettings mydownloadjob NO_PROXY
GOTO :EOF
回答by Kalpesh Soni
' Create an HTTP object
myURL = "http://www.google.com"
Set objHTTP = CreateObject( "WinHttp.WinHttpRequest.5.1" )
' Download the specified URL
objHTTP.Open "GET", myURL, False
objHTTP.Send
intStatus = objHTTP.Status
If intStatus = 200 Then
WScript.Echo " " & intStatus & " A OK " +myURL
Else
WScript.Echo "OOPS" +myURL
End If
then
然后
C:\>cscript geturl.vbs
Microsoft (R) Windows Script Host Version 5.7
Copyright (C) Microsoft Corporation. All rights reserved.
200 A OK http://www.google.com
or just double click it to test in windows
或者只需双击它即可在 Windows 中进行测试
回答by Abscissa
AFAIK, Windows doesn't have a built-in commandline tool to download a file. But you can do it from a VBScript, and you can generate the VBScript file from batch using echo and output redirection:
AFAIK,Windows 没有内置的命令行工具来下载文件。但是您可以从 VBScript 中执行此操作,并且您可以使用 echo 和输出重定向从批处理中生成 VBScript 文件:
@echo off
rem Windows has no built-in wget or curl, so generate a VBS script to do it:
rem -------------------------------------------------------------------------
set DLOAD_SCRIPT=download.vbs
echo Option Explicit > %DLOAD_SCRIPT%
echo Dim args, http, fileSystem, adoStream, url, target, status >> %DLOAD_SCRIPT%
echo. >> %DLOAD_SCRIPT%
echo Set args = Wscript.Arguments >> %DLOAD_SCRIPT%
echo Set http = CreateObject("WinHttp.WinHttpRequest.5.1") >> %DLOAD_SCRIPT%
echo url = args(0) >> %DLOAD_SCRIPT%
echo target = args(1) >> %DLOAD_SCRIPT%
echo WScript.Echo "Getting '" ^& target ^& "' from '" ^& url ^& "'..." >> %DLOAD_SCRIPT%
echo. >> %DLOAD_SCRIPT%
echo http.Open "GET", url, False >> %DLOAD_SCRIPT%
echo http.Send >> %DLOAD_SCRIPT%
echo status = http.Status >> %DLOAD_SCRIPT%
echo. >> %DLOAD_SCRIPT%
echo If status ^<^> 200 Then >> %DLOAD_SCRIPT%
echo WScript.Echo "FAILED to download: HTTP Status " ^& status >> %DLOAD_SCRIPT%
echo WScript.Quit 1 >> %DLOAD_SCRIPT%
echo End If >> %DLOAD_SCRIPT%
echo. >> %DLOAD_SCRIPT%
echo Set adoStream = CreateObject("ADODB.Stream") >> %DLOAD_SCRIPT%
echo adoStream.Open >> %DLOAD_SCRIPT%
echo adoStream.Type = 1 >> %DLOAD_SCRIPT%
echo adoStream.Write http.ResponseBody >> %DLOAD_SCRIPT%
echo adoStream.Position = 0 >> %DLOAD_SCRIPT%
echo. >> %DLOAD_SCRIPT%
echo Set fileSystem = CreateObject("Scripting.FileSystemObject") >> %DLOAD_SCRIPT%
echo If fileSystem.FileExists(target) Then fileSystem.DeleteFile target >> %DLOAD_SCRIPT%
echo adoStream.SaveToFile target >> %DLOAD_SCRIPT%
echo adoStream.Close >> %DLOAD_SCRIPT%
echo. >> %DLOAD_SCRIPT%
rem -------------------------------------------------------------------------
cscript //Nologo %DLOAD_SCRIPT% http://example.com targetPathAndFile.html
More explanation here
更多解释在这里
回答by boksiora
Download Wget from here http://downloads.sourceforge.net/gnuwin32/wget-1.11.4-1-setup.exe
Then install it.
Then make some .bat file and put this into it
@echo off for /F "tokens=2,3,4 delims=/ " %%i in ('date/t') do set y=%%k for /F "tokens=2,3,4 delims=/ " %%i in ('date/t') do set d=%%k%%i%%j for /F "tokens=5-8 delims=:. " %%i in ('echo.^| time ^| find "current" ') do set t=%%i%%j set t=%t%_ if "%t:~3,1%"=="_" set t=0%t% set t=%t:~0,4% set "theFilename=%d%%t%" echo %theFilename% cd "C:\Program Files\GnuWin32\bin" wget.exe --output-document C:\backup\file_%theFilename%.zip http://someurl/file.zip
Adjust the URL and the file path in the script
- Run the file and profit!
从这里下载 Wget http://downloads.sourceforge.net/gnuwin32/wget-1.11.4-1-setup.exe
然后安装它。
然后制作一些.bat文件并将其放入其中
@echo off for /F "tokens=2,3,4 delims=/ " %%i in ('date/t') do set y=%%k for /F "tokens=2,3,4 delims=/ " %%i in ('date/t') do set d=%%k%%i%%j for /F "tokens=5-8 delims=:. " %%i in ('echo.^| time ^| find "current" ') do set t=%%i%%j set t=%t%_ if "%t:~3,1%"=="_" set t=0%t% set t=%t:~0,4% set "theFilename=%d%%t%" echo %theFilename% cd "C:\Program Files\GnuWin32\bin" wget.exe --output-document C:\backup\file_%theFilename%.zip http://someurl/file.zip
调整脚本中的URL和文件路径
- 运行文件和利润!
回答by Matt Wrock
You cannot use xcopy over http. Try downloading wget for windows. That may do the trick. It is a command line utility for non-interactive download of files through http. You can get it at http://gnuwin32.sourceforge.net/packages/wget.htm
您不能通过 http 使用 xcopy。尝试为 Windows 下载 wget。这可能会奏效。它是一个命令行实用程序,用于通过 http 非交互式下载文件。你可以在http://gnuwin32.sourceforge.net/packages/wget.htm得到它
回答by Sasikumar
Instead of wget you can also use aria2 to download the file from a particular URL.
除了 wget,您还可以使用 aria2 从特定 URL 下载文件。
See the following link which will explain more about aria2:
请参阅以下链接,该链接将解释有关 aria2 的更多信息: