Page 1 of 2

Simplest Way To Download Files?

Posted: 10 Jul 2011 13:14
by nitt
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.

Re: Simplest Way To Download Files?

Posted: 10 Jul 2011 15:47
by aGerman
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

Re: Simplest Way To Download Files?

Posted: 10 Jul 2011 18:38
by nitt
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.

Re: Simplest Way To Download Files?

Posted: 11 Jul 2011 06:45
by Ed Dyreen
'
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]]] )

Re: Simplest Way To Download Files?

Posted: 11 Jul 2011 10:29
by aGerman
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

Re: Simplest Way To Download Files?

Posted: 11 Jul 2011 16:09
by Ed Dyreen
'@SomeGerman
What is cURL ? It isn't native with my XP.

Re: Simplest Way To Download Files?

Posted: 11 Jul 2011 16:31
by 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.

Regards
aGerman

Re: Simplest Way To Download Files?

Posted: 11 Jul 2011 16:56
by Cleptography
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.

Re: Simplest Way To Download Files?

Posted: 11 Jul 2011 17:12
by aGerman
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

Re: Simplest Way To Download Files?

Posted: 11 Jul 2011 17:14
by Cleptography
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.

Re: Simplest Way To Download Files?

Posted: 12 Jul 2011 07:11
by Ed Dyreen
'@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%...

Re: Simplest Way To Download Files?

Posted: 12 Jul 2011 07:21
by Cleptography
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."

Re: Simplest Way To Download Files?

Posted: 12 Jul 2011 11:10
by nitt
No one really helped find a simpler way (using native commands). :(

Re: Simplest Way To Download Files?

Posted: 12 Jul 2011 11:25
by Cleptography
@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.

Re: Simplest Way To Download Files?

Posted: 12 Jul 2011 11:32
by aGerman
nitt wrote:(using native commands)

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

Regards
aGerman