Stop statement???

Anything VBScript-related, including Windows Script Host, WMI, ADSI, and more.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 11 years and 11 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
tech_soul8
Posts: 26
Last visit: Sat Mar 08, 2014 12:14 pm

Stop statement???

Post by tech_soul8 »

Hello folks, again :)How can I stop script from executing further code after STOP statement?For example I have wrote this code:Dim oFSO, iStart, iKraj, strMjesec, strProvjera, strPrethodniMjesec, objFolderstrMjesec = Month (Now) 'Dohavti tekuci mjeseciStart = 1 'Pocetna vrijednost brojacastrPrethodniMjesec = strMjesec - 1 'Sluzi za stvaranje foldera arhive'iKraj 'Zavrsna vrijednost brojacaiProvjera = 30 'Provjera kojeg dana se skripta izvodi! Ako se skripta pokrenula 30. ili 31. znaci da se izvodi u tekucem mj.(ako nije prijestupna god.) i u tom sulcaju izvodjenje se treba prekinuti inace Folder u kojem se sprema arhiva nece biti imenovan pravilno!If iProvjera = 30 Then Stop _End IfIf strMjesec = 4 Then iKraj = 30 _ _ else if strMjesec = 5 Then iKraj = 31 _ else if strMjesec = 6 Then iKraj = 30 _ else if strMjesec = 7 Then iKraj = 31 _ else if strMjesec = 8 Then iKraj = 31 _ else if strMjesec = 9 Then iKraj = 30 _ else if strMjesec = 10 Then iKraj = 31 _ else if strMjesec = 11 Then iKraj = 30 _ else if strMjesec = 12 Then iKraj = 31 __else Wscript.Echo "Nisam uspio saznati u kojem sam mjesecu!"Set oFSO = CreateObject ("Scripting.FileSystemObject")'Folder u kojem ce se spremiti arhiva iz prethodnog mjesecaoFSO.CreateFolder "C:Documents and SettingsTestDesktop" & strPrethodniMjesec & "." & "Mjesec" 'Dohvati folder u koji ce se spremiti arhivaSet objFolder = oFSO.GetFolder ("C:Documents and SettingsTestDesktop" & strPrethodniMjesec & "." & "Mjesec") 'Backup prethodnog mjesecaoFSO.MoveFile "C:Documents and SettingsTestDesktopNew Folder*.*", objFolder 'Popuni folder s praznim tablicama za tekuci mjesecDo while iStart <= iKrajoFSO.CopyFile "D:dokumentiExample1-Empty1.xlsx", "C:Documents and SettingsKasaDesktop" & _"New Folder" & iStart & "." & strMjesec & ".xlsx", FalseiStart = iStart + 1If iStart > iKraj Then _Exit Do _End ifLoopShouldn't script stop after "If iProvjera = 30 Then Stop _
End If" and all the code bellow shouldn't be executed?? If I'm wrong (and obviously I am) how can I stop script if above statement is true?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Stop statement???

Post by jvierra »

The following syntax is completely wrong:

If strMjesec = 4 Then iKraj = 30 _ _ else if strMjesec = 5 Then iKraj = 31 _ else if strMjesec = 6 Then iKraj = 30 _ else if strMjesec = 7 Then iKraj = 31 _ else if strMjesec = 8 Then iKraj = 31 _ else if strMjesec = 9 Then iKraj = 30 _ else if strMjesec = 10 Then iKraj = 31 _ else if strMjesec = 11 Then iKraj = 30 _ else if strMjesec = 12 Then iKraj = 31 __Look at the documentation for VBScript and study how to form statements.

http://msdn.microsoft.com/en-us/library ... s.85).aspx

jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Stop statement???

Post by jvierra »

Sorry but that syntax will not work. It may not throw an error but it won't execute correctly.

The line continuation does not work as you expect it to and it will give you many issues with your code. The parser will no generate an error becues the statement, as you have structired it, has no end. The parser jsust assumes that ther is more an jumps into space.

In VBScipt we do this with a 'case' statemnet. It is more naturaly, more flexible, easier to maintain and it works.

Code: Select all

Select Case strMjesec
    Case 4: iKraj = 30
    Case 5: iKraj = 31 
    Case 6: iKraj = 30
    Case 7: iKraj = 31
    Case 8: iKraj = 31
    Case 9: iKraj = 30
    Case 10: iKraj = 31
    Case 11: iKraj = 30
    Case 12: iKraj = 31
End Select


I guarantee you cannot show me where this is in the documentation:

If condition Then _
Elseif condition Then _
Elseif condition Then _
Else... _
End if


This is what is in the docs:

If <condition> Then
<statements>
Elseif <condition>
Then
<statements>
Elseif <condition>Then
<statements>
Else <statements>
End if


This is eactly the way Microsoft explains it in the documenttion for VBScript (WSH)

Code: Select all

If condition Then statements [Else elsestatements ] 
' Or, you can use the block form syntax: 
If condition Then
   [statements]
[ElseIf condition-n Then
   [elseifstatements]] . . .
[Else
   [elsestatements]]
End If 
Note that there is one continued form:

If condition Then statements [Else elsestatements ]


It can only be used in that form and only that form can be continued.

Only the 'else' statement is optional. No 'elseif'.

Use the case for simple numeric or string matches. Use ElseIf for complex logic like:

If 1 = b AND b = c AND x = y Then
ElseIf a = 3 AND b = 5 Then
Else
End If


The logic can be continued for compaction and readability.

If A = 3 _
AND B = 5 _
AND X = Y Then
< statements>
End If


The 'Then' is not continued It termintes the line. If you add a statement after it is terminated the if command.

This is the totallity of that form. Try it.

If a = 1 Then MsgBox "hello"
If a = 1 Then MsgBox "hello" Else MsgBox "bye"


Both are complete statements and can execute cleanly. They are usful for tests that will exit code like the following

If a = 1 Then Exit Sub

It is readable and easy to understand. We use this form to keep the logic clean and to avoid large switch statements that are sometimes ahard to follow like th ones you are tryoing to build. I am sdure Don Jones would agree 100%,

You should get a more basic book on VBScript before jumping into and advanced tome like WMI and ADSI which assumes a good fundamenal knowledge of scripting.

Anyway - abandon the "line continuation forever" method and most of you problems will go away.

Also be very careful. The line continuator MUST be preceded by a space and be the last character on the line. FOr this reason I recommend not using it unless you absolutely have to.

jvierra
2012-04-15 10:39:13
User avatar
tech_soul8
Posts: 26
Last visit: Sat Mar 08, 2014 12:14 pm

Stop statement???

Post by tech_soul8 »

You are wright! The code bellow works and I like it more than if, elsif,else...statatement.

Select Case strMjesec
Case 4: iKraj = 30
Case 5: iKraj = 31
Case 6: iKraj = 30
Case 7: iKraj = 31
Case 8: iKraj = 31
Case 9: iKraj = 30
Case 10: iKraj = 31
Case 11: iKraj = 30
Case 12: iKraj = 31
End Select

The reason why I tryed it with the If statement is becouse I found this in vbscript documentation "The Else and ElseIf clauses are both optional. You can have as many ElseIf statements as you want in a block If, but none can appear after the Else clause..." so I tought it is the way I could do it and becouse I didn't know for Select case statement.

Now I changed my If statement with the Select case so I hope so now it's fine :)

I used line continuation method becouse I wanted for long statements to fit onto the screen and not need to scroll with mouse to read it.

Actually this is the first script I have ever wrote and all by my self so errors like this was expected.

I noticed that the book from Don Jones is for more advanced readers and I'll try to get something for the begginers before I come back to it again :)
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Stop statement???

Post by jvierra »

'wright' is not a word. You have crossed 'right' and 'write' into a new thing.

I am not sure if you are correct because you jsut fed backe exactlky what I posted with some errors.

You cannot use line continuation in an if/else stement except as part of a single conditional.

This would have worked:

Code: Select all

	
If strMjesec = 4 Then 
    iKraj = 30        
elseif strMjesec = 5 Then 
    iKraj = 31 _
elseif strMjesec = 6 Then
    iKraj = 30
elseif strMjesec = 7 Then
    iKraj = 31
elseif strMjesec = 8 Then
    iKraj = 31
elseif strMjesec = 9 Then
    iKraj = 30
elseif strMjesec = 10 Then
    iKraj = 31
elseif strMjesec = 11 Then
   iKraj = 30
elseif strMjesec = 12 Then
     iKraj = 31 
else 
     Wscript.Echo "Nisam uspio saznati u kojem sam mjesecu!"
End If
	

Study the difference.

In this case the 'Select Case' statement is the preferred approach.

Save elseif for complex logic situations.

It is altogether beter to design code to avoid switch logic. It can become a bad habit and create very unstable code. Consider basing logic on conditions and states and not on ladder tests.
This topic is 11 years and 11 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked