Contents for Ezine 100 – ‘If’ Statements
This Week’s Missions – ‘If’ Statement also Datepart and TimeThis week’s main mission is to master the tiny but useful, ‘If’ conditional statement. Every scripting language has an ‘If’ construction, we are going to study the VBScript syntax for ‘If’…Then End If. I will also touch on variations such as ElseIf. The context for this week’s script is checking the system clock, (next week I promise we will use a new vehicle for our scripts – Active Directory). What we want to do is display different messages depending on whether the computer’s clock is fast, slow, or synchronized with the external time source. Here is an example: If timeDiff < 0 then 0) I like this 3 line version of the ‘If’ logic. However, it is possible to employ short ‘If’ statements all on one line. 1) Observe the word ‘then’. See how ‘then’ follows the test, ‘If timeDiff < 0’ and notice that ‘then’ is the last word on the first line. 2) To tell VBScript that ‘If’ has finished evaluating the test statement, sign off with, ‘End If’ (note the space between End and If. 3) As you can see, the ‘If’ construction is flexible. In scripts where there is a main test and only one subsidiary, then I recommend that you employ ‘If’ (as usual) and just one plain ‘Else’ statement. With VBScript, when you have three or four tests, then precede the second and subsequent test with ElseIf. ElseIf is different from Else If. Where possible, choose ElseIf (all one word) because then you only need one End If no matter how many ElseIfs you have. Avoid Else (space) If unless you have a specific reason for using this construction, the reason being each Else (space) If needs its own End If. As ever, examples clarify thinking. Example A (Good) If timeDiff < 0 then Below, I added the extra End If, caused by choosing Else If instead of ElseIf. Example B (Not Recommended) If timeDiff < 0 then 4) Should you have more than five ElseIf statements then consider a different construction altogether – Select Case. Time and Date Functions – DATEPART and Time Almost incidentally, this week’s script features time functions. There is a whole family of related date and time functions, for example, NOW, TIME and DATEPART. In this week’s ezine, Datepart and TIME play a role in calculating whether the computer clock was fast, slow or synchronized with the time server. As with previous scripts, this script cries out for you to deliberately alter the computer’s clock; otherwise the script just says ‘Clock synchronized’ . When Datepart reads the TIME function, "s" means seconds, minutes is ‘n’ (and not "m"), while hour is ("h"). My mission was to translate the clock time into seconds, then we can compare one pure number with another. Guy Recommends: The Free IP Address Tracker (IPAT)Calculating IP Address ranges is a black art, which many network managers solve by creating custom Excel spreadsheets. IPAT cracks this problem of allocating IP addresses in networks in two ways: For Mr Organized there is a nifty subnet calculator, you enter the network address and the subnet mask, then IPAT works out the usable addresses and their ranges. For Mr Lazy IPAT discovers and then displays the IP addresses of existing computers. Download the Free IP Address Tracker This Week’s SecretSENDKEYS is a useful method for leaning what the code is doing. I also use .Sendkeys for troubleshooting what went wrong. However, in the case of production scripts favour the .RUN method. You can imagine the potential for disaster if you or a user switches windows after one .Sendkeys {} command and before the next. At best the script would fail, at worst it could unleash a disastrous sequence of commands. Example: To Synchronize with the Windows Time ServicePurposes of the ScriptThe script synchronizes the local machine with an internet time server, then displays a message indicating if the internal clock was slow, fast or on time. Instructions for Synchronizing with the Windows Time Service
‘=========================================== ‘ Time Server set (Remove ‘ Rem if you want to change) ‘ Use .Run method to configure the time server ‘ Key command to resynchronize with time server ‘ Cosmetic section to display the clock adjustment Sub Restart() VBScript Tutorial – Learning PointsNote 1: Get into the rhythm of the If, then, End If. This simple construction is versatile and greases the wheels of many VBScripts. Note 2: ElseIf is better than Else If. My advice is avoid a space between Else and If, unless you deliberately want a different outcome. If you have more than five ElseIfs then investigate the alternative Select Case. Note 3: Observe how this script employs the .Run method rather than .Sendkeys {}. Guy Recommends: SolarWinds Free Network Bandwidth MonitorThis freeware monitor is great for checking whether your network’s load-balancing is performing as expected, for example, are two interfaces are getting about equal traffic? It’s easy to install and straightforward to configure. You will soon be running tests to see how much network bandwidth your applications consume. The GUI has a lovely balance between immediate network traffic data in the middle, combined with buttons to seek related data and configuration settings. Give this monitor a try, it’s free! Download your free network bandwidth monitor If you need more comprehensive network analysis software: Challenges
Summary of ‘If’ StatementThe ‘If’ conditional statement is flexible and adds value to so many VBScripts. Take the time to master the syntax, especially of ElseIf. SendKeys is great in testing and troubleshooting, but production scripts are more reliable if you employ the .Run method. See more about VBScript• VBScripts • Ezines • WMI • Logon Scripts • PowerShell Foreach •Tool Kit • Ezine 1 Loops •Ezine 14 Loops • Ezine 47 .put • PowerShell Loops • Free Web Watcher • Ezine 90 VBS Looping • Ezine 91 VBS Looping • Ezine 92 LDIFDE • Ezine 100 If |