2012-07-15 75 views
1

我發現這個有用的vbscript on the web自動文件下載。vbScript自動文件下載擴展

function download(sFileURL, sLocation) 

'create xmlhttp object 
Set objXMLHTTP = CreateObject("MSXML2.XMLHTTP") 

'get the remote file 
objXMLHTTP.open "GET", sFileURL, false 

'send the request 
objXMLHTTP.send() 

'wait until the data has downloaded successfully 
do until objXMLHTTP.Status = 200 : wscript.sleep(1000) : loop 

'if the data has downloaded sucessfully 
If objXMLHTTP.Status = 200 Then 

     'create binary stream object 
    Set objADOStream = CreateObject("ADODB.Stream") 
    objADOStream.Open 

     'adTypeBinary 
    objADOStream.Type = 1 
    objADOStream.Write objXMLHTTP.ResponseBody 

     'Set the stream position to the start 
    objADOStream.Position = 0  

     'create file system object to allow the script to check for an existing file 
     Set objFSO = Createobject("Scripting.FileSystemObject") 

     'check if the file exists, if it exists then delete it 
    If objFSO.Fileexists(sLocation) Then objFSO.DeleteFile sLocation 

     'destroy file system object 
    Set objFSO = Nothing 

     'save the ado stream to a file 
    objADOStream.SaveToFile sLocation 

     'close the ado stream 
    objADOStream.Close 

    'destroy the ado stream object 
    Set objADOStream = Nothing 

'end object downloaded successfully 
End if 

'destroy xml http object 
Set objXMLHTTP = Nothing 

End function 

download "http://remote-location-of-file", "C:\name-of-file-and-extension" 

有沒有辦法強制它解析的網址和例如下載* .exe文件在某些​​網站的位置?像這樣:

URL: http://examplesite.com/files/ 

file0001.exe 

因爲我有一個URL,其中包含一個名稱在更改期間更改的文件。它有.exe擴展名。

我同時啓用了http和ftp協議,ftp沒有認證(免費)。

回答

0

這隻有在該網站上的文件瀏覽功能被啓用時纔有可能,而且這種情況很少。您將需要一些URL列出文件的名稱以解析文件名。你最好使用像wget http://users.ugent.be/~bpuype/wget/這樣的工具,它有這樣的選項。此外,對於這樣的操作,vbscript並不是一個好的選擇,如果你從頭開始,你會比如Ruby更好。

如果您發佈有問題的網址,也許我可以進一步幫助您。