Sei sulla pagina 1di 18

Command Prompt and NotePad++ Settings

Task 1: Command Prompt Desktop Icon


Click on the Start button and select All Programs, open the Accessories folder and find the Command Prompt item. Press and hold the <Alt> key while you Drag/Drop the Command Prompt item to a blank area of the desktop. Release the mouse button before you release the <Alt> key. <Alt> Drag/Drop creates a link (or shortcut). If we had used Drag/Drop without the <Alt> key, we would have moved the Command Prompt item and it would no longer appear in the Accessories folder.

Note: The default action of Drag/Drop varies depending on the context (for example: between the same or different hard drives or between the Start menu and the desktop). You can override the default Drag/Drop action by holding the <Alt>, <Ctrl> or <Shift> as you Drag/Drop. <Alt> Drag/Drop always creates a link (or shortcut), <Ctrl> Drag/Drop always performs a copy operation and <Shift> Drag/Drop always performs a move operation.
Right-click on the new Command Prompt desktop icon and select Properties.

Click the Advanced button to open the Advanced dialog. Check the box Run as administrator. Click OK to close the Advanced dialog. Click Apply then OK to in the Properties
CLI_NotePad++_Settings.docx 1 Revision Date: 1111

dialog to youre your changes. Now every time you open the Command Prompt link you will run the CLI as administrator.

CLI_NotePad++_Settings.docx

Revision Date: 1111

Task 2: Create the C:\Scripts folder


We will save all of our VBScript programs in the C:\Scripts folder. If you havent created the C:\Scripts, please create the folder (or directory) now. You can make the folder using Windows Explorer or you can double-click on your new Command Prompt desktop icon to the Windows CLI and use the mkdir C:\Scripts command as shown below:

Task 3: Install NotePad++ and Edit Shortcut Properties


Navigate to the webpage: http://notepad-plus-plus.org/download Download the current version of the NotePad++ Installer. Double-click on the NotePad++ Installer program in Windows Explorer to install the program on your computer. You can use the default settings when you install the program. Before we start using NotePad++, we need to make sure that every time it opens, the application runs with administrator privileges. If we dont do this, there will be some VBScripts that will not execute correctly from NotePad++ because of insufficient permissions. Find the NotePad++ icon on your Windows desktop and right-click on it and select Properties. Click on the Advanced button in the Shortcut tab. In the Advanced dialog box, click the checkbox Run as administrator

Click OK to exit the Advanced dialog. In the Properties dialog, click Apply then OK to accept the changes and close the Properties dialog.
3 Revision Date: 1111

CLI_NotePad++_Settings.docx

Task 4: NotePad++ Default Language, Directories & Preferences


Open NotePad++ and from the Language menu, select V then VB for Visual Basic. Now under Settings menu, select Preferences. Open the New Document/Default Directory tab and select Remember last used directory.

Note: The advantage of using Remember last used directory is the path used by NotePad++ will default to the last file you opened using File/Open. This will allow us th Run or Execute our scripts with having to specify an explicit path. Open the Language Menu/Tab Settings tab and change the Tab size: to 2 and check box Replace by space.

Open the Backup/Auto-Completion tab and check the Enable auto-completion on each input box for Function completion. Also check the Function parameters hint on input box.

Close the Preferences Tabbed dialog.

CLI_NotePad++_Settings.docx

Revision Date: 1111

Task 5: NotePad++ Style Configurator Settings


One of the advantages of using NotePad++ instead of Microsoft Windows NotePad is the ability to use different fonts and colors for different language elements in the Sourcecode Editor. We can edit these Sourcecode Editor properties using the Style Configurator. From the Settings menu, select Style Configurator. This will open the Style Configurator dialog. Leave the Selected theme set to Default (stylers.xml).

Under Language, select VB / VBS. Note that we can set individual fonts and colors for the styles: DEFAULT, COMMENT, NUMBER, WORD, STRING, PREPROCESSOR, OPERATOR, DATE

If you cannot find the color you need in the standard palette, click the More Colours button. Click and Drag your mouse across the color spectrum and click on the color you want to use. You can adjust the brightness (Luminosity) level of your color by sliding up or down with your mouse.

To make the configuration easier, just choose the closest color found in the standard palette.

For DEFAULT, set the color to Black, the Font to Consolas and the Font size to 10.

CLI_NotePad++_Settings.docx

Revision Date: 1111

