VBS – Variables



The Command PromptThe command prompt windowThe command prompt (sometimes referred as DOS window) is the command line non-graphic environment. In this course we will be using the command prompt for most of our exercises. To access command prompt, go to the search window, type CMD or prompt, select command prompt and press enter. The window command will open. Type CScript and press enter, you should see list of options for the CScript.?The command prompt commandsIn the command prompt commands, you should be familiar with the basic commands, here are the most used command.>Dir “You type Dir, the “>” before Dir to indicate that it is from the command prompt”>Dir ?????????????????????????????????????????????????????? To list the content or the Folder / Directory.>cls??????????????????????? ??????????????????????????????? To clear the content of the screen>MD????????????????????? ??????????????????????????????? To Make directory>CD??????????????????????? ??????????????????????????????? To change directory>Dir > myresults.txt ?????????????????????? The?“>”?after Dir to direct the results into a file named myresults.txt>Type myresults.txt ?????????????????????? To see the content of file myresults.txtTo Do:?From the command prompt>CD\????????? ???????????Press enter to go to the root folder>MD entd261???? Press enter to create a folder name entd261, this is going to be your working folder for this course>CD entd261?????? Press enter to change to entd261 folder.For more commands visit:?21 CMD Commands All Windows Users Should KnowThe EditorYou need an editor to write your code. You can use Notepad.exe, but it is preferable an editor with a line number. When you run the code, if you have an error, then engine will refer you to the error by the line number. I recommend NotePad++ or TextPad both are available to download for freeTo Do:?? ? (Please complete the code below! You'll want to practice BEFORE completing the weekly assignment.)Open a new file in the editor you selected and type the followingWSH.Echo “Hello ENTD261”?? à? type the code, do not copy and paste.Save the code as MyVBScript01.vbs in \ENTD261 folderENTD261>Script MyVBScript01.vbsentd261>cscript vb01.vbsYou will seeHello ENTD261?Now you are ready to do programingVBS – VariablesVariable is a temporary place in memory used to store valuesWe are going to create a simple calculator to demonstrate the programing elementsfnum = 5??? ????????????????????????????????????????????????????????? ' fnum is a variable contain numbersnum = 7??? ???????????????????????????????????????????????????????? ' snum is a variable contain numberMsg =?"Thank you"?????????????????????????????????????? ' is a variable contain string.WSH.Echo ("The total =" & total)????????????? ' & operator is concatenation operator to print number and strings together You can also do it like this? WSH.Echo ?"The total =", total? ? ? ?To Do:? (Please complete the code below! You'll want to practice BEFORE completing the weekly assignment.)Open a new file and type the followingWe are going to create a simple calculator to demonstrate the programing elementsfnum = 5??? ????????????????????????????????????????????????????????? ' fnum is a variable contain numbersnum = 7??? ???????????????????????????????????????????????????????? ' snum is a variable contain numbertotal = fnum + snum?????????????????????????????????????? ' total?? is a variableMsg =?"Thank you"??????????????????????????????????????? ' is a variable contain string.WSH.Echo ("The total =" & total)????????????? ' & operator is concatenation operator to print number and strings together the & is like converting numbers to string automatically.WSH.Echo (Msg)Note the { ‘ } single quote is a comment, the engine will ignore any writing right to the single quote.Save the above code in ENTD261\? as MyVBScript02.vbsRun the codeENTD261> CScript MyVBScript02.vbsThe above code is not useful, because the values are hard coded, we want to be able to pass values to the script without changing the code. The solution is use argument from the command line. To understand the arguments, copy the following code in MyVBScript03.vbs;The command to capture an argument in VBScript is WScript.arguments(0) for the first argument, WScript.arguments(1) for the second argument, WScript.arguments(2) for the third argument and so on. “cint” is a function to convert string to integer. The arguments by default are string.?fnum =?? cint(WScript.arguments(0))? ????? 'fnum is a variable contain number from first argumentsnum =?? cint(WScript.arguments(1)) ????? 'snum is a variable contain number from second argumenttotal = fnum + snum?????????????????????????????????????? 'total?? is a variableWSH.Echo ("The total =" & total)????????????? ' & operator is concatenation operator to print number and strings togetherTo run the above codeENTD261> CScript MyVBScript03.vbs 12 15You will getThe total =27Try it with different variablesVBS – LoopsLoops are mechanism to repeat the same command(s) several times. In the code below, we will loop from a value to a value using the For-next loop. Below is the general structure for the for Next-loopFor I = F to T????? Commands to repeatNext?To Do:? ? ?(Please complete the code below! You'll want to practice BEFORE completing the weekly assignment.)To understand the for loop, copy the following code into MyVBScript04.vbsThe?&?operator is the concatenation operator, it is used in the print (Echo) function to concatenate numbers and strings.fnum = cint(WScript.arguments(0))snum = cint(WScript.arguments(1))For I = fnum to snum??? WSH.Echo ("I =" & I)?? ' & operator is concatenation operator to print number and strings togetherNextTo run the above code, do this?ENTD261> CScript MyVBScript04.vbs 2 5VBS – If-then Structure?The if-then or the decision-making structure is an evaluation of an expression using If-then. If the value is True, the statements under Then condition(s) are Executed. If the Condition is False, the statements under?Else?Part would be?executed.?To Do:? ? ?(Please complete the code below! You'll want to practice BEFORE completing the weekly assignment.)Create the following code and run as you did before.' This is comment block' This program will accept two arguments from the command line' The program will run the loop only if the first number is greater than the second numberfnum = cint(WScript.arguments(0))snum = cint(WScript.arguments(1))If snum > fnum then?? For I = fnum to snum????? WSH.Echo ("I =" & I)? ' & operator is concatenation operator to print number and strings together?? NextElse?? WSH.Echo (“ Second number? must be greater? than first number”)End ifVBScript ObjectsVBScript has a very rich object library that allows you to do many tasks around the system. For example if you want to work with file systems such as reading / writing files, checking folders and so on you need to use a library named “Scripting.FileSystemObject” to use this library, you need to use the command “set”. Here is an examplefn="xyz"Set fs = CreateObject("Scripting.FileSystemObject")If fs.FolderExists(fn) then?? msgbox "good folder"else?? msgbox "bad file"end ifSet folder = fs.GetFolder(fn)wscript.echo "Folder Results File Created"Set files = folder.FilesFor Each file in files??? wscript.echo "Name: " & (file.name)??? wscript.echo "Size: " & (file.size)??? wscript.echo "Date: " & (file.datecreated)Next?Create a program from the above code as you did in prior exercises and run it.fn is variable to hold folder name, change xyz to a folder/directory name you have on your systemset fs, fs is an object name (you can name it whatever) for the file systemfs.FolderExist(fn),? is how to check the existing of a folder? FolderExist is a function in the library “Scripting.FileSystemObject” that takes the folder name (fn) as parameter.Set folder = is another variable to hold folder informationfs.GetFolder(fn), GetFolder is a function to get the folder content?Set files = is a variable to hold file information from the folder objectfolder.Files??For more information about the? HYPERLINK "(v=vs.84).aspx" \t "_blank" FileSystemObject?IntroductionWhat Is a FlowerBox?'----------------------------------------''''This is a flowerbox.''''''You'd generally have comments? here with the program name, your name,'''assignment information and program description'----------------------------------------'Note: You'll have to modify this slightly for each language per how comments are achieved. See this example in PERL :?flower box commentsRequirementsPointsComment block (flowerbox) with Instructions on how to run the code with examples.20Code documentation and comments.10Assignment code including creating command line parameter70TOTAL POINTS100 ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download