VBScript: The quick start course in Windows Automation, 2020 edition by Armstrong D

VBScript: The quick start course in Windows Automation, 2020 edition by Armstrong D

Author:Armstrong, D [Armstrong, D]
Language: eng
Format: epub
Publisher: D Armstrong
Published: 2020-01-15T16:00:00+00:00


Answer 25

You could do this:

Dim filePath, fso, file, i

filePath = "BigFile.html"

Set fso = CreateObject("Scripting.FileSystemObject")

Set file = fso.CreateTextFile(filePath, True)

For i = 1 to 1000

If i Mod 10 = 0 Then

file.Write("<!--" & i & " - dummy text" & "-->" & vbCrLf)

Else

file.Write("<p>" & i & " - dummy text" & "</p>" & vbCrLf)

End If

Next

file.Close

7 – Work with loops

7.1 – Repeat an action until something happens

Example

Write the script:

Dim num : num = 1

Do While num < 10

num = num * 2

MsgBox num

Loop

Run it.

Explanation

This script keeps doubling, and displaying, a number, until it exceeds ten.

This kind of loop is known as a Do...Loop. It is often used with a While-condition, as shown here (and so referred to as a Do...While loop). The loop checks the condition, and if that condition is met, runs the code inside, then goes back to the "Do" line and repeats the cycle, until the condition returns false.

Beware though, if you give it a condition which never returns false, the script will just keep going round the loop.

Notice also how we declare a variable and assign it a value on the same line, by using the ":" (colon) character.

Exercise 26

Make the code only display the number if it is ten more.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.