For COMMENT, set the color Dark Green, the font to Consolas, Italic and the Font size to 10.

For NUMBER, set the color to Dark Purple, the Font to Consolas and the Font size to 10.

For WORD, set the color to Medium Blue, the Font to Consolas and the Font size to 10.

For STRING, set the color to Red, the Font to Consolas and the Font size to 10.

CLI_NotePad++_Settings.docx

Revision Date: 1111

For PREPROCESSOR, set the color to Red, the Font to Consolas and the Font size to 10.

For OPERATOR, set the color to Dark Blue, the Font to Consolas Bold and the Font size to 10.

For DATE, set the color to Green, the Font to Consolas and the Font size to 10.

CLI_NotePad++_Settings.docx

Revision Date: 1111

Task 6: NppExec and Explorer PlugIns for NotePad++


Open NotePad++ and right-click on Plugins. Select Plugin Manager Show Plugin Manager.

In the Available tab, check the Explorer plugin. Scroll down the list of plugins and also check the NppExec plugin. Click the Install button to install the plugins into NotePad++.

NppExec provides a built-in Console for running VBScripts with cscript. This will allow you to run your script and test for errors without leaving NotePad++. You can also remove the WScript.Sleep( ) and WScript.StdIn.Read(1) statements if they were used to prevent the Console window from closing. The NppExec Console window will not close until you choose to close it from the toolbar. The NppExec dialog is activated with an <F6> keypress. The last program can be run again by pressing <Ctrl><F6>. The Console font should be changed to Consolas Bold 10 pt. using the Plugins menu. The NppExec console can be docked anywhere on the Notepad++ application window or it could be left floating on the desktop. Explorer provides a navigation pane and contents pane (similar to Windows Explorer) that can be used to find your VBScript sourcecode files. Double-clicking on a file in the contents pane will open the file in the NotePad++ editor window. The Explorer hotkey can be changed from <Ctrl><Alt><Shift>E to <Ctrl>E using the Macro menu. Like the NppExec Console, Explorer can be docked anywhere on the Notepad++ application window or it could be left floating on the desktop.

CLI_NotePad++_Settings.docx

Revision Date: 1111

After the NppExec and Explorer plugins have been installed and NotePad++ has been restarted, Use the Plugins menu as shown below to change the Console font to Consolas Bold 10 pt. Click OK to record the changes.

To change the Explorer hotkey using the Macro Modify ShortCut/Delete Macro menu option. This will open the Shortcut mapper dialog. Open the Plugin commands tab and select Explorer. Click the Modify button and remove the check marks for the ALT and SHIFT shift keys. Click OK and close the Shortcut mapper dialog. With your cursor in the Editor window, press <Ctrl>E to verify that the new Explorer hotkey is enabled. Move the cursor back to the Editor windows and press <Ctrl>E again to close Explorer.

CLI_NotePad++_Settings.docx

Revision Date: 1111

Task 7: Running VBScript Programs in The Windows CLI


Open NotePad++ and write the VBScript program exactly as shown below save the source code file in the C:\Scripts folder as EchoDemo.vbs. Make sure that you specify the (.vbs) extension or it will be incorrectly saved with the (.vb) extension. After youve saved the sourcecode file the first time, you can the hot key <Ctrl>S to save any changes to the file.

Open the Windows CLI using the desktop link (or shortcut) you created in Task 1. Change Directory (CD) to C:\Scripts use the dir command to verify the sourcecode file is there. Microsoft Windows [Version 6.1.7601] Copyright (c) 2009 Microsoft Corporation. C:\Windows\system32>cd \Scripts C:\Scripts>dir EchoDemo.vbs Directory of C:\Scripts 11/14/2011 11:55 AM 1 File(s) 183 EchoDemo.vbs 183 bytes

All rights reserved.

C:\Scripts> VBScript programs are not standalone programs like those created by a C++ compiler. C++ programs can be compiled and linked to create a standalone executable (.exe) program. VBScript programs must be run using an Windows Scripting Host (WSH) interpreter. The two WSH interpreters are cscript and wscript. Cscript sends program output to the Command Line Interface (or console) whereas wscript sends program outout to a PopUp Window. Most of the programs we will write will use cscript. Assuming that we want to run the VBScript program EchoDemo.vbs, we would enter the following commands: C:\Scripts>cscript EchoDemo.vbs or C:\Scripts>wscript EchoDemo.vbs
CLI_NotePad++_Settings.docx 10 Revision Date: 1111

