cscript.exe check is return is + or -

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Adrianvdh
Posts: 177
Joined: 16 May 2013 13:00

Re: cscript.exe check is return is + or -

#16 Post by Adrianvdh » 15 Aug 2013 14:48

Even with your address it does not work for me... It shows the error level to be 0 every time.
My address I am downloading from is valid because I can download it with wget.exe, but I don't want to use wget or curl because the user would have to download it to use the batch file I am working on. All I m downloading is a 1KB batch file that my batch file need to do a simple task.

Any help?

Code: Select all

set "tempdir=%AppData%\New Folder"
set "UpdateURL=http://electronicsclub.info/docs/simpletester.pdf"
set "updateinfofile=%tempdir%\UpdateInfo.bat
set "vbsDown=%tempdir%\vbsDown.vbs"

cscript.exe "%vbsDown%" //B //E:VBS //Nologo "%UpdateURL%" "%updateinfofile%"


Also how would I write this vbs to a vbs file in a batch file?

Code: Select all

(
echo If WScript.Arguments.Unnamed.Count <> 2 Then
echo    WScript.Echo
echo    WScript.Quit(0)
echo End If
echo(
echo Dim strFileURL : strFileURL = WScript.Arguments.Unnamed.Item(0)
echo Dim strHDLocation : strHDLocation = WScript.Arguments.Unnamed.Item(1)
echo Dim objXMLHTTP : XMLHTTP = CreateObject("MSXML2.XMLHTTP")
echo(
echo objXMLHTTP.open "GET", strFileURL, false
echo objXMLHTTP.send()
echo(
echo If objXMLHTTP.Status = 200 Then
echo    Dim objADOStream : Set objADOStream = CreateObject("ADODB.Stream")
echo    objADOStream.Open
echo    objADOStream.Type = 1
echo(
echo    objADOStream.Write objXMLHTTP.ResponseBody
echo    objADOStream.Position = 0
echo(
echo    Set objFSO = Createobject("Scripting.FileSystemObject")
echo    If objFSO.Fileexists(strHDLocation) Then objFSO.DeleteFile strHDLocation
echo(
echo    objADOStream.SaveToFile strHDLocation
echo    objADOStream.Close
echo    Set objADOStream = Nothing
echo(
echo    If objFSO.Fileexists(strHDLocation) Then
echo       Set objFSO = Nothing
echo       Set objXMLHTTP = Nothing
echo       WScript.Quit(1)
echo    End If
echo    Set objFSO = Nothing
echo End if
echo(
echo Set objXMLHTTP = Nothing )>>"%vbsDown%"


It says "< was unexpected?"

Thanks again :)

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: cscript.exe check is return is + or -

#17 Post by penpen » 15 Aug 2013 16:50

You only had forgotten to escape all special batch characters, in this case the <>)

Code: Select all

:: writeVbsDown.bat
@echo off
set "vbsDown=%tempdir%\vbsDown.vbs"

(
echo If WScript.Arguments.Unnamed.Count ^<^> 2 Then
:: nextline: vbs code changed
echo    WScript.Echo "usage: cscript.exe download.vbs //B //E:VBS //Nologo ""inetAdress"" ""saveFile"""
echo    WScript.Quit(0^)
echo End If
echo(
echo Dim strFileURL : strFileURL = WScript.Arguments.Unnamed.Item(0^)
echo Dim strHDLocation : strHDLocation = WScript.Arguments.Unnamed.Item(1^)
:: nextline: vbs code changed
echo Dim objXMLHTTP : set objXMLHTTP = CreateObject("MSXML2.XMLHTTP"^)
echo(
echo objXMLHTTP.open "GET", strFileURL, false
echo objXMLHTTP.send(^)
echo(
echo If objXMLHTTP.Status = 200 Then
echo    Dim objADOStream : Set objADOStream = CreateObject("ADODB.Stream"^)
echo    objADOStream.Open
echo    objADOStream.Type = 1
echo(
echo    objADOStream.Write objXMLHTTP.ResponseBody
echo    objADOStream.Position = 0
echo(
:: nextline: vbs code changed
echo    Dim objFSO : set objFSO = Createobject("Scripting.FileSystemObject"^)
echo    If objFSO.Fileexists(strHDLocation^) Then objFSO.DeleteFile strHDLocation
echo(
echo    objADOStream.SaveToFile strHDLocation
echo    objADOStream.Close
echo    Set objADOStream = Nothing
echo(
echo    If objFSO.Fileexists(strHDLocation^) Then
echo       Set objFSO = Nothing
echo       Set objXMLHTTP = Nothing
echo       WScript.Quit(1^)
echo    End If
echo    Set objFSO = Nothing
echo End if
echo(
echo Set objXMLHTTP = Nothing
) > "%vbsDown%"
Btw, you have used my old non working code (that i had replaced), additionally you had deleted some code.
I have 'corrected' these lines (only the second needs to be corrected), and marked them with ":: nextline: vbs code changed":
The first line could be completely deleted if you want no output there, the "WScript.Echo" should not stand alone.
The second line is critical, as this is why the code was not working.
The third line is not critical, but it is "good style" to declare a variable prior to using it.

If you are using the code this batch produces, hopefully the resulting vbs code will work as you expect.

penpen

Adrianvdh
Posts: 177
Joined: 16 May 2013 13:00

Re: cscript.exe check is return is + or -

#18 Post by Adrianvdh » 17 Aug 2013 04:53

Hi penpen...

The code you gave me... and I modified... Does not work still...

Code: Select all

(echo If WScript.Arguments.Unnamed.Count ^<^> 2 Then
echo    WScript.Quit(0^)
echo End If
echo(
echo Dim strFileURL : strFileURL = WScript.Arguments.Unnamed.Item(0^)
echo Dim strHDLocation : strHDLocation = WScript.Arguments.Unnamed.Item(1^)
echo Dim objXMLHTTP : set objXMLHTTP = CreateObject("MSXML2.XMLHTTP"^)
echo(
echo objXMLHTTP.open "GET", strFileURL, false
echo objXMLHTTP.send(^)
echo(
echo If objXMLHTTP.Status = 200 Then
echo    Dim objADOStream : Set objADOStream = CreateObject("ADODB.Stream"^)
echo    objADOStream.Open
echo    objADOStream.Type = 1
echo(
echo    objADOStream.Write objXMLHTTP.ResponseBody
echo    objADOStream.Position = 0
echo(
echo    Dim objFSO : set objFSO = Createobject("Scripting.FileSystemObject"^)
echo    If objFSO.Fileexists(strHDLocation^) Then objFSO.DeleteFile strHDLocation
echo(
echo    objADOStream.SaveToFile strHDLocation
echo    objADOStream.Close
echo    Set objADOStream = Nothing
echo(
echo    If objFSO.Fileexists(strHDLocation^) Then
echo       Set objFSO = Nothing
echo       Set objXMLHTTP = Nothing
echo       WScript.Quit(1^)
echo    End If
echo    Set objFSO = Nothing
echo End if
echo(
echo Set objXMLHTTP = Nothing )>>"%vbsDown%"


I have no understanding why it does not work, it always return the errorlevel as 0.

Any help would be great thanks :)

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: cscript.exe check is return is + or -

#19 Post by foxidrive » 17 Aug 2013 05:03

With the code below that is generated by your script, and launching this command from a cmd prompt:

cscript /nologo vbs.vbs http://www.google.com "D:\abc\g.htm"

I get this response: d:\abc\vbs.vbs(10, 1) msxml3.dll: Access is denied.

from this line: objXMLHTTP.send()

Do you get a different result? This is in Win8 without UAC enabled.

With this command it works:

cscript /nologo vbs.vbs http://www.nbc.com "D:\abc\g.htm"

so there is no error handling that gets past the case with google.


Code: Select all

If WScript.Arguments.Unnamed.Count <> 2 Then
   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

   objADOStream.Write objXMLHTTP.ResponseBody
   objADOStream.Position = 0

   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

Adrianvdh
Posts: 177
Joined: 16 May 2013 13:00

Re: cscript.exe check is return is + or -

#20 Post by Adrianvdh » 17 Aug 2013 07:57

Yes, it works. So it was the command the whole time, huh? xD
Must I remove the quotes between the URL? UAC is not an issue to me, the access is allowed because my batch file is running ad Admin and it has UAC checking for the purpose.

But, how would it determine is the download was successful?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: cscript.exe check is return is + or -

#21 Post by foxidrive » 17 Aug 2013 09:03

Adrianvdh wrote:Yes, it works. So it was the command the whole time, huh? xD
Must I remove the quotes between the URL?
But, how would it determine is the download was successful?


The script works, and also with the URL in quotes, but the send command seems to fail if the site uses secure transfers.

As for a successful download, are you downloading ZIP files?

What situation is likely to occur where the transfer fails?

Adrianvdh
Posts: 177
Joined: 16 May 2013 13:00

Re: cscript.exe check is return is + or -

#22 Post by Adrianvdh » 17 Aug 2013 09:08

Ok I am not downloading a ZIP file I am actually downloading a batch file which I had said already.

Just for security sake, I want it to know if the download was a success or not.
I am using normal HTTP on my web server. So I was testing it and deleted the file from the
web server and the script can recovery it from a temp location or something similar?
I think it is able to download a logical file only not the physical ones when they don't exist.

Any help?

Adrianvdh
Posts: 177
Joined: 16 May 2013 13:00

Re: cscript.exe check is return is + or -

#23 Post by Adrianvdh » 17 Aug 2013 09:37

I think I am going to give up on this vbs because I found something else...

Create a download.ps1 file:

Code: Select all

param($url, $filename)
$client = new-object System.Net.WebClient
$client.DownloadFile( $url, $filename)


Now you can download a file like this:

Code: Select all

powershell Set-ExecutionPolicy Unrestricted
powershell -ExecutionPolicy RemoteSigned -File "download.ps1" "http://somewhere.com/filename.ext" "d:\filename.ext"


So this is what I found on the internet, but it is basic, how would I implement error checking?
Also how would I make it only in a batch file, so it wouldn't have to create *.ps1 file.

I think you could do this is because I have some power shell in my batch file, for example this is some power shell that is in my abcth file that does not need to create a ps file

Code: Select all

set "ps=Add-Type -AssemblyName System.windows.forms | Out-Null;"
set "ps=%ps% $f=New-Object System.Windows.Forms.OpenFileDialog;"
set "ps=%ps% $f.Filter='Web Matrix List Files (*.wmsl)|*.wmsl';"
set "ps=%ps% $f.showHelp=$true;"
set "ps=%ps% $f.ShowDialog() | Out-Null;"
set "ps=%ps% $f.FileName"
for /f "delims=" %%I in ('powershell "%ps%"') do set "FileName=%%I"
if "%FileName%"=="FileName" goto notdefinedimportlist
if defined FileName goto definedimportlist
if not defined FileName goto notdefinedimportlist


Thanks!

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: cscript.exe check is return is + or -

#24 Post by penpen » 17 Aug 2013 14:03

foxidrive wrote:With the code below that is generated by your script, and launching this command from a cmd prompt:

cscript /nologo vbs.vbs http://www.google.com "D:\abc\g.htm"

I get this response: d:\abc\vbs.vbs(10, 1) msxml3.dll: Access is denied.

from this line: objXMLHTTP.send()
foxidrive wrote:The script works, and also with the URL in quotes, but the send command seems to fail if the site uses secure transfers.
The obove result is not caused by a site that uses secure transfer.
The script i used above worked on WinXp home/prof, win7 home:

Code: Select all

cscript /nologo vbs.vbs "http://www.google.com" "Z:\index.html"

In most cases this error is caused by your pc's security manager or firewall, where one of their rules disallow downloading.
If you can open the sites above from IE successfully, then in most cases this should be successfull, when the above code fails:

Code: Select all

Dim objXMLHTTP : set objXMLHTTP = CreateObject("MSXML2.ServerXMLHTTP.6.0")
' instead of this line:
' Dim objXMLHTTP : set objXMLHTTP = CreateObject("MSXML2.XMLHTTP")
But this is not secure, as it may underrun your systems security manager, or your firewall, or both.
If you know that the files cannot harm your system, you may download it in such a way,
and if you download your own files on your own server the file download sould be trustworthy enough,
but the recommended way is to find the security manager/firewall settings that disallows the download, and change them as needed (although this easily may become very tricky).

Note that this script returns 0 if the download failed, so returning errorcode 0 is correct in this case.
On success it returns 1.

penpen

PS: Powershell uses the same function as vbs with MSXML2.ServerXMLHTTP.6.0 if the std function fails (in vbs MSXML2.XMLHTTP).
If you want the same handling as in powershell (first trying "MSXML2.XMLHTTP" and if this fails "MSXML2.ServerXMLHTTP.6.0"), just post it.

Adrianvdh
Posts: 177
Joined: 16 May 2013 13:00

Re: cscript.exe check is return is + or -

#25 Post by Adrianvdh » 17 Aug 2013 16:33

So, what do I do now? I would rather use the powershell then the vbs, but could you add error handling?

Also maybe not make it have to write to *.ps1 file. Just run it off the batch file like the code I posted of the powershell script?
That would be great. I know I am pushing it with the powershell script, but the vbs is to much of a hassle. Sorry.

The firewall on my computer should be fine, all that vbs/ps1 script needs to do is download a batch file from my web server that I wrote.
I am able to see the batch file on my web server if I look at the link of it.
The batch file is only like 7 lines long and contains independent variables that my main batch file need to function on a certain function.

Thanks for your help!

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: cscript.exe check is return is + or -

#26 Post by penpen » 17 Aug 2013 17:08

It may suffice if you add a a line at the end of your PS script:

Code: Select all

Exit $LASTEXITCODE
Your Powershell script then should return an errorcode of 0 on success and 1 on a failure.

penpen

Adrianvdh
Posts: 177
Joined: 16 May 2013 13:00

Re: cscript.exe check is return is + or -

#27 Post by Adrianvdh » 18 Aug 2013 05:17

Wait!

Do I add the line you gave me in the download.ps1 file or the batch file?

Thanks penpen

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: cscript.exe check is return is + or -

#28 Post by penpen » 18 Aug 2013 12:02

Sorry, i thought the function $client.DownloadFile would return an exitcode itself, but this is not the case.
So you have to add more than one line to your Powershell script "download.ps1":

Code: Select all

param($url, $filename)
try {
   $client = new-object System.Net.WebClient
   $client.DownloadFile( $url, $filename)
   Exit 1
}
catch [System.Net.WebException] {
   Exit 2
}
catch [System.IO.IOException] {
   Exit 3
}
catch {
   Exit 4
}

Exit 0

Then you can call the PsScript your way, and can additionally check the result:

Code: Select all

:checkDownload.bat
@echo off
set "error0=Unknown Powershell script error occured when downloading"
set "error1=Downloaded"
set "error2=System.Net.WebException while retrieving"
set "error3=System.IO.IOException occured while downloading"
set "error4=Thrown an unexpected exception on downloading"


set "url=http://www.google.de"
set "filename=Z:\index.html"
powershell Set-ExecutionPolicy Unrestricted
powershell -ExecutionPolicy RemoteSigned -File "download.ps1" "%url%" "%filename%"

if defined error%errorlevel% (
   call echo %%error%errorlevel%%% "%filename%" from "%url%".
) else (
   echo Unknown and unexpected system error occured while downloading "%filename%" from "%url%".
)
Hopefully this works for you.

penpen

Adrianvdh
Posts: 177
Joined: 16 May 2013 13:00

Re: cscript.exe check is return is + or -

#29 Post by Adrianvdh » 18 Aug 2013 13:30

I am afraid the code does not work on one tested reason...

Code: Select all

powershell Set-ExecutionPolicy Unrestricted
powershell -ExecutionPolicy RemoteSigned -File "%vbsDown%" "%UpdateURL%" "%updateinfofile%"
echo %errorlevel%
pause
if "%errorlevel%"=="0" (goto getupdateinfo1
) else if "%errorlevel%"=="1" (goto checkupdateinforfile
) else if "%errorlevel%"=="2" (goto getupdateinfo1
) else if "%errorlevel%"=="3" (goto getupdateinfo1
) else if "%errorlevel%"=="4" goto getupdateinfo1
) else goto getupdateinfo1 )


If I delete the file from my web server it will just download XML OK (which is not the file)

Therefore it has not downloaded the file successfully...

What am I doing wrong?

Adrianvdh
Posts: 177
Joined: 16 May 2013 13:00

Re: cscript.exe check is return is + or -

#30 Post by Adrianvdh » 18 Aug 2013 14:02

Nevermind penpen, I got it to work alright! Thank you very much :)

Post Reply