Page 1 of 2
cscript.exe check is return is + or -
Posted: 12 Aug 2013 14:46
by Adrianvdh
Hello everyone...
I am using cscript.exe to use a *.vbs file.
In the vbs file is this...
Code: Select all
strFileURL = "http://example.com/data/file.ext"
strHDLocation = "C:\file.ext"
Set objXMLHTTP = CreateObject("MSXML2.XMLHTTP")
objXMLHTTP.open "GET", strFileURL, false
objXMLHTTP.send()
If objXMLHTTP.Status = 200 Then
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
Set objFSO = Createobject("Scripting.FileSystemObject")
If objFSO.Fileexists(strHDLocation) Then objFSO.DeleteFile strHDLocation
Set objFSO = Nothing
objADOStream.SaveToFile strHDLocation
objADOStream.Close
Set objADOStream = Nothing
End if
Set objXMLHTTP = Nothing
This vbs of course downloads a file with cmd with out CURL or wget or ftp.exe

So how do I measure the error level?
How would I check if the download was successful or not?
Any help would be great and thanks for your time

.
Re: cscript.exe check is return is + or -
Posted: 13 Aug 2013 06:24
by penpen
I'm not sure, what you want to do:
1) If you want to set an errorlevel if objXMLHTTP.Status is not 200, then just add these lines prior to End if:
2) If you want to set the errorlevel to 1 if a file is created (else it is 0 even on errors), just add this to the end (assumed that this file doesn't exist on start):
Code: Select all
Set objFileSystem = CreateObject("Scripting.FileSystemObject")
If objFileSystem.FileExists(strHDLocation) Then
WScript.Quit(1)
End If
3) If you want to do some simple error checking within the script, you may do something like this:
Code: Select all
Option Explicit
On Error resume next
Err.Clear
Dim objString : objString = "MSXML200000.XMLHTTP"
Set objXMLHTTP = CreateObject(objString)
If Err.Number <> 0 Then
WScript.Echo("Error: Could not create object: '" & objString & "'.")
WScript.Quit(1)
End If
On Error goto 0
4) If you want to do excessive error checking within the script, you may do something like this:
Code: Select all
Option Explicit
Class Inet
Dim objXMLHTTP
Dim objADOStream
Dim objFSO
Dim Called
Function Download (strFileURL, strHDLocation)
Called = 0
Download = 0
Set objXMLHTTP = CreateObject("MSXML2.XMLHTTP")
objXMLHTTP.open "GET", strFileURL, false
objXMLHTTP.send()
If objXMLHTTP.Status = 200 Then
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
Set objFSO = Createobject("Scripting.FileSystemObject")
If objFSO.Fileexists(strHDLocation) Then objFSO.DeleteFile strHDLocation
objADOStream.SaveToFile strHDLocation
objADOStream.Close
Set objADOStream = Nothing
If objFSO.Fileexists(strHDLocation) Then Download = 1
Set objFSO = Nothing
End if
Set objXMLHTTP = Nothing
End Function
Private Sub CatchErr_Download
Select Case Err.Number
Case 0
Case 429
Dim obj : obj = Mid(Err.Description, InStr (Err.Description, "'") + 1, Len(Err.Description) - InStr (Err.Description, "'") - 1)
If StrComp(obj,"ADODB.Stream") Then
Set objXMLHTTP = Nothing
ElseIf StrComp(obj,"Scripting.FileSystemObject") Then
On Error Resume Next
objADOStream.Close
On Error goto 0
Set objADOStream = Nothing
Set objXMLHTTP = Nothing
Else
WScript.Echo "No exception handling on failed creation of the activeX object: '" & obj & "'."
End If
Case Else WScript.Echo "Unhandled error (" & Err.Number & ", " & Err.Source & "," & Err.Description & ") occurred."
End Select
Err.Clear
End Sub
Private Sub Class_Terminate
Select Case Err.Number
Case 0 CatchErr_Download
Case Else WScript.Echo "Unknown function called."
End Select
End Sub
End Class
Dim objInet : Set objInet = New Inet
Select Case objInet.Download("http://example.com/data/file.ext", "C:\Temp\batch\test\file.ext")
Case 1
WScript.Echo "File downloaded."
Case Else
WScript.Echo "No File downloaded."
End Select
Set objInet = Nothing
This is an exemplary error handling of two errors, not of all.
penpen
Re: cscript.exe check is return is + or -
Posted: 13 Aug 2013 07:42
by Adrianvdh
Thanks man.
Sorry, what I wanted to do is just in the batch or vbs file show the errorlevel, just some basic stuff. Something like you said, "If the file does not start." or the file did not download. Then the batch file would show the errorlevel.
Thanks again

P.S. OR if you know how to download a file with explorer.exe or without wget or curl. (Something that does not need 3rd party software.) Just a simple script to download a file with troubleshooting
Re: cscript.exe check is return is + or -
Posted: 13 Aug 2013 08:32
by brinda
Re: cscript.exe check is return is + or -
Posted: 13 Aug 2013 12:25
by Adrianvdh
Thanks man

. So I guess I can throw my vbs away? (Don't use it?)
Also, if I delete the file from the web server, and run the vb script then the vbs code will be able to copy it from a temp process and put it in the download folder?
So how would I prevent that?
Re: cscript.exe check is return is + or -
Posted: 13 Aug 2013 12:58
by penpen
@Adrianvdh: So this seems to be, what you are looking for:
Code: Select all
' download.vbs
Option Explicit
If WScript.Arguments.Unnamed.Count <> 2 Then
WScript.Echo "usage: cscript.exe download.vbs //B //E:VBS //Nologo ""inetAdress"" ""saveFile"""
WScript.Quit(0)
End If
Dim strFileURL : strFileURL = WScript.Arguments.Unnamed.Item(0)
Dim strHDLocation : strHDLocation = WScript.Arguments.Unnamed.Item(1)
Dim objXMLHTTP : set objXMLHTTP = CreateObject("MSXML2.XMLHTTP")
objXMLHTTP.open "GET", strFileURL, false
objXMLHTTP.send()
If objXMLHTTP.Status = 200 Then
Dim objADOStream : 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
Dim objFSO : Set objFSO = Createobject("Scripting.FileSystemObject")
If objFSO.Fileexists(strHDLocation) Then objFSO.DeleteFile strHDLocation
objADOStream.SaveToFile strHDLocation
objADOStream.Close
Set objADOStream = Nothing
If objFSO.Fileexists(strHDLocation) Then
Set objFSO = Nothing
Set objXMLHTTP = Nothing
WScript.Quit(1)
End If
Set objFSO = Nothing
End if
Set objXMLHTTP = Nothing
Code: Select all
:: download.bat
cscript.exe download.vbs //B //E:VBS //Nologo "http://example.com/data/file.ext" "C:\file.ext"
if %errorlevel% == 1 (
echo success: File downloaded.
) else (
echo failed: File not downloaded.
)
@brinda: The lower vbs is that of this opening post, and the other is nearly the same without checking for existing file.
Neither of them sets the errorlevel, so the task stays.
penpen
Edit: Corrected some typos, seems i have tipped the insert key some time:
ownload.vbs -> download.vbs
Dim objXMLHTTP : XMLHTTP -> Dim objXMLHTTP : set objXMLHTTP
Edit2: Added "Option Explicit" and "Dim objFSO : "
Re: cscript.exe check is return is + or -
Posted: 13 Aug 2013 13:45
by Adrianvdh
Hello penpen, I used your script and tried to download the file from the web server, but the errorlevel always returns 0, the URL is valid, etc. I have triple check on it, it just won't work?
Re: cscript.exe check is return is + or -
Posted: 13 Aug 2013 15:03
by penpen
I've tricked myself out with my keyboard...
But it should work now...
penpen
Re: cscript.exe check is return is + or -
Posted: 14 Aug 2013 08:16
by Adrianvdh
Ok thanks I will test it now

!
Re: cscript.exe check is return is + or -
Posted: 14 Aug 2013 08:23
by Adrianvdh
OK I tested it and still does not work,. What now?

Re: cscript.exe check is return is + or -
Posted: 14 Aug 2013 08:52
by penpen
It works with :
http://electronicsclub.info/p_simpletester.htm,
http://electronicsclub.info/docs/simpletester.pdf,
... (urls use per post is limited to 2, so i haven't posted all)
I've found nothing, that doesn't work.
So it seems it is an issue with the adress of the file you want to download from,
or you have left out the protocol specifier "http://".
Maybe you link the adress that doesn't work, if it is a public accessible.
penpen
Re: cscript.exe check is return is + or -
Posted: 14 Aug 2013 09:34
by carlos
i think that always is a bad idea delete a file without ask to user:
Code: Select all
If objFSO.Fileexists(strHDLocation) Then objFSO.DeleteFile strHDLocation
maybe you can add (number) to a filename that exists:
example:
exists.txt
to
exists(1).txt
also, maybe the download fails, not because a bad url, but for filesystem permissions, i remember for example that using windows vista / seven in some security configuration is not possible write to C:\ without authorize the action as administrator.
Re: cscript.exe check is return is + or -
Posted: 14 Aug 2013 10:00
by penpen
carlos wrote:i think that always is a bad idea delete a file without ask to user:
Code: Select all
If objFSO.Fileexists(strHDLocation) Then objFSO.DeleteFile strHDLocation
I disagree with that, and believe it depends on the situation if the user should be asked for that.
For example it would be very soul-destroying if you have to agree to all file deletion of Temporary Internet files, when surfing the internet (all htmls, pictures, scripts, ...).
And in this case, where you call the program with the saveFile explicitely, the user interaction is not needed,
as the user may check for existence of the file prior to use this VBS:
Code: Select all
cscript.exe download.vbs //E:VBS "inetAdress" "saveFile"
penpen
Re: cscript.exe check is return is + or -
Posted: 14 Aug 2013 10:36
by carlos
penpen wrote:carlos wrote:i think that always is a bad idea delete a file without ask to user:
Code: Select all
If objFSO.Fileexists(strHDLocation) Then objFSO.DeleteFile strHDLocation
I disagree with that, and believe it depends on the situation if the user should be asked for that.
For example it would be very soul-destroying if you have to agree to all file deletion of Temporary Internet files, when surfing the internet (all htmls, pictures, scripts, ...).
And in this case, where you call the program with the saveFile explicitely, the user interaction is not needed,
as the user may check for existence of the file prior to use this VBS:
Code: Select all
cscript.exe download.vbs //E:VBS "inetAdress" "saveFile"
penpen
but for example in the copy command if you not specify the /y parameter it ask to user. in summary, is not good idea delete a file without a authorization of the user: prea-uthorization (like /y parameter) or after-authorization (ask). a file is a file and is important.
Re: cscript.exe check is return is + or -
Posted: 14 Aug 2013 15:29
by penpen
Ok, then here is a version with /Y option to overwrite the file without asking,
and if /Y is not set it asks you if you want to overwrite an existing file.
Additionally it works on cscript.exe and wscript.exe:
Code: Select all
' download.vbs
Option Explicit
If WScript.Arguments.Unnamed.Count <> 2 Then
WScript.Echo "usage: cscript.exe download.vbs //B //E:VBS //Nologo ""inetAdress"" ""saveFile"" [/Y|/y]"
WScript.Quit(0)
ElseIf WScript.Arguments.Named.Count = 1 And Not WScript.Arguments.Named.Exists("Y") Then
WScript.Echo "usage: cscript.exe download.vbs //B //E:VBS //Nologo ""inetAdress"" ""saveFile"" [/Y|/y]"
WScript.Quit(0)
End If
Dim strFileURL : strFileURL = WScript.Arguments.Unnamed.Item(0)
Dim strHDLocation : strHDLocation = WScript.Arguments.Unnamed.Item(1)
Dim objFSO : Set objFSO = Createobject("Scripting.FileSystemObject")
If objFSO.Fileexists(strHDLocation) Then
If (WScript.Arguments.Named.Count = 0) Then
Dim inputText : inputText = "Overwrite existing file? [YN]: "
Dim strInput : strInput = ""
Dim upcaseFullName : upcaseFullName = UCase (WScript.FullName)
Dim cScriptPos : cScriptPos = InStr (upcaseFullName, "CSCRIPT")
Dim wScriptPos : wScriptPos = InStr (upcaseFullName, "WSCRIPT")
If 0 = cScriptPos Then cScriptPos = cScriptPos + Len (upcaseFullName)
If 0 = wScriptPos Then wScriptPos = wScriptPos + Len (upcaseFullName)
While 0 <> StrComp (strInput,"Y")
If cScriptPos < wScriptPos Then
WScript.StdOut.Write inputText
strInput = UCase (WScript.StdIn.ReadLine)
Else
strInput = UCase (InputBox (myPrompt))
End If
If 0 = StrComp(strInput,"N") Then
Set objFSO = Nothing
WScript.Echo "File not downloaded."
WScript.Quit(0)
End If
Wend
End If
objFSO.DeleteFile strHDLocation
End If
Dim objXMLHTTP : set objXMLHTTP = CreateObject("MSXML2.XMLHTTP")
objXMLHTTP.open "GET", strFileURL, false
objXMLHTTP.send()
If objXMLHTTP.Status = 200 Then
Dim objADOStream : 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
objADOStream.Close
Set objADOStream = Nothing
If objFSO.Fileexists(strHDLocation) Then
Set objXMLHTTP = Nothing
Set objFSO = Nothing
WScript.Quit(1)
End If
End if
Set objXMLHTTP = Nothing
Set objFSO = Nothing
And a batch file to an existing source in the inet with errorlevel check:
Code: Select all
:: download.bat
@echo off
cls
cscript.exe download.vbs //B //E:VBS //Nologo "http://electronicsclub.info/docs/simpletester.pdf" "Z:\simpletester.pdf" /Y
if %errorlevel% == 1 (
echo success: File downloaded.
) else (
echo failed: File not downloaded.
)
Hope all wishes are fulfilled now

.
penpen