2006/07/07

IEを使って指定したURLのファイルをダウンロードしようとしても、IEで開かれてしまったりしてなかなかうまくいきません。そこでこんなスクリプトを作ってみました。クリップボードに格納されたURLのファイルをダウンロードするスクリプトです。PowerShellではなくWSHのスクリプトですが、たまにはこんなのも良いでしょう。

Set Ie = WScript.CreateObject("InternetExplorer.Application")
Ie.Navigate "about:blank"
Do While Ie.Busy And IE.readyState=4
    Wscript.Sleep 100
Loop
'クリップボードの文字列を取得。
sClipBoard = Ie.Document.parentWindow.clipboardData.getData("text")  
Ie.Quit 
If Left(sClipBoard,5)="http:" Then
    sDest = Mid(sClipBoard,InStrRev(sClipBoard,"/")+1)
    Const adTypeBinary = 1
    Const adSaveCreateNotExist = 1
    Const adSaveCreateOverWrite = 2
    Set oHTTP = WScript.CreateObject("Msxml2.XMLHTTP")
    oHTTP.Open "GET", sClipBoard, False
    oHTTP.Send
    Set Stream = WScript.CreateObject("Adodb.Stream")
    Stream.Type = adTypeBinary
    Stream.Open
    Stream.Write oHTTP.responseBody
    Stream.Savetofile sDest, adSaveCreateOverWrite
    msgbox "done"
End If
元記事:http://blogs.wankuma.com/mutaguchi/archive/2006/07/07/31826.aspx

古い記事へ | 新しい記事へ


プライバシーポリシー

Twitter

Books