3

I'm asking that what other method to download a file from internet other than browsers and other softwares, i think is possible with CMD(command line), batch script ? To be more exactly to use a windows (let say 7) component to download a file without a dialog like vbs, hta, ..etc, is possible? I dont want to download wget, curl, telnet or other component This will work on windows 7? http://semitwist.com/articles/article/view/downloading-files-from-plain-batch-with-zero-dependencies

georgesz
  • 43
  • 1
  • 5

6 Answers6

2

Try this sample in 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\2 - width\2, screen.availHeight\2 - height\2"
    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

And may be another samples here : VBS/Batch Check if Download Complete

Community
  • 1
  • 1
Hackoo
  • 18,337
  • 3
  • 40
  • 70
0

Batch cannot natively download files.

To download files from the command line, you must use third party tools like cURL or wget.

SomethingDark
  • 13,229
  • 5
  • 50
  • 55
0

PowerShell will pretty easily do this; however - even though a version is included in Windows 7 - you'll probably want to download the latest version.

John Castleman
  • 1,552
  • 11
  • 12
0

Within Windows 7 or Windows 8 (and related server versions) you can map a drive to a WebDAV folder and use COPY, XCOPY or ROBOCOPY to your heart's content. You can map the drive using command-line NET USE if you prefer.

For example, NET USE S: https:\live.sysinternals.com (or use PUSHD), and follow up with you copy commands.

cheers!

Skatterbrainz
  • 1,077
  • 5
  • 23
  • 31
0

You can try this Vbscript that use PowerShell to download and execute the file. So in this example we download an mp3 file and we play it.

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)
Hackoo
  • 18,337
  • 3
  • 40
  • 70
-1

Linux:

wget http://somedomain.com/somefile.xxx

If you are using windows, download a exe named wget.exe(here) and you will be able to use the above code.

ch271828n
  • 15,854
  • 5
  • 53
  • 88