2012-05-26 19 views
0

我使用此代碼下載鏈接,我想將文件保存在%SystemRoot%\system32但與%SystemRoot%更換C:\,則VBS無法運行。如何在VBS中使用%SYSTEMROOT%?

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 : wcript.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" 

download "http://demo.com/test.gif", "C:\test.gif" **OK** 
download "http://demo.com/test.gif", "%SystemRoot%\system32\test.gif" **Faild** 

回答

5

你需要在VBS變量來讀取環境變量。例如:

Dim mySystemRoot 
Dim myPath 
Dim wshShell 
Set wshShell = CreateObject("WScript.Shell") 
mySystemRoot = wshShell.ExpandEnvironmentStrings("%SystemRoot%") 
wshShell = Nothing 
download "http://demo.com/test.gif", myPath & "\test.gif" 

然後使用mySystemRoot變量來形成文件路徑。

+0

我在**結束函數**之後添加您的代碼,但我想我不會寫出該補丁的正確地址。 http://www.uploadimage.in/di-N24L.gif – Amirreza

+0

你做得不對。將更新我的答案。 –

+0

錯誤消息再次出現 **錯誤:未設置對象變量:'wshShell'** 第59行(wshShell = Nothing) – Amirreza