Run the EchoDemo.vbs program using cscript first and then wscript. You should get the outputs shown below:

If you enter the VBScript program name in the Windows CLI without specifying the interpreter, the wscript Interpreter will be used by default. Type EchoDemo.vbs and verify this.

CLI_NotePad++_Settings.docx

11

Revision Date: 1111

Task 8: Running VBScript Program in NotePad++


Here we are back in Notepad++ with our EchoDemo.vbs program opened in the Editor window using Explorer. You can run this EchoDemo.vbs program from NotePad++ using the Run hot key <F5> and typing in the filename of the script you want to run. To Run the EchoDemo.vbs program, type EchoDemo.vbs and click the Run button. As it is in Windows CLI, if you dont specify the WSH interpreter, it will use wscript by default. Note that we are not specifying the path in our <F5> Run dialog. We can do this because we have set the default directory to Remember last used directory.

If you want to use the cscript WSH interpreter, you will have to type cscript before you type the path and VBScript filename as shown below. Click the Run button. What did you see? What you should see is a console window flash on the screen and then quickly disappear. The EchoDemo.vbs program did run, but when the program ended, the console window closed.

CLI_NotePad++_Settings.docx

12

Revision Date: 1111

To run a program with cscript in NotePad++ using the Run dialog, you need a statement to pause the program before the console window closes. There are two ways to pause or delay closing the NotePad++ console window. The first statement will pause the program until the user presses the <Enter> key. Modify your EchoDemo.vbs program as shown below. A WScript.Echo statement is added on line 10 to prompt the use to Press <Enter> to End Program. The WScript.StdIn.Read(1) statement in line 11 pauses the program execution until the user presses the <Enter> key. Save the program using <Ctrl>S and press <F5> <Enter> to run the program again.

Now our revised EchoDemo.vbs program can be run using cscript in NotePad++ Run dialog. WScript.StdIn.Read(1) is very effective while you are developing a console program so you dont have to go back and forth between NotePad++ and the Windows CLI. Note: If the program being run from NotePad++ is a lab assignment, you can use your mouse to click and drag over the console text you want to copy and press <Enter> to put the console text into the clipboard.

Another available option is to use the WScript.Sleep(6000) to keep the NotePad++ console window open for an additional 6000 milliseconds (6 seconds). The Sleep value can be any value of delay you need in milliseconds (seconds * 1000 = milliseconds) . Delete lines 10 and 11 from the EchoDemo.vbs program in NotePad++ and add the following line 10, save your program <Ctrl>S and run the program with <F5> <Enter>. The console window will now remain open for 6 seconds before it closes.

CLI_NotePad++_Settings.docx

13

Revision Date: 1111

Task 9: Debugging a VBScript Program in NotePad++


Open the last modified version of our EchoDemo.vbs program in NotePad++ as shown below. Run the program using <F5> <Enter> as we did in Task 6. Verify that the program run can be seen in the NotePad++ console windows for 6 seconds before the console window closes.

Introduce an error into the program in line 7 by misspelling the Echo method as Eccho. Save the program <Ctrl>S. Run the program using <F5> <Enter>.

Even though we had a 6 second Sleep method in our program, the console flashed on and closed immediately. If we are running a console VBScript program in NotePad++ and we have used WScript.StdIn.Read(1) or WScript.Sleep(6000) to pause the console window; and the console window still flashes and closes immediately, that is a clear indication of an error in our program. When this happens, analyze your program in NotePad++ for syntax errors including misspelled commands, strings with a missing , etc. If you find an error, correct it, save your program and try to run it again. If you cant find the error by inspecting your source code, you will need to trouble-shoot your program from the Windows CLI.

Minimize, but dont close NotePad++. Open the Windows CLI using the desktop link (or shortcut) you created in Task 1. Change Directory (CD) to C:\Scripts. Microsoft Windows [Version 6.1.7601] Copyright (c) 2009 Microsoft Corporation. C:\Windows\system32>cd \Scripts C:\Scripts>

All rights reserved.

CLI_NotePad++_Settings.docx

14

Revision Date: 1111

Run your program and look for error messages. In the run below, an error has been detected in line 7, column 1 of the source code. Even though the column number is specified as 1, the error could be anywhere in line 7. C:\Scripts>cscript EchoDemo.vbs Microsoft (R) Windows Script Host Version 5.8 Copyright (C) Microsoft Corporation. All rights reserved. C:\Scripts\EchoDemo.vbs(7, 1) Microsoft VBScript runtime error: Object doesn't support this property or method: 'WScript.Eccho'

Read the error message to see if this helps to locate the error. The error message is Object doesnt support this property or method: WScript.Eccho. The object WScript does not contain a method called Eccho. Return to NotePad++ and correct the problem changing line 7 to:

Save your changes <Ctrl>S and re-run your program <F5><Enter>. Your program should now run as expected.

CLI_NotePad++_Settings.docx

15

Revision Date: 1111

Task 10: The NppExec Plugin to the Rescue


Obviously, it gets rather tedious going back and forth between NotePad++ and the Windows CLI to locate and correct sourcecode errors. It is also cumbersome to always add the WScript.StdIn.Read(1) or WScript.Sleep( ) function so you can keep the NotePad++ Console window open when you are running your program using cscript. This is where the NppExec Console becomes very useful. Edit the EchoDemo.vbs program by deleting the WScript.Sleep(6000) function in line 10.

This time we will run the program using the NppExec plugin. Press <F6> and enter cscript EchoDemo.vbs. Press OK to run the program. Notice that the program now runs in a built-in Console window that will not close until you click the Console toolbar button

CLI_NotePad++_Settings.docx

16

Revision Date: 1111

Put the error back into the program in line 7 by misspelling the Echo method as Eccho. Save the program <Ctrl>S.

Press <Ctrl><F6> to run your last NppExec command. This handle hotkey will run your program again without having to open the Execute dialog. You can see the error is detected in line 7.

Correct the error in line 7 and press <Ctrl>S to save your changes. Use wscript this time when you run EchoDemo.vbs with NppExec (<F6>). Just replace cscript with wscript and a pop-up windows will be displayed instead of a Console window. Please note that VBScript programs that are run using wscript cannot contain any WScript.StdOut and WScript.StdIn functions. Press <F6> and change the command to wscript. Click OK and observe the output.

CLI_NotePad++_Settings.docx

17

Revision Date: 1111

Terminology: What is an Object, a Method and a Property


To effectively program in VBScript and efficiently debug your programs, you need to understand the concept of Objects, Methods and Properties. An Object is an a unique feature of many programming and Scripting languages including C++, Java, JavaScript, JScript, VBScript and others. In non-object-oriented programming languages, we use statements for decision-making and loops, we have variables that hold data of a specific data type (such as string or integer) and we have functions that perform a task such as calculating a value, getting user input or displaying a message. In object-oriented programming languages, we also have statements, variables and functions, but we also have an additional construct called an object. An object is NOT a variable and it is NOT a function. An object encapsulates (includes) both functions (also called methods) and variables (also called properties) into a single entity. WScript is the top-level (and most commonly used) WSH object for VBScript and JScript programs. WScript.Echo is a method (or function) that belongs to the WScript object that performs a task. WScript.Version is a property (or variable) that holds the WSH version information. Note the use of the dot operator (.) to access methods and properties from an object. Below is a description of the methods and properties contained in the WScript object.

*WScript Object
Top-level object; forming the root of the object hierarchy.

Methods
WScript.ConnectObject(objectName,eventPrefix) WScript.CreateObject(objectName[,eventPrefix]) WScript.DisconnectObject(objectName) WScript.Echo([Arg1][,Arg2][,ArgN]) WScript.GetObject(pathToFileContainingAutomationObject[,programID][,eventPrefix]) WScript.Quit([errorCode]) WScript.Sleep(numberOfMilliseconds)

Properties
WScript.Arguments (Returns: WshArguments collection) WScript.BuildVersion (Returns: integer) WScript.FullName (Returns: string; full path to host executable) WScript.Interactive (Returns: Boolean) WScript.Name (Returns: string; name of WScript object) WScript.Path (Returns: string; directory where script host resides) WScript.ScriptFullName(Returns: string; full path to current script) WScript.ScriptName (Returns: string; name of current script) WScript.StdErr (Returns: StdErr stream) WScript.StdIn (Returns: StdIn stream) WScript.StdOut (Returns: StdOut stream) WScript.Version (Returns: script; script host version)
* From Microsoft PowerShell, VBScript and JScript Bible, Wiley Publishing, 2009

CLI_NotePad++_Settings.docx

18

Revision Date: 1111

Potrebbero piacerti anche