Simplest Way To Download Files?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
nitt
Posts: 218
Joined: 22 Apr 2011 02:43

Simplest Way To Download Files?

#1 Post by nitt » 10 Jul 2011 13:14

I'm trying to figure out the SIMPLEST/EASIEST way. So, this is the only thing I could think of:

Code: Select all

@echo off
set url=http://cmdr.zxq.net/debug
set file=debug.bat
(
echo set fso = createobject^("scripting.filesystemobject"^)
echo set file = fso.createtextfile^("%file%"^)
echo set http = createobject^("winhttp.winhttprequest.5.1"^)
echo x = http.open^("get","%url%"^)
echo http.send
echo for i = 1 to lenb^(http.responsebody^)
echo file.write^(chr^(ascb^(midb^(http.responsebody,i,1^)^)^)^)
echo next
)>download.vbs & download & del download.vbs


So basically it just uses an VBScript file. This takes a while to write out, so I wouldn't consider it very easy, but still is simple. This can't download like videos, mp3's, or any other binary files. But it can download any ASCII based file, such as webpages, text files, batch files, etc...

I want this to be something universal that would work on all computers, meaning I don't want to have to download any software/executables that will do this for me. I want to do this with the stuff that's already built-in to the computer.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Simplest Way To Download Files?

#2 Post by aGerman » 10 Jul 2011 15:47

Good idea nitt, but it wastes a lot of time to iterate over the entire stream (more than 2 min for a file of approx. 200 KB on my computer). You could use an ActiveX Data Object instead.

filedownload.vbs

Code: Select all

If Not WScript.Arguments.Count = 2 Then
  WScript.Echo vbCrLf & "  Usage:" & vbCrLf & "filedownload.vbs SourceURL DestinationFileName" & vbCrLf
  WScript.Quit 2
End If

strWebSource = WScript.Arguments(0)
strLocalDestination = WScript.Arguments(1)

On Error Resume Next
Set objXMLHTTP = CreateObject("MSXML2.XMLHTTP")
For i = 1 To 10
  Err.Clear
  objXMLHTTP.Open "GET", strWebSource, False
  objXMLHTTP.Send()
  If (Not Err) And (objXMLHTTP.ReadyState = 4) Then Exit For
  If i = 10 Then WScript.Quit 1
  WScript.Sleep 500
Next

Set objADOStream = CreateObject("ADODB.Stream")
objADOStream.Open
objADOStream.Type = 1
objADOStream.Write objXMLHTTP.ResponseBody
objADOStream.Position = 0
objADOStream.SaveToFile strLocalDestination, 2
objADOStream.Close
Set objADOStream = Nothing
Set objXMLHTTP = Nothing
If Err Then WScript.Quit 1


callFiledownload.bat

Code: Select all

@echo off &setlocal
cscript //nologo filedownload.vbs "http://cmdr.zxq.net/debug" "debub.bat"
echo %errorlevel%
pause


The script will try 10 times to get the file stream.
Also it returns an ErrorLevel:
2 wrong syntax
1 something failed
0 success

Regards
aGerman

nitt
Posts: 218
Joined: 22 Apr 2011 02:43

Re: Simplest Way To Download Files?

#3 Post by nitt » 10 Jul 2011 18:38

aGerman wrote:Good idea nitt, but it wastes a lot of time to iterate over the entire stream (more than 2 min for a file of approx. 200 KB on my computer). You could use an ActiveX Data Object instead.

filedownload.vbs

Code: Select all

If Not WScript.Arguments.Count = 2 Then
  WScript.Echo vbCrLf & "  Usage:" & vbCrLf & "filedownload.vbs SourceURL DestinationFileName" & vbCrLf
  WScript.Quit 2
End If

strWebSource = WScript.Arguments(0)
strLocalDestination = WScript.Arguments(1)

On Error Resume Next
Set objXMLHTTP = CreateObject("MSXML2.XMLHTTP")
For i = 1 To 10
  Err.Clear
  objXMLHTTP.Open "GET", strWebSource, False
  objXMLHTTP.Send()
  If (Not Err) And (objXMLHTTP.ReadyState = 4) Then Exit For
  If i = 10 Then WScript.Quit 1
  WScript.Sleep 500
Next

Set objADOStream = CreateObject("ADODB.Stream")
objADOStream.Open
objADOStream.Type = 1
objADOStream.Write objXMLHTTP.ResponseBody
objADOStream.Position = 0
objADOStream.SaveToFile strLocalDestination, 2
objADOStream.Close
Set objADOStream = Nothing
Set objXMLHTTP = Nothing
If Err Then WScript.Quit 1


callFiledownload.bat

Code: Select all

@echo off &setlocal
cscript //nologo filedownload.vbs "http://cmdr.zxq.net/debug" "debub.bat"
echo %errorlevel%
pause


The script will try 10 times to get the file stream.
Also it returns an ErrorLevel:
2 wrong syntax
1 something failed
0 success

Regards
aGerman


Um, that's a faster way but it's actually more complex. I was looking for a simpler way.
Also, I think you know more VBScript than me because I have never heard of those other two objects. The "MSXML2.XMLHTTP" and "ADODB.Stream" objects. I may look those up, but I'm a little sick right now and I don't feel up to learning anything.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Simplest Way To Download Files?

#4 Post by Ed Dyreen » 11 Jul 2011 06:45

'
As this is a dos batch forum, a batch only solution would be desireable but is missing

If we are escaping to other languagues ?, Why not simply use the one liner autoIT download command ?
This can even be in the autocom app @cleo wrote.

Code: Select all

InetGet ( "URL" [,"filename" [, reload [, background]]] )

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Simplest Way To Download Files?

#5 Post by aGerman » 11 Jul 2011 10:29

nitt wrote:I have never heard of those other two objects. The "MSXML2.XMLHTTP" and "ADODB.Stream" objects.

They are well documented in the msdn. The reason why I used the XMLHttp object is that it supports the ReadyState property which I used to determine whether the stream was downloaded successfully and completely.

Ed Dyreen wrote:As this is a dos batch forum, a batch only solution would be desireable but is missing

Use cURL.

Ed Dyreen wrote:If we are escaping to other languagues ?, Why not simply use the one liner autoIT download command ?

The reason is ...
nitt wrote:I want this to be something universal that would work on all computers, meaning I don't want to have to download any software/executables that will do this for me. I want to do this with the stuff that's already built-in to the computer.

... for people like me who is strictly forbidden to set up or use additional software at work (or even at school). That's also the reason why I first try to find a solution in VBS or JS if no "native" batch commands are available (where "native" means that they come with Windows).

Regards
aGerman

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Simplest Way To Download Files?

#6 Post by Ed Dyreen » 11 Jul 2011 16:09

'@SomeGerman
What is cURL ? It isn't native with my XP.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Simplest Way To Download Files?

#7 Post by aGerman » 11 Jul 2011 16:31

That was the meaning of my post above :wink: cURL is a 3rd party command line tool (I don't use it and I already explained why). But if you're looking for a command line solution cURL is a powerful tool. Have a look at the manual.

Regards
aGerman

Cleptography
Posts: 287
Joined: 16 Mar 2011 19:17
Location: scriptingpros.com
Contact:

Re: Simplest Way To Download Files?

#8 Post by Cleptography » 11 Jul 2011 16:56

Simplest way to download files is what is the simplest to you, could be anything you want, curl, wget, vbscript etc...etc... There is no right or wrong to this question and what works for some may not always work for others. If you are talking native then yes vbscript, or newer flavors of windows powershell, or if you can use 3rd party tools, curl wget, autoit, python, ruby, perl, whatever you would like. In some cases though certain methods will not allow you to send automated responses to servers, such is the case with using the autoit one liner. The one liner actually needs to be modified a little more to bypass the authentication for certain servers. In comes a tool like wget which seems to take care of this issue. Use what works for you everyone's needs are different and no one answer is the correct answer.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Simplest Way To Download Files?

#9 Post by aGerman » 11 Jul 2011 17:12

Cleptography wrote:Use what works for you everyone's needs are different and no one answer is the correct answer.

I agree.
I explained my reasons and talked about what nitt wrote in the end of his first post. Latest you have to decide yourself ... there are a lot of possibilities.

Regards
aGerman

Cleptography
Posts: 287
Joined: 16 Mar 2011 19:17
Location: scriptingpros.com
Contact:

Re: Simplest Way To Download Files?

#10 Post by Cleptography » 11 Jul 2011 17:14

aGerman wrote:
Cleptography wrote:Use what works for you everyone's needs are different and no one answer is the correct answer.

I agree.
I explained my reasons and talked about what nitt wrote in the end of his first post. Latest you have to decide yourself ... there are a lot of possibilities.

Regards
aGerman


Yes we rewrite wheels not because we have to but because we can, and to show that there is always more than one way out.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Simplest Way To Download Files?

#11 Post by Ed Dyreen » 12 Jul 2011 07:11

'@aGerman
That was the meaning of my post above :wink: cURL is a 3rd party command line tool (I don't use it and I already explained why). But if you're looking for a command line solution cURL is a powerful tool. Have a look at the manual.
I just stick to AutoIT for now, it accepts parameters from commandline and can interpret and execute and return to console.

I see no need for using cURL if I can do the same with a call to:
Au3CMD.EXE InetGet ( "URL" [,"filename" [, reload [, background]]] ) :P
if %errorlevel%...

Cleptography
Posts: 287
Joined: 16 Mar 2011 19:17
Location: scriptingpros.com
Contact:

Re: Simplest Way To Download Files?

#12 Post by Cleptography » 12 Jul 2011 07:21

Yes Edward but as explained autoit will not always download a page or the file requested from some servers do to their auto response policies. This is why I redid autocom project because certain commands did not always produce the intended results. I will upload a newer version here soon but the projects resides on a machine with no network and involves me having to burn some cds in order to transfer files from one machine to another. Autocom is outdated and rather useless, well at least the version posted. :roll: :lol: :wink: 8)

QUOTE:
"I'm a little t-pot stupid as hell, here is my buttons here is my doubt. When you tip me over here me shout, I am a moron, I am a trout."

nitt
Posts: 218
Joined: 22 Apr 2011 02:43

Re: Simplest Way To Download Files?

#13 Post by nitt » 12 Jul 2011 11:10

No one really helped find a simpler way (using native commands). :(

Cleptography
Posts: 287
Joined: 16 Mar 2011 19:17
Location: scriptingpros.com
Contact:

Re: Simplest Way To Download Files?

#14 Post by Cleptography » 12 Jul 2011 11:25

@nitt
This has been solved, you must define what simpler is to you.
Some might say a one liner in powershell which is native to newer flavors of Windows is the easier approach. One might say a vbscript is easier for them because they know vbscript and not powershell. There is no answer to your direct question.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Simplest Way To Download Files?

#15 Post by aGerman » 12 Jul 2011 11:32

nitt wrote:(using native commands)

Unfortunately no way (if the meaning of commands is batch commands).

Regards
aGerman

Post Reply