如何在没有浏览器和 open/save 对话框批处理的情况下下载文件?
How to download a file without browser and open/save dialog batch maybe?
我想问一下,除了浏览器和其他软件之外,还有什么其他方法可以从 Internet 下载文件,我认为可以使用 CMD(命令行)、批处理脚本?
更确切地说,可以使用 windows(比方说 7)组件来下载没有对话框的文件,例如 vbs、hta、..etc 吗?我不想下载 wget、curl、telnet 或其他组件
这将适用于 windows 7?
http://semitwist.com/articles/article/view/downloading-files-from-plain-batch-with-zero-dependencies
批量无法原生下载文件。
要从命令行下载文件,您必须使用第三方工具,如 cURL 或 wget。
Linux:
wget http://somedomain.com/somefile.xxx
如果你使用的是windows,下载一个名为wget.exe
(here)的exe文件,你就可以使用上面的代码了。
在 vbscript 中试试这个示例:
Option Explicit
Dim URL,ws,fso,Srcimage,Temp,PathOutPutHTML,fhta,stRep,stFichier,oShell,oFolder,oFichier,Dimensions
Dim arrSize,intLength,intHorizontalSize,intVerticalSize,Tab
URL = "http://www.animatedimages.org/data/media/902/animated-tunisia-flag-image-0023.gif"
Set ws = CreateObject("wscript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
Temp = WS.ExpandEnvironmentStrings("%Temp%")
PathOutPutHTML = Temp & "\image.hta"
Set fhta = fso.OpenTextFile(PathOutPutHTML,2,True)
stRep = Temp
Tab = split(url,"/")
stFichier = Tab(UBound(Tab))
Srcimage = stRep & "\" & stFichier
If Not fso.FileExists(Srcimage) Then
Call DownloadingFile(URL,Srcimage)
Set oShell = CreateObject("Shell.Application")
Set oFolder = oShell.Namespace(stRep)
Set oFichier = oFolder.Items.Item(stFichier)
Dimensions = oFolder.GetDetailsOf(oFichier,31)
arrSize = Split(Dimensions,"x")
'***************************************Important à savoir **********************************************
'Instead, we ended up retrieving item 31, which gave us the total dimensions of the picture,
'using an output format similar to this: ?150 x 354?
'http://blogs.technet.com/b/heyscriptingguy/archive/2008/05/16/how-can-i-search-a-folder-for-all-the-image-files-that-are-not-a-specified-height-and-width.aspx
'Un grand merci à omen999 ==>
'http://www.developpez.net/forums/d1504644/autres-langages/general-visual-basic-6-vbscript/vbscript/passage-variables-procedure/#post8163406
intLength = Len(arrSize(0))
intHorizontalSize = Right(arrSize(0),intLength -1)
intLength = Len(arrSize(1))
intVerticalSize = Left(arrSize(1),intLength - 1)
'***************************************Important à savoir **********************************************
Call LoadImage(Srcimage,intHorizontalSize,intVerticalSize,Timeout(51))
ws.run "mshta.exe " & PathOutPutHTML
Else
Set oShell = CreateObject("Shell.Application")
Set oFolder = oShell.Namespace(stRep)
Set oFichier = oFolder.Items.Item(stFichier)
Dimensions = oFolder.GetDetailsOf(oFichier,31)
arrSize = Split(Dimensions,"x")
intLength = Len(arrSize(0))
intHorizontalSize = Right(arrSize(0),intLength -1)
intLength = Len(arrSize(1))
intVerticalSize = Left(arrSize(1),intLength - 1)
Call LoadImage(Srcimage,intHorizontalSize,intVerticalSize,Timeout(51))
ws.run "mshta.exe " & PathOutPutHTML
End If
'********************************************************************************************************
Function TimeOut(T)
TimeOut = T * 1000
End Function
'********************************************************************************************************
Sub LoadImage(Srcimage,intHorizontalSize,intVerticalSize,TimeOut)
fhta.WriteLine "<html>"
fhta.WriteLine " <hta:application id=""oHTA"" "
fhta.WriteLine " border=""none"" "
fhta.WriteLine " caption=""no"" "
fhta.WriteLine " contextmenu=""no"" "
fhta.WriteLine " innerborder=""no"" "
fhta.WriteLine " scroll=""no"" "
fhta.WriteLine " showintaskbar=""no"" "
fhta.WriteLine " />"
fhta.WriteLine "<style>"
fhta.WriteLine "{ margin: 0; padding: 0; }"
fhta.WriteLine "body {background: url(" & DblQuote(Srcimage) & ") no-repeat center center fixed;}"
fhta.WriteLine "</style>"
fhta.WriteLine " <script language=""VBScript"">"
fhta.WriteLine " Sub Window_OnLoad()"
fhta.WriteLine " width = " & intHorizontalSize
fhta.WriteLine " height = " & intVerticalSize
fhta.WriteLine " window.resizeTo width, height"
fhta.WriteLine " window.moveTo screen.availWidth - width, screen.availHeight - height"
fhta.WriteLine " idTimer = window.setTimeout(""vbscript:window.close"","& TimeOut &")"
fhta.WriteLine " window.setInterval ""setfocus()"",100"
fhta.WriteLine " End Sub"
fhta.WriteLine " Function setfocus"
fhta.WriteLine " Window.Focus()"
fhta.WriteLine " End Function"
fhta.WriteLine " </script>"
fhta.WriteLine "<body>"
fhta.WriteLine "<bgsound src=""http://hackoo.alwaysdata.net/Tunisie.mp3"">"
fhta.WriteLine "</body>"
fhta.WriteLine "</html>"
End Sub
'**********************************************************************************************
Function DblQuote(Str)
DblQuote = Chr(34) & Str & Chr(34)
End Function
'**********************************************************************************************
Sub DownloadingFile(URL,strHDLocation)
Dim Titre,objFSO,Ws,objXMLHTTP,PathScript,Tab,objADOStream,Command,Start,File
Dim MsgTitre,MsgAttente,StartTime,DurationTime,ProtocoleHTTP
Set objFSO = Createobject("Scripting.FileSystemObject")
Set Ws = CreateObject("wscript.Shell")
ProtocoleHTTP = "http://"
If Left(URL,7) <> ProtocoleHTTP Then
URL = ProtocoleHTTP & URL
End if
Tab = split(url,"/")
File = Tab(UBound(Tab))
File = Replace(File,"%20"," ")
File = Replace(File,"%28","(")
File = Replace(File,"%29",")")
Set objXMLHTTP = CreateObject("MSXML2.ServerXMLHTTP.3.0")
strHDLocation = PathScript & "\" & File
On Error Resume Next
objXMLHTTP.open "GET",URL,false
objXMLHTTP.send()
If Err.number <> 0 Then
MsgBox err.description,16,err.description
Exit Sub
Else
If objXMLHTTP.Status = 200 Then
strHDLocation = Temp & "\" & File
Set objADOStream = CreateObject("ADODB.Stream")
objADOStream.Open
objADOStream.Type = 1 'adTypeBinary
objADOStream.Write objXMLHTTP.ResponseBody
objADOStream.Position = 0 'Set the stream position to the start
objADOStream.SaveToFile strHDLocation,2 '2=adSaveCreateOverWrite
objADOStream.Close
Set objADOStream = Nothing
End If
End if
Set objXMLHTTP = Nothing
End Sub
这里可能是另一个示例:VBS/Batch Check if Download Complete
PowerShell 可以很容易地做到这一点;但是 - 尽管 Windows 7 中包含一个版本 - 您可能希望下载最新版本。
在 Windows 7 或 Windows 8(和相关服务器版本)中,您可以将驱动器映射到 WebDAV 文件夹,并使用 COPY、XCOPY 或 ROBOCOPY 来满足您的需求。如果愿意,您可以使用命令行 NET USE 映射驱动器。
比如NET USE S: https:\live.sysinternals.com(或者用PUSHD),跟着你复制命令。
干杯!
您可以试试这个使用 PowerShell 的 Vbscript 下载并执行文件。
所以在这个例子中我们下载了一个 mp3 文件并播放它。
Option Explicit
Dim MyCmd,Ws,Ret
Set Ws = CreateObject("wscript.Shell")
MyCmd = "cmd /c Powershell.exe -ExecutionPolicy bypass -noprofile -WindowStyle Hidden (New-Object System.Net.WebClient).DownloadFile('http://hackoo.alwaysdata.net/Matrix.mp3','%TEMP%\Matrix.mp3'); Start-Process %TEMP%\Matrix.mp3;"
Ret = Ws.run(MyCmd,0,True)
我想问一下,除了浏览器和其他软件之外,还有什么其他方法可以从 Internet 下载文件,我认为可以使用 CMD(命令行)、批处理脚本? 更确切地说,可以使用 windows(比方说 7)组件来下载没有对话框的文件,例如 vbs、hta、..etc 吗?我不想下载 wget、curl、telnet 或其他组件 这将适用于 windows 7? http://semitwist.com/articles/article/view/downloading-files-from-plain-batch-with-zero-dependencies
批量无法原生下载文件。
要从命令行下载文件,您必须使用第三方工具,如 cURL 或 wget。
Linux:
wget http://somedomain.com/somefile.xxx
如果你使用的是windows,下载一个名为wget.exe
(here)的exe文件,你就可以使用上面的代码了。
在 vbscript 中试试这个示例:
Option Explicit
Dim URL,ws,fso,Srcimage,Temp,PathOutPutHTML,fhta,stRep,stFichier,oShell,oFolder,oFichier,Dimensions
Dim arrSize,intLength,intHorizontalSize,intVerticalSize,Tab
URL = "http://www.animatedimages.org/data/media/902/animated-tunisia-flag-image-0023.gif"
Set ws = CreateObject("wscript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
Temp = WS.ExpandEnvironmentStrings("%Temp%")
PathOutPutHTML = Temp & "\image.hta"
Set fhta = fso.OpenTextFile(PathOutPutHTML,2,True)
stRep = Temp
Tab = split(url,"/")
stFichier = Tab(UBound(Tab))
Srcimage = stRep & "\" & stFichier
If Not fso.FileExists(Srcimage) Then
Call DownloadingFile(URL,Srcimage)
Set oShell = CreateObject("Shell.Application")
Set oFolder = oShell.Namespace(stRep)
Set oFichier = oFolder.Items.Item(stFichier)
Dimensions = oFolder.GetDetailsOf(oFichier,31)
arrSize = Split(Dimensions,"x")
'***************************************Important à savoir **********************************************
'Instead, we ended up retrieving item 31, which gave us the total dimensions of the picture,
'using an output format similar to this: ?150 x 354?
'http://blogs.technet.com/b/heyscriptingguy/archive/2008/05/16/how-can-i-search-a-folder-for-all-the-image-files-that-are-not-a-specified-height-and-width.aspx
'Un grand merci à omen999 ==>
'http://www.developpez.net/forums/d1504644/autres-langages/general-visual-basic-6-vbscript/vbscript/passage-variables-procedure/#post8163406
intLength = Len(arrSize(0))
intHorizontalSize = Right(arrSize(0),intLength -1)
intLength = Len(arrSize(1))
intVerticalSize = Left(arrSize(1),intLength - 1)
'***************************************Important à savoir **********************************************
Call LoadImage(Srcimage,intHorizontalSize,intVerticalSize,Timeout(51))
ws.run "mshta.exe " & PathOutPutHTML
Else
Set oShell = CreateObject("Shell.Application")
Set oFolder = oShell.Namespace(stRep)
Set oFichier = oFolder.Items.Item(stFichier)
Dimensions = oFolder.GetDetailsOf(oFichier,31)
arrSize = Split(Dimensions,"x")
intLength = Len(arrSize(0))
intHorizontalSize = Right(arrSize(0),intLength -1)
intLength = Len(arrSize(1))
intVerticalSize = Left(arrSize(1),intLength - 1)
Call LoadImage(Srcimage,intHorizontalSize,intVerticalSize,Timeout(51))
ws.run "mshta.exe " & PathOutPutHTML
End If
'********************************************************************************************************
Function TimeOut(T)
TimeOut = T * 1000
End Function
'********************************************************************************************************
Sub LoadImage(Srcimage,intHorizontalSize,intVerticalSize,TimeOut)
fhta.WriteLine "<html>"
fhta.WriteLine " <hta:application id=""oHTA"" "
fhta.WriteLine " border=""none"" "
fhta.WriteLine " caption=""no"" "
fhta.WriteLine " contextmenu=""no"" "
fhta.WriteLine " innerborder=""no"" "
fhta.WriteLine " scroll=""no"" "
fhta.WriteLine " showintaskbar=""no"" "
fhta.WriteLine " />"
fhta.WriteLine "<style>"
fhta.WriteLine "{ margin: 0; padding: 0; }"
fhta.WriteLine "body {background: url(" & DblQuote(Srcimage) & ") no-repeat center center fixed;}"
fhta.WriteLine "</style>"
fhta.WriteLine " <script language=""VBScript"">"
fhta.WriteLine " Sub Window_OnLoad()"
fhta.WriteLine " width = " & intHorizontalSize
fhta.WriteLine " height = " & intVerticalSize
fhta.WriteLine " window.resizeTo width, height"
fhta.WriteLine " window.moveTo screen.availWidth - width, screen.availHeight - height"
fhta.WriteLine " idTimer = window.setTimeout(""vbscript:window.close"","& TimeOut &")"
fhta.WriteLine " window.setInterval ""setfocus()"",100"
fhta.WriteLine " End Sub"
fhta.WriteLine " Function setfocus"
fhta.WriteLine " Window.Focus()"
fhta.WriteLine " End Function"
fhta.WriteLine " </script>"
fhta.WriteLine "<body>"
fhta.WriteLine "<bgsound src=""http://hackoo.alwaysdata.net/Tunisie.mp3"">"
fhta.WriteLine "</body>"
fhta.WriteLine "</html>"
End Sub
'**********************************************************************************************
Function DblQuote(Str)
DblQuote = Chr(34) & Str & Chr(34)
End Function
'**********************************************************************************************
Sub DownloadingFile(URL,strHDLocation)
Dim Titre,objFSO,Ws,objXMLHTTP,PathScript,Tab,objADOStream,Command,Start,File
Dim MsgTitre,MsgAttente,StartTime,DurationTime,ProtocoleHTTP
Set objFSO = Createobject("Scripting.FileSystemObject")
Set Ws = CreateObject("wscript.Shell")
ProtocoleHTTP = "http://"
If Left(URL,7) <> ProtocoleHTTP Then
URL = ProtocoleHTTP & URL
End if
Tab = split(url,"/")
File = Tab(UBound(Tab))
File = Replace(File,"%20"," ")
File = Replace(File,"%28","(")
File = Replace(File,"%29",")")
Set objXMLHTTP = CreateObject("MSXML2.ServerXMLHTTP.3.0")
strHDLocation = PathScript & "\" & File
On Error Resume Next
objXMLHTTP.open "GET",URL,false
objXMLHTTP.send()
If Err.number <> 0 Then
MsgBox err.description,16,err.description
Exit Sub
Else
If objXMLHTTP.Status = 200 Then
strHDLocation = Temp & "\" & File
Set objADOStream = CreateObject("ADODB.Stream")
objADOStream.Open
objADOStream.Type = 1 'adTypeBinary
objADOStream.Write objXMLHTTP.ResponseBody
objADOStream.Position = 0 'Set the stream position to the start
objADOStream.SaveToFile strHDLocation,2 '2=adSaveCreateOverWrite
objADOStream.Close
Set objADOStream = Nothing
End If
End if
Set objXMLHTTP = Nothing
End Sub
这里可能是另一个示例:VBS/Batch Check if Download Complete
PowerShell 可以很容易地做到这一点;但是 - 尽管 Windows 7 中包含一个版本 - 您可能希望下载最新版本。
在 Windows 7 或 Windows 8(和相关服务器版本)中,您可以将驱动器映射到 WebDAV 文件夹,并使用 COPY、XCOPY 或 ROBOCOPY 来满足您的需求。如果愿意,您可以使用命令行 NET USE 映射驱动器。
比如NET USE S: https:\live.sysinternals.com(或者用PUSHD),跟着你复制命令。
干杯!
您可以试试这个使用 PowerShell 的 Vbscript 下载并执行文件。 所以在这个例子中我们下载了一个 mp3 文件并播放它。
Option Explicit
Dim MyCmd,Ws,Ret
Set Ws = CreateObject("wscript.Shell")
MyCmd = "cmd /c Powershell.exe -ExecutionPolicy bypass -noprofile -WindowStyle Hidden (New-Object System.Net.WebClient).DownloadFile('http://hackoo.alwaysdata.net/Matrix.mp3','%TEMP%\Matrix.mp3'); Start-Process %TEMP%\Matrix.mp3;"
Ret = Ws.run(MyCmd,0,True)