this session teaches you how to use this course

Upload: nitish2312

Post on 08-Apr-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/7/2019 This session teaches you how to use this Course

    1/31

    This session teaches you how to use this Course. In the Introduction we will:

    Learn to use the interactive Exercises on which the Course is based

    Create the BatchWindow we'll need for all the Lessons that follow

    Learn to use the clickable on-Screen Course Reference and List Box

    This Course is for Windows 95/98/ME users (click here Windows NT/2000/XP users). Andremember, you can work offline (for details, click here Work OffLine).

    Every journey begins with the first step, so let's go ...

    The Batch Language and Batch files

    The MS-DOS Batch Language is the set of commands you can use in a Batch file. It'san interpreted program language. You type commands to run them immediately ? or you canlist them in a text file (a Batch file or "script") to run them later.

    By the way, whenever you see this symbol ? or a term in purple, you can hover your mouse

    pointer over it to read extra details. Try it out on the previous paragraph ...

    We type commands for now, point-and-click later

    In a later Lesson, we'll see that Batch files can be run by point-and-click (often the best way to runthem). However, while we learn, we'll work with command lines typed at a prompt. How will youknow what to type? Click an Exercise button and see instructions in the corner of your screen.This Course works wholly on-screen (no print-outs needed).

    First preview, then click, then work through

    Let's start with some practice Exercises to see how this works. First, glance briefly at the previewpane below (it lets you know what to expect). Then click the button on the right, marked ex 1, anda small pop-up window will appear (for Exercise 1):

    ex 1

    This is the preview pane for Exercise 1. Each Exercise has a preview pane with either:

    Summary of main points (like this), or

    Results of running Batch commands, or

    Changes to the Batch file we're writing

    Glance at the preview pane before you do each Exercise. Review it briefly afterwards. This willhelp fix what you've learnt in your memory.

    Keeping track of which Exercise is next

  • 8/7/2019 This session teaches you how to use this Course

    2/31

    Notice that the ex 1 on the button for Exercise 1 has now greyed out ? to show that you'veclicked it. This is merely to help you keep track of which Exercises you've done so far. You canstill click a greyed -out button to recall a previous Exercise.

    If you refresh this page in your browser (don't do it now!), all the Exercise buttons will revert totheir unused normal appearance. So, for example, if you wanted to restart a Lesson from the

    beginning, you could refresh the page to reset all the Exercise buttons.

    Please work though all the Exercises

    You need to do all the Exercises in each Lesson (and in the order they occur). This is becausethe Course is really in the Exercise windows, not in this main Lesson window. Glance at thepreview pane below, then click the button (for Exercise 2):

    ex 2

    Exercise 2 preview:

    Each Exercise is one elementary step The Exercise window is small to leave room for other windows you'll work in

    You can resize the Exercise windows

    If a "figure" illustration is too large for its window, click its link again to see it all

    Work to the end of each Exercise, then come back to this main Lesson window

    Note We try to make this Course usable with most versions ? of Windows, from Windows 95onwards. The illustrations we show are normally for Windows 95. Later Windows versions aresimilar, but may have additional features or different wording.

    A Working Folder:Making a new folder for our Course work

    We'll need a folder on your hard drive to work in. We strongly suggest you make a new folder forthis, and you call itC:\CSW (the Lessons that follow all assume you will use this name). CSWmerely stands for Command-line Scripting in Windows, and (we hope) it's not a folder nameyou're currently using (if you are, choose another name).

    Make the C:\CSW folder

    You're probably used to making new folders in Windows ExplorerorMy Computer. This is aBatch Scripting Course, so let's make the folder in a completely different way.

    We can't use a Batch file to do it, because we don't know how to write one yet! We can't type a

    command at the prompt in the BatchWindow , because we haven't made thespecial BatchWindow yet that needs the C:\CSWfolder to be there first!

    A sneak preview of things to come

    So we'll take a sneak preview of some advanced syntax (from Lesson 2 and Lesson 7, and createour folder with a command executed in a single-use child shell in the Run dialogue box soon,you'll be using this jargon like an old hand!

  • 8/7/2019 This session teaches you how to use this Course

    3/31

    NT/2000/XP (Exercise 3 shows only Windows 95/98/ME version). Syntax to use:

    command /c md C:\CSW as in Exercise 3 (for Windows 95/98/ME)

    cmd /c md C:\CSW (alternative forWindows NT/2000/XP ? )

    Glance through the preview, then try it out for yourself (Exercise 3):

    ex 3

    For Exercise 3, you may prefer to read all the mini-pages first, before going back to try it out

    Open the Start , Run dialogue box

    Type: command /c md C:\CSW

    Click OK to run your first Batch line

    Use My ComputerorExplorerto confirm your new folder is now there

    (use F5 to refresh Explorerdisplay)

    Remember, if an illustration is too big for its pop-up window, click its hyperlink again:

    to see all the figure in a bigger window

    and again, to restore the default size

    (May be particularly useful for reading figure 2)

    NT/2000/XP Did you remember to use cmd if you're using Windows NT/2000/XP?Although command would have worked, it's better to get into the right habit now.

    A Working Window:The BatchWindow will be our main workplace

    Now we'll make our BatchWindow . This is a customized DVM). It will be similar to the MS-DOSPrompt window (installed in your Start + Programs menu). The difference is that we'llcustomize the BatchWindow to make it ideal for learning to write Batch files.

    A DVM is a Shortcut toCOMMAND.COM

    To make a new DVM, all we need do is create a Windows Shortcut to the programfile COMMAND.COM. This file is located in your Windows folder (there may be a copy of it inyour root folder, too, but it's the one in the Windows folder that we'll use).

    NT/2000/XP Although Windows NT/2000/XP includes a version ofCOMMAND.COM, for your

    Batch file work it's better to use CMD.EXE instead.

    In this Exercise, we'll work on the Desktop

    We'll create the new Shortcut on the Windows Desktop.

  • 8/7/2019 This session teaches you how to use this Course

    4/31

    NT/2000/XP (Exercise 4 shows only Windows 95/98/ME version). Syntax to use:

    C:\WINDOWS\command.com /k as in Exercise 4 (for Windows 95/98/ME)

    C:\WINDOWS\SYSTEM32\cmd.exe /k (alternative forWindows NT/2000/XP)

    In either case, if your Windows folder isn't C:\WINDOWS, use your own folder name. And be sure

    to type those slash characters: \ (path separator); and / (switch character) the right wayround (remember to hover your mouse over a purple term to read more).

    The preview gives you an idea of what we're about to do. Glance through it, but don't try tomemorize it, everything is in the Exercise window mini-pages (Exercise 4):

    ex 4

    Exercise 4 preview:

    We'll work on the Windows Desktop

    Minimize (don't close) this main Window

    Right-click Desktop for pop-up menu In pop-up menu, click New, Shortcut

    Type in the Shortcut program nameC:\WINDOWS\command.com /k

    Select Shortcut title: BatchWindow

    Don't try the new Shortcut yet, it's better to configure it properly first

    Remember, if an illustration is too big for its pop-up window, click its hyperlink again:

    to see all the figure in a bigger window

    and again, to restore the default size

    (Especially useful for looking at figures 4 and 6)

    NT/2000/XP Did you remember to use the alternative Windows NT/2000/XP syntax? If youforgot, delete the Shortcut you created and repeat Exercise 4. By the way, the Icon appearancewill be different inWindows NT/2000/XP.

    Note Our Shortcut is actually a tiny file, 967 bytes (characters) long. The full name Windows95/98/ME gives the Shortcut is BatchWindow.pif ? (but the extension isn't displayed).The /kswitch we used in our command line suppresses the Copyright message that wouldotherwise appear whenever you open the BatchWindow .

    Configuring a DVM Shortcut Properties

    We'll configure the settings, including TrueType fonts, for ourDVM Shortcut by usingits Properties sheet (a specialised editor window).

    NT/2000/XP (Exercise 5 shows only Windows 95/98/ME version). The layout of the PropertiesSheet items inWindows NT/2000/XP differs. The TrueType fonts are underLucida Console,and sizes are in points (inExercise 5, try about 20 points).

  • 8/7/2019 This session teaches you how to use this Course

    5/31

    We can open the Properties sheet by right-clicking on our new Desktop Shortcut without actuallyopening the DVMitself yet. We'll do this to configure some settings before we open and use thenew DVM (Exercise 5):

    ex 5

    Exercise 5 preview:

    Work on the Windows Desktop again

    Right-click BatchWindow , click Properties to see the editor window

    In Program Tab, we'll change the title, and the Working folder, and the Icon

    In Font Tab, we'll change the font (choose a TrueType font if you can)

    Remember: you can click again on any of the illustration links to make their window larger.

    Note 1 TrueType fonts are not supplied in Windows 95 unless you have the Microsoft Plus!Pack for 95 (choose Custom Install, Visual Enhancements, MS-DOS font option).

    Note 2 In Windows Millennium, CMDINIT.BAT is run automatically whenever a DVM opens.However, if you put anything in the Properties, Program Tab, Batch file field, your commandruns instead of the commands inCMDINIT.BAT.

    A review of window sizes

    Before we open the new BatchWindow , it's worthwhile reviewing a few things about windowsizes. As you know, an ordinary program window has three states:

    MaximizedA Maximized window occupies all your monitor screen (except for the Taskbar), and its

    control buttons look like this .Restored

    A Restored window occupies part of the screen. It's usually resizeable smoothly by

    dragging its edges with the mouse. Its control buttons look like this .Minimized

    A Minimized window doesn't occupy any Desktop space. To use it, you either maximizeor restore it with its button on the Taskbar.

    DVM window sizes

    A DVM window, such as the BatchWindow , works differently. And a DVM window also has anextra, fourth state (Full screen). A DVM window state can be:

    Maximized

    The size of a Maximized DVM is set by the Font size chosen. It won't normally fill yourscreen exactly. If the Font size is too large, it may even more than fill the screen (so yousee only part of the contents, and there are scroll bars). Its control buttons have the

    normal maximized look .Restored

    A Restored DVM window is resizeable smoothly by dragging its edges with the mouse.But again, its maximum size is set by your choice of Font. And it may more than fill thescreen (you see only part, and there are scroll bars) if the Font is too large. Its control

    buttons have the normal restored look .

  • 8/7/2019 This session teaches you how to use this Course

    6/31

    MinimizedA Minimized DVM works like other minimized windows. It doesn'toccupy any Desktop space, and you access it by clicking itsTaskbar button .

    Full screenA DVM can also be used "Full screen" a legacy mode ? ofyour monitor (we won't normally use it). To switch between thismode and the normal Windows

    screen, rememberthe Alt Enter key combination: to use it, you hold down Alt andpress the Enter or Return key. We'll practise Alt Enter switching shortly so you won'tforget it, in case you switch to Full screen by mistake!

    The BatchWindow on the initial open

    When you open the BatchWindow for the first time, it will typically open restored, so its control

    buttons appear as , and have scroll bars. It should look like this:

    NT/2000/XP In Windows NT/2000/XP, there is no Toolbar. You right-click on the Title bar,then click Propertiesto reach the corresponding CMD.EXE window controls.

    The Screen Layout:Window positions for easy use of the Course

    We need to lay out the screen properly, so that you can use the Course in the way intended(which the diagram shows).

    We'll have the main Lesson window taking up the full screen. Overlying it will bethe BatchWindow , the Exercise window, and any figure window (which will normally fit in thetop left).

  • 8/7/2019 This session teaches you how to use this Course

    7/31

    Text you'll need to type will be shown in the Exercise window. You copy this tothe BatchWindow (or a Notepad window, which we'll also keep in the bottom left corner of thescreen).

    With the windows arranged to fit properly, you'll find you can work quickly and smoothly. Switchbetween windows (or return to the main Lesson window) by clicking their Taskbar buttons. All the

    windows will "remember" their positions throughout a Lesson.

    Note The layout works for 800x600 monitor resolution (or higher). With this technique, you'llfind it's easy to maintain concentration, since there's no need to glance away from the screen.Indeed, if you work for long sessions in this way, you may consider using a lower ? resolutionthan you'd normally use for casual browsing, to avoid eyestrain.

    Maximizing and positioning properly

    When you first open the BatchWindow , it may partly cover the Exercise window. In Exercise6 we'll maximize it first (remember, for a DVM that's not Full screen size). Then we'll position itproperly. Its Shortcut will "remember" that size and position every time we open it at the start of aLesson. We'll work on the Desktop again (Exercise 6):

    ex 6

    Exercise 6 preview:

    Work on the Desktop once again

    Double-click BatchWindow Shortcut

    Drag window to fit just above Taskbar

    Don't close the BatchWindow , just leave it as a button on the Taskbar Return to this window, allowing it to cover the BatchWindow

    Remember: you can click again on any of the illustration links to make their window larger.

    Types of Exercises:There are three different types of Exercise

    We said earlier that there are three types of Exercise in this Course. They are:

    1. Mini-page: Preview shows a summary of steps (so you know what to expect)

    2. DVM: Preview shows the relevant results of running Batch commands3. Notepad: Preview shows relevant changes to a file we're writing in Notepad

    All the Exercises so far have been the Mini-page type. Now we'll try a DVM Exercise.

    DVM Exercises:DVM Preview shows commands and results

  • 8/7/2019 This session teaches you how to use this Course

    8/31

    In a DVM Exercise, the preview simulates the relevant part of the BatchWindow , showing thecommands you type, and the results you will see.

    Preview repeated

    The preview also appears in the Exercise window (after the instructions). You can scrollthe Exercise window down and compare the preview with your own results. It's easier thanlooking back at this main window (which would overlie yourDVM work).

    To type into a window, it must have focus

    When you scroll or click the Exercise window, you'll take focus off the BatchWindow (its Titlebar turns grey). You'll need to click on the BatchWindow again (or click its Taskbar button) toreturn focus to it (the Title bar will turn dark blue again) before you can continue typing any moretext or commands in it.

    Switching in and out of Full screen mode

    In this Course, we won't be using Full screen mode, but we'll practise switching in and out of itnow, in a DVMExercise (just to become familiar with how to switch modes).

    In Full screen mode, other windows are obscured (that's why we won't normally use it). We'll typea brief reminder (of how to swap back) in the BatchWindow before we switch. We'll prefix ournote with REM. Glance at the preview pane below, then click the button and try it out foryourself (Exercise 7):

    ex 7

    C:\CSW>rem alt-return swaps between full and windowed

    C:\CSW>_

    The CLS command:Clear BatchWindow display contents

    In the BatchWindow , you can clear clutter from previous Exercises with the CLS command(clearscreen). Click the Exercise button and try it now (Exercise 8):

    ex 8

    C:\CSW>rem alt-return swaps between full and windowed

    C:\CSW>cls

    C:\CSW>_

    Environment memory:Space for variables that a Batch file can use

    We can use simple variables in our Batch files to hold strings of characters. They're stored in aregion of memory reserved by COMMAND.COM, called the environment (and so they're known

  • 8/7/2019 This session teaches you how to use this Course

    9/31

    as environment variables). The default amount of memory for the environment is small, typicallyabout 512 bytes (one byte=one character).

    NT/2000/XP Windows NT/2000/XP users should skip Exercise 9, it's not needed.

    We'll use the BatchWindow Properties sheet to set a larger value, so we don't run into "out of

    environment space" errors while we learn. For our purposes, an environment size of 2048 byteswill be ample, so let's set that now(Exercise 9):

    ex 9

    Exercise 9 preview:

    Click BatchWindow Taskbar button

    Click ToolbarProperties button

    Switch to the Memory changes Tab

    Set 2048 bytes Initial environment

    Leave other memory settings at Auto

    Remember to click again on an illustration link if you need to make its figure window larger.

    Each DVM has its own environment setting

    Now all the Batch files we run in the BatchWindow will have enough memory for the variableswe'll be using. The memory setting we've just changed applies only to the BatchWindow . In alater Lesson, we'll learn how to configure Shortcuts to individual Batch files to run them by point-and-click with their own customised DVM settings.

    National Keyboards:Typing the right keys in the BatchWindow

    When Windows was installed on your PC, the right keyboard drivers were installed by the setupprocess, so that what you type is interpreted properly (always a good idea!).

    The DVM keyboard driver is different from the one used in the GUI, and it has to be set upseparately. This may have been done correctly on your machine or not! So our next task is tocheck if there's a problem. And if there is, to fix it.

    Note If you're using Windows Millennium, your keyboard driver should be correct. If itisn't, don't ? use our solution. Instead, read the Microsoft Knowledge Base document:Keyboard Command Does Not Work in Command Prompt (reference Q261417).

    NT/2000/XP You should have no keyboard problems; the keyb command discussednext isn't used. You can skip Exercise 10, Exercise 11, and Exercise 12 below.

    The US keyboard layout is the DVM default

    The default driver is for United-States-layout keyboards. And users in, for example, the UnitedKingdom, may find their systems are left with this default setup in a DVM window.

    The " and @ may not be rendered correctly

    http://support.microsoft.com/default.aspx?scid=kb;%5BLN%5D;Q261417http://support.microsoft.com/default.aspx?scid=kb;%5BLN%5D;Q261417
  • 8/7/2019 This session teaches you how to use this Course

    10/31

    The main QWERTY keys are common to most keyboard layouts, and there won't be problemswith them. It's only some marginal characters' keyboard positions that vary with the country. Forexample, trying to type the " (double-quote) and the @ ("at" symbol) will soon show whetherUnited Kingdom users have a keyboard driver problem.

    Let's try a test in the BatchWindow with anotherREM command (Exercise 10):

    ex 10

    C:\CSW>rem "quotes" and @ symbol test

    C:\CSW>_

    Fixing keyboard setup

    If your keystrokes are displayed properly, you don't need to fix anything (but you may want toread throughExercise 11 and Exercise 12 anyway, without making changes).

    Otherwise, let's fix the problem now.

    The command to change the DVM keyboard setup is: keyb ?? where the ?? is your two-letterCountry keyboard code. For the United Kingdom, the two-letter code is uk.

    Table for other countries

    The table below shows the two-letter codes for other countries in the main driver list:

    Country keyb Country keyb

    Belgium be Brazil br

    Canadian-French cf Czechoslovakia (Czech) cz

    Czechoslovakia (Slovak) sl Denmark dk

    Finland su France fr

    Germany gr Hungary hu

    Italy it Latin America la

    Netherlands nl Norway no

    Poland pl Portugal po

    Spain sp Sweden sv

    Switzerland (French) sf Switzerland (German) sg

    United States us Yugoslavia yu

    Users whose national keyboards are very different from the United States layout are more likelyto find their systems were already set up properly. So, as our example, we'll suppose a UnitedKingdom PC has been left inadvertently with the United States default driver (you must useyourown Country keyboard code for any changes, of course).

    Use ofkeyb command

    A user in the United Kingdom can configure the BatchWindow to use a UK format keyboard withthe commandkeyb uk

  • 8/7/2019 This session teaches you how to use this Course

    11/31

    If you're a United Kingdom PC user who saw " and @ transposed when you typed them,use keyb uk in the BatchWindow , then try the REM test again (Exercise 11):

    ex 11

    x x x(wrong line)

    C:\CSW>rem @quotes@ and " symbol test

    C:\CSW>keyb uk

    C:\CSW>rem "quotes" and @ symbol test

    C:\CSW>_

    If the keyb command corrected your typing

    Using the keyb command in a DVM affects only your current session. Any change is lost whenyou reboot. At this stage, we don't want to get involved with changing startup files, so we'llsimply configure the BatchWindow Properties sheet to run the keyb command for us any time

    we open the window.

    The change applies only to MS-DOS processes, such as the Batch file work we'll do in thisCourse. It doesn't affect any of your usual Windows programs.

    You need complete Exercise 12 only if the keyb command that you used actually did correcterrors in the way your keystrokes were rendered on screen (Exercise 12):

    ex 12

    Exercise 12 preview:

    Click BatchWindow Taskbar button Click ToolbarProperties button

    In Program Tab, Batch file field Enter the keyb uk command instead

    Click OK to save the change

    Don't do Exercise 12 unless the REM typing check failed when you tried it.

    Managing windows:That finishes configuring the BatchWindow

    We've finished configuring the BatchWindow . You're now ready to start the Course.

    We suggest you start each Lesson with no other windows open, so the Lesson button is in thefirst position on your Taskbar. You'll be switching between multiple windows, so it will help if youkeep to standard positions for the Taskbar buttons.

    Copying the Shortcut to yourPrograms menu

  • 8/7/2019 This session teaches you how to use this Course

    12/31

    At the beginning of each Lesson, you'll need to open the BatchWindow , making it the secondTaskbar button. To make this easier, you may wish to copy its Shortcut to your StartPrograms menu ? so you can launch the BatchWindow as you would any other program inyour Start menu.

    NT/2000/XP Location of folders are different (both Exercise 13 and Exercise 14 show only

    Windows 95/98/ME version), and if you're using Profiles, the locations are more complex. At thisstage of the Course, you may prefer to skip on to Exercise 17 now. The WindowsNT/2000/XP "All Users" locations are usually:

    C:\Documents and Settings\All Users\Desktop

    C:\Documents and Settings\All Users\Start Menu

    Copy to current folder

    If you're using profiles, folder locations are more complex, and you should skip ? to Exercise17 now. You can leave the BatchWindow Shortcut on your Desktop.

    You could copy the Shortcut with Windows Explorer, but since this is a Batch Course, let's do itfrom the BatchWindow command-line. We'll do this by sneaking a preview of two moreinstructions. We'll do it in three steps. Don't bother to remember the details at this stage, there'sample practice to come in the Lessons that follow!

    First we'll copy the Shortcut to our new folder (Exercise 13):

    ex 13

    C:\CSW>copy %windir%\desktop\batchwindow.pif1 file(s) copied

    C:\CSW>_

    Copy to Programs menu

    If you get it right, the COPY command reports that one file was copied. If you didn't, try again(check carefully against the line exactly as shown in the Exercise window). It's important to leavea Space between the word copyand the rest (the file specification). Don't leave any Space s inthe file specification part, however.

    We didn't say where to copy our Desktop Shortcut, so Windows copies it to the current folder(the one shown in the prompt ), which is our new C:\CSW folder.

    We didn't say what name to give the copy, either, so Windows gives it the same name

    (BatchWindow.pif) as the original. Now let's copy the Shortcut from C:\CSW to theStart

    Programs folder (Exercise 14):

    ex 14

    C:\CSW>copy batchwindow.pif "%windir%\start menu\programs"1 file(s) copied

    C:\CSW>_

  • 8/7/2019 This session teaches you how to use this Course

    13/31

    Delete the copy left in ourC:\CSW folder

    Again, if you get the syntax correct, the COPY command reports that one file was copied. If youdidn't, try again, checking carefully against the Exercise window. Once again, the Space s areimportant in the syntax.

    Since we didn't include a folder as part of the BatchWindow.pifname, Windows assumed thatthe file was in the current folder (and it was, since we'd copied it there).

    The COPY command will have left the copy Shortcut in C:\CSW, so let's delete it. It's alwaysworth keeping your disk free of unnecessary clutter (Exercise 15):

    ex 15

    C:\CSW>del batchwindow.pif

    C:\CSW>_

    The BatchWindow is in yourStart menu

    Now you can open the BatchWindow from your Start Programs menu. Let's test it now, sowe know it will be ready for Lesson 1 (Exercise 16):

    ex 16

    Exercise 16 preview:

    First close the BatchWindow

    Reopen it from Start, Programs

    Confirm the window reopens in the standard position and at the correct size you've set.

    On screen help:Use /? for quick help with Batch commands

    The syntax and the features of Batch commands aren't easy to remember when you arebeginning to learn them. Fortunately, all the commands have a standard system for getting quickhelp, and reminding yourself what features are available for any particular command. You simplytype the CommandName followed by /? (the help switch).

    Let's try it. The first command we used was MD to make a folder (Exercise 17):

    ex 17

    C:\CSW>md /?Creates a directory.

    MKDIR [drive:]pathMD [drive:]path

    C:\CSW>_

  • 8/7/2019 This session teaches you how to use this Course

    14/31

    NT/2000/XP The /? help is different from Windows 95/98/ME, and includes details of extrafeatures. InWindows NT/2000/XP it's particularly important to check the /? help.

    Help during the Course

    We'll be making use of the /? help throughout the Course. It's worth checking command details

    regularly in this way, until you become very familiar with them.

    However, the /? help doesn't show actual syntax examples. It shows an idealised pattern

    where [item] means itemis optional (it doesn't mean put brackets round the item when you usethe command). And you'll find that /? help uses this | that to mean eitherthisorthat, not both(it doesn't mean type a vertical bar between them).

    In other words, /? help can be confusing until you're more familiar with using commands! For this

    reason, we've added some extra help.

    Click a command for the Course Reference

    To provide more help during the Course we've added our own Command Reference. From nowon, whenever you see a command name in dark blue, you can use the Course Reference. To doso, click on the command itself. Try it,click on MD.

    Hmm... That doesn't add much to what we know, does it?

    The Course Reference isdynamic, it grows!

    Our Course Reference is dynamic: new information doesn't appear until you've learnt to makeuse of it. We haven't covered MD properly, so there's nothing much to say!

    The Course Reference List Box

    You'll also be able to use our Course Reference from a List Box at the top of each Lesson. It hasall commands covered up to (and including) those in the current Lesson.

    It's a pull-down box, try it now:

    To use it, click the tab to pull down the list, then click the item you're interested in.

    Of course, MD is the only Batch term in the list at this stage! And we won't be using MD properlyuntil a later Lesson, so after this Introduction, it will disappear until we do.

    More details will appear about each command

    With each Lesson, the Course Reference gradually shows more about each command. Advanceddetails appear only when basic ideas have been covered properly.

    The Reference List Box at the start of each Lesson includes commands to be covered in thatLesson. Use it to brush up on commands only after you've learnt about them in the Lessons, andwhen you want to practise on your own.

  • 8/7/2019 This session teaches you how to use this Course

    15/31

    Use Course Reference to review and revise

    The Lessons themselves lead you through new material. The clickable Reference is for youto review, revise, andexpand on what you've already learnt. And it adds to the informationthe /? system provides (it doesn't repeat or replace /? help), so use both.

    Now for the Lessons:You've been Introduced

    That completes the Course Introduction. At the top of each Lesson that follows, there's a link backto this Introduction (in case you need it), and a link to the Lesson Index, too.

    Lesson 1 has 34 exercises and takes about 70 minutes.Open the BatchWindow now, so it's thesecond button on the Taskbar, as explained in the Introduction ?

    You should complete the Introduction (which is really Lesson Zero) first. It shows you how tocreate and configure the customized MS-DOS BatchWindow and how to use our specially-designed multi-window layout.

    Remember, you can hover the mouse over ? orpurple items to read further details,and Work Offline if you prefer (click Work Offline for details). And bear in mind that thisCourse is for Windows 95/98/ME(click here Windows NT/2000/XP users).

    In Lesson 1, we'll cover some groundwork. This will also help you become fluent in using themultiple Course windows on screen. We shall:

    Use the ECHO command to write text to the screen

    Open and manage Windows processes with the start command

    Build simple scripts to learn how ECHOed text works in them

    Set and clear environment variables with the SET command

    Expand environment variables to use their contents

    You can click on any item in dark blue (such as ECHO) to read about it in the Course Referencewindow. Use it only to review and expand on what you've learnt in a Lesson. Don't try to learnabout new commands from their Reference window details.

    Commands can be immediate, or scripted

    http://www.allenware.com/icsw/icsw000.htmhttp://www.allenware.com/icsw/icsw000.htmhttp://www.allenware.com/icsw/icsw000.htmhttp://www.allenware.com/icsw/icsw000.htmhttp://www.allenware.com/icsw/icsw000.htm
  • 8/7/2019 This session teaches you how to use this Course

    16/31

    Most commands we'll use also work if you type them at the prompt inthe BatchWindow : this is calledimmediate mode. A command used in immediate mode iscarried out as soon as you press Return at the end of the line.

    Batch scripts will always run in Windows

    A Batch script, also known as a Batch file, is a list of commands. It's executed by a commandprocessor(a special program). Windows 95/98/ME uses the commandprocessorCOMMAND.COM (which is always installed so Batch files will always run).

    NT/2000/XP Although Windows NT/2000/XP includes a version ofCOMMAND.COM, forBatch work it's better to use CMD.EXE, since it has more powerful Batch features.

    Batch scripts are plain text files

    Batch scripts must be plain text files, created with a text editor. With a Word Processor, suchas WordPad, which saves files in a special format, you must choose the option:

    Save as type :Text documents otherwise your scripts will fail to work.

    We shall always use Notepad for our Batch work in this Course. However, unlikemost Notepad files (normally.TXT type), our files will have the extension .BAT this is howWindows recognises them as the Batch file type.

    NT/2000/XP In Windows NT/2000/XP you can use either.BAT or.CMD as the fileextension. CMD.EXE will interpret either file type. We use only .BAT in the Exercises, since thiswill work for both Windows 95/98/ME andWindows NT/2000/XP.

    Remember how these interactive pages work

    By the way, remember that you can:

    Click any dark blue term, such as ECHO, to look it up in the Course Reference

    Hover your mouse over any ? symbol, or any term in purple to see an InfoBox

    Displaying text:ECHO displays text messages on the screen

    A good script displays clear instructions and progress reports on the screen. The command todisplay text is ECHO, and almost all the scripts you write will use it.

    A "Hello World" Batch script to begin

    Batch scripts are computer programs. And the traditional way to begin writing computer programsis with a program that says: "Hello World". ECHO is just what we need. First, we'll practise inimmediate mode, in the BatchWindow , then write a simple script.

    The Exercise window avoids the need for notes

  • 8/7/2019 This session teaches you how to use this Course

    17/31

    When you click the Exercise 1 button on this page, the small Exercise window will appear in thetop right of the screen. The Exercise window has all the instructions you need for each Exercise(and often has extra information, too).

    The Exercise window also repeats the simulated screen images, which show you what you canexpect to see. This is so you can easily compare your results with ours, before you need to

    return to this main Lesson window (and lose sight of your results).

    Keep Exercise on top right, others bottom left

    The Exercise window is deliberately quite small, to allow you to read it while you work in otherwindows open in the main bottom left area of the screen. For this reason, you'll often need to usethe Exercise window scroll bar (that will appear on the right of it) to read all the details. Click thebutton now, and follow the instructions (Exercise 1):

    ex 1

    C:\CSW>echo Hello WorldHello World

    C:\CSW>_

    Making the immediate command into a script

    That line does what we want for our Hello World task, so let's make it into a script. It isn't a scriptyet, because we haven't typed it into a file. Our file will need the right Batch file extension, too, solet's call it HW.BAT forHello World.

    You could call it HelloWorld.BAT if you wished. However, we'll be running the Batch files wewrite in this Course by typing their names at the prompt, so we'll keep names short.

    A Batch script is just a simple text file, so the Windows Notepad text editor is all you need (feelfree to use your favourite text editor if you prefer, but we'll demonstrate with Notepad because it'salways part of Windows). You can start the Notepad editor with an immediate mode commandfrom the BatchWindow like this (click Exercise 2now):

    ex 2

    C:\CSW>start /min notepad HW.BAT

    C:\CSW>_

    Notepad starts up minimized as a button

    What happened was that Notepad created an empty file called HW.BAT, in the folderC:\CSW.Using the startcommand, as here, is neater than opening Notepad from the normal Programsmenu because this command line sets both the filename and folder for our script file.The Notepad window was minimized (=just a button on the Taskbar). That's because we usedthe /min (=minimized) switch of the start command. We did that so the Notepadwindow wouldn'tget in our way for the moment.

  • 8/7/2019 This session teaches you how to use this Course

    18/31

    If you found that confusing, or got into a tangle with the windows, try it again. To do so properly,you'll need to delete the HW.BAT file or you won't see the Notepad dialogue box asking if youwant the file created. If you do want to try again:

    Right-click the Notepad button on the Taskbar, and click Close

    In the BatchWindow , use del hw.bat to delete the blank file

    Repeat Exercise 2, now that you know what to expect

    Type the script in Notepad

    Our script consists of just one line: ECHO Hello World. It's important when typing scripts to

    make sure the last line ends with a Return , otherwise Windows 95/95/ME puts a spurious blankline on the screen when the script is run. Type the single line in Notepad, making sure youpress Return at the end, like this (Exercise 3):

    ex 3

    ECHO Hello World

    Remember to save the changes

    Note You must remember to click File, Save in Notepad to save any change we make. If youprefer, you can use Alt + F , S (=hold down Alt while you press F key, release both, thenpress S key). This is the keyboard alternative to the mouse method.

    Keep Notepad open to maintain window position

    Unlike our BatchWindow Shortcut, Notepad doesn't store its last-used window position, so keepit open during the Lesson, then you won't have to re-position it to read fromthe Exercise window.

    Don't use Notepad maximized (where its window would fill the display) or you won't be able toread the notes in the Exercise window.

    Run HW.BAT in the BatchWindow

    We've saved the script as HW.BAT. To run any .BAT script, we can just type its name at theprompt in the BatchWindow . Try it now. This is what you'll see (Exercise 4):

    ex 4

    C:\CSW>hw

    C:\CSW>ECHO Hello WorldHello World

    C:\CSW>

    C:\CSW>_

  • 8/7/2019 This session teaches you how to use this Course

    19/31

    Note We didn't add the .BAT extension, it's optional here. You would need to add the extensionif there were filesHW.COM orHW.EXE in ourCSW folder, since .COM and .EXE files takeprecedence with COMMAND.COM. In that case, we could specifyname and extension: hw.bat so as to ensure it was our Batch script that ran.

    NT/2000/XP You won't see as many spurious lines displayed.

    Turn offECHO to stop script lines echoing

    Our "Hello World" is in there, but amidst a lot of other clutter, too. When Windows runs our script,it displays each command in turn, then executes it. This is very useful when we're debuggingscripts, but it makes a mess on screen. We need to turn off echoing.

    Brief help with /?

    Remember that you can get brief help with most Batch commands by using the help switch (/?).

    Try using it withECHO in the BatchWindow now (Exercise 5):

    ex 5

    C:\CSW>echo /?Displays messages, or turns command-echoing on or off.

    ECHO [ON | OFF]ECHO [message]

    Type ECHO without parameters to display the current echo setting.

    C:\CSW>_

    Turning ECHO OFF and ON again

    As well as echoing text to screen, ECHO will turn off (and on) the display of commands in a Batchfile. Windows always starts with command ECHO turned on. ECHO OFF will turn it off. Inimmediate mode, ECHO OFFsuppresses the prompt that you normally see onscreen.

    Try that now. In the BatchWindow , type echo off, then press Return a few times. The promptis no longer displayed. Then type echo on to restore normality (Exercise 6):

    ex 6

    C:\CSW>echo off

    echo on

    C:\CSW>_

    ECHO OFF stops subsequent command lines displaying

  • 8/7/2019 This session teaches you how to use this Course

    20/31

    We need ECHO OFF in our script to prevent the script command lines themselves beingdisplayed as the script runs. It needs to be the first line of the script (because it affects only thelines that follow it). Add it to ourHW.BATscript (Exercise 7):

    ex 7

    ECHO OFFECHO Hello World

    Note The de-emphasized text such as this in the simulation marks the text already there,

    so that you can see clearly exactly where your new text goes.

    Run HW.BAT again

    With the change saved, run HW.BAT in the BatchWindow once more (Exercise 8):

    ex 8

    C:\CSW>hw

    C:\CSW>ECHO OFFHello WorldC:\CSW>_

    NT/2000/XP You may see an extra blank line displayed before the prompt returns in Exercise8 above, andExercise 10 (and following Exercises) below.

    Use the @ prefix to turn off echo of a single line

    This is better, but the ECHO OFF command itself still shows on screen. Its effect only applies to

    the lines thatfollow it. You can turn off display of a single line by typing the line with an @ prefix.So let's fix the problem in our script like this (Exercise 9):

    ex 9

    @ECHO OFFECHO Hello World

    Run HW.BAT once again

    With the change saved, run HW.BAT in the BatchWindow once again (Exercise 10):

    ex 10

    C:\CSW>hwHello WorldC:\CSW>_

    Most scripts will start with an@ECHO OFF

  • 8/7/2019 This session teaches you how to use this Course

    21/31

    Now the script writes "Hello World" to the screen, with no other clutter. We could have just usedthe @ command to prefix our original (single) line, making it:

    @ECHO Hello World

    However, while using @ to prefix each line is fine for a tiny script, we shall soon be writing longer

    ones.

    Most of our scripts will start with @ECHO OFF to prevent the display of each line of the script asit runs. Of course, while you are debugging a script, it can be useful not to use @ECHO OFF.Then, if there is a problem with the script, you will see what line is being executed when theproblem occurs.

    In debugging longer scripts, you can start with @ECHO OFF, then use the command ECHOON later, at any point, to turn on display of subsequent lines of the script as it runs. We don'tneed the @ prefix for the ECHO ON, since it affects only subsequent lines, and we would use itonly when echoing was already off.

    Used on its own, the ECHO command simply reports whether echoing (displaying) of script linesis currently on or off. Try it now, it will report that ECHO is on (Exercise 11):

    ex 11

    C:\CSW>echoECHO is on

    C:\CSW>_

    CLS clears the screen

    By the way, when you are working in immediate mode, and you want to clear previous work

    cluttering the screen, use the command CLS (clear screen). You'll find that clearing the screenfrom time to time makes it easier to work in a DVM window. It's probably about time you didso (Exercise 12):

    ex 12

    C:\CSW>cls

    C:\CSW>_

    Note The simulation shows you the effect, both before and after the CLS command.

    ECHO's dual role can upset text displays

    As we've seen, ECHO is used both for changing and reporting the echo state, and for displayingtext. In Windows 95/98/ME, these roles may conflict. Let's use Notepad to make a new scriptcalled HIGHNOON.BAT and see what can happen (Exercise 13):

    ex 13

  • 8/7/2019 This session teaches you how to use this Course

    22/31

    @ECHO OFFECHO Do not forsake me, Oh my darlingECHO On this our wedding day.

    Did you remember to press Return at the end of the final line? And to make sure the new file

    was saved in theC:\CSW folder?

    NT/2000/XP The ECHO command doesn't suffer from the conflict Windows 95/98/ME userswill see inExercise 14 Exercise 16 but you should work through anyway, so you will be aware ofthe problem should you need to use Windows 95/98/ME systems.

    Notepad: remember to take care with file types

    Care When using Notepad, be careful about file types when yousave .BAT files. Notepad would normally add a spurious extra .TXT extension if we saved ourscript as type Text Documents (the script would then fail to work until we renamed it properly).

    You might have noticed that this didn't happen when we saved HIGHNOON.BAT in Exercise 13,even though we left the Save as type :Text documents. This was because we had previouslyopened Notepad with a .BAT file when we started.

    Notepad remembers an extension already used in the current session (while you keep its windowopen) and doesn't append a spurious .TXT to it (however, just to be sure, make certainthere isn't a spurious extra .TXT extension added to HIGHNOON.BAT)

    If you're unsure when saving a file with Notepad, you can avoid a spurious .TXT extension beingadded if you:

    Set the Save as type : pull-down box to All files (*.*) before saving the file (this will workonly for registered file extensions)

    Enclose the filename in "quotes" (this works for all file extensions, whether or not they'reregistered in HKEY_CLASSES_ROOT in the registry)

    Files with .BAT extension are registered in HKEY_CLASSES_ROOT by default.

    NT/2000/XP The alternative .CMD extension is also registered by default.

    HIGHNOON.BAT doesn't work properly

    With changes saved, try running HIGHNOON.BAT to see what happens (Exercise 14):

    ex 14

    C:\CSW>highnoonDo not forsake me, Oh my darlingONC:\CSW>_

    NT/2000/XP You won't see this conflict in Windows NT/2000/XP, both lines will displaycorrectly. However, it's worth being aware of the problem, in case you need to write Batch filesmeant to run under Windows 95/98/ME andWindows NT/2000/XP

  • 8/7/2019 This session teaches you how to use this Course

    23/31

    The initial word "On" produced a conflict

    Remember that our script is:

    @ECHO OFFECHO Do not forsake me, Oh my darling

    ECHO On this our wedding day

    The last line produces an error because it starts with the word On, which is one ofthe ECHO commands (a line of text starting with Offwould also produce an error). In fact thefaulty line doesn't even turn echo on, it just writes a spurious ON. This is because any other textfollowing an ON orOFF instruction forECHO produces confusion.

    Using the ECHO. syntax is a reliable technique

    The standard way to separate ECHO from the following text to be displayed is with a Space . Thefirst Space following ECHO is always ignored (so if you need your displayed text indented bysay, five Space s, you must put six Space s in your script)

    There are eleven other special characters you can use immediately afterECHO that, just like thefirst Space , are not themselves echoed.

    They are: Comma ; = : and Period / \ + [ ] " .

    The first four work like a Space : they don't stop the action of a following ON orOFF. However,any of the last seven stop these words being treated specially (this is exactlywhat HIGHNOON.BAT needs). The usual one to use is the Period , thus ECHO.

    Instead of trying to remember to use a Period whenever ourECHO text begins with an on oroff,we'll always useECHO. whenever we display text. The only time we won't use it is when we really

    do want to turn echo on or off.

    NT/2000/XP When these Comma ; = : Period / \ + [ ] are used immediatelyafterECHO, they aren't themselves displayed (but there isn't any ON/OFF conflict to group themby). However, you can't use " immediately afterECHO in this way. In WindowsNT/2000/XP the / works best (fastest) immediately afterECHO.

    Correcting our script

    Now let's alter ourHIGHNOON.BAT script by using ECHO. each time we merely display text.Change it to this(Exercise 15):

    ex 15

    @ECHO OFFECHO.Do not forsake me, Oh my darlingECHO.On this our wedding day.

    The wedding's back ON

  • 8/7/2019 This session teaches you how to use this Course

    24/31

    With changes saved, run the corrected version ofHIGHNOON.BAT. This time you will see thetext displayed as we intend (Exercise 16):

    ex 16

    C:\CSW>highnoon

    Do not forsake me, Oh my darlingOn this our wedding day.C:\CSW>_

    Use ECHO. on its own to create a blank line

    Remember, the initial Period is not displayed, it just ensures that the rest of the line is treated asordinary text. You can use ECHO. on a line alone to create a blank line on screen. This is often

    useful in scripts to break up text and make screen output clearer. Let's try that inourHIGHNOON.BAT script. We'll make a blank line display both above and below the two textlines; make the changes in Notepad (Exercise 17):

    ex 17

    @ECHO OFFECHO.ECHO.Do not forsake me, Oh my darlingECHO.On this our wedding day.ECHO.

    The final showdown

    Did you remember to press Return after you added the second ECHO.? If you didn't, do so now.

    It is the (new) last line of the script, and a script should always end with a properly terminated

    line, one that ends with a Return . You don't need to add any further blank lines afterwards, it'ssufficient that the last line of every script is properly terminated (this prevents a spurious blankline in the display when the script runs).

    With changes saved, run the script a last time (Exercise 18):

    ex 18

    C:\CSW>highnoonDo not forsake me, Oh my darlingOn this our wedding day.

    C:\CSW>_

    That's enough ECHOing for now. When you've completed Exercise 18, use the CLS command inthe BatchWindow to clear the DVM screen.

    Variables:Stored in environment

  • 8/7/2019 This session teaches you how to use this Course

    25/31

    In the second half of this Lesson, we'll learn to use Environment Variables. These are a way ofstoring small pieces of text in a special area of memory called the environment. They are muchsimpler than the variables in most programming languages; there are only three principal thingsyou can do with them:

    Set them (normally with the SET command)

    Clear them (by setting them as blank)

    Use them as part (or all) of a text string

    Nevertheless, they are the basis of many powerful script techniques. Any variable you set ina DVM normally retains its value until it's cleared, or reset, or you end your current DVM session(close the DVM window).

    Setting a variable

    A variable name can contain most normal characters, including letters and digits; but you shouldavoid variable names starting with a digit, they conflict with replaceable parameters (which weshall learn about in later Lessons). To set a variable named SRC to, say C:\BKTEST (afoldername we shall later use for testing), you useSET in the DVM window like this (Exercise 19):

    ex 19

    C:\CSW>set src=C:\BKTEST

    C:\CSW>_

    Expanding a variable

    The name of the variable, src, is changed to uppercase as it's stored, so it doesn't matterwhether you use upper or lowercase in the SET command. However the case of letters in the

    contents (the part on the right of the = sign) is preserved.

    In our scripts, we'll always use uppercase for variable names to help them stand out:eg SRC (this is just a convention). We don't bother when simply working at the prompt.

    To make use of the current contents of a variable, that is, to expand the variable contents, yousurround the name with % characters. To expand the variable we have just set, src, you would

    type %src%. When you type %src% at the prompt (or in a script), it's as if you had used the exact

    text stored in the variable. Try it (Exercise 20):

    ex 20

    C:\CSW>echo.The folder is %src%The folder is C:\BKTEST

    C:\CSW>_

    This is useful where, say, a foldername is going to be used many times throughout a script. Wecan set it in a variable once at the start, then use the variable throughout the script. If we want tochange the folder later, we need only change the initial assignment.

    Storing messages

  • 8/7/2019 This session teaches you how to use this Course

    26/31

    Variables are often used to store messages, for example (Exercise 21):

    ex 21

    C:\CSW>set task=Build fresh test folder

    C:\CSW>_

    Expand the message

    Now try using the message, by expanding the variable, like this (Exercise 22):

    ex 22

    C:\CSW>echo.%task% - finishedBuild fresh test folder - finished

    C:\CSW>_

    Global variables

    Environment variables can be set in the Windows startup files; then they're available wheneveryou open a DVMwindow. These are called global variables (there is also a utility, WINSET.EXE,provided on the standard Windows CD-ROM, to set global variables). Windows normally setssome important global variables for you:

    winbootdirThe path to your windows folder, set in real-mode and in the Windows GUI. Usually setto C:\WINDOWS; this setting is taken from the file MSDOS.SYS.

    COMSPECThe path to COMMAND.COM, the command processor that runs our.BAT scripts. It's

    usually set toC:\WINDOWS\COMMAND.COM in Win95/98/MENT/2000/XP COMSPEC usually set to C:\WINDOWS\SYSTEM32\CMD.EXE

    PATHIf you ask to run a program (or a script), Windows searches certain folders for it bydefault. The list of foldernames Windows searches is set in the PATH variable, witha ; between each, usually:C:\WINDOWS;C:\WINDOWS\COMMANDNT/2000/XP PATH will include C:\WINDOWS;C:\WINDOWS\SYSTEM32

    TEMPThe path to the temporary folder. It's usually set to C:\WINDOWS\TEMP

    PROMPTThis specifies how the prompt looks. Usually set to $p$g. The $p means display currentdrive and path tocurrent folder, and $g means display a > symbol

    windir

    The path to your windows folder, set only in the Windows GUI. Usually setto C:\WINDOWSYou'll often see another variable. Unlike the global variables above, however, it's transient, andlocal to yourDVM:CMDLINE

    Holds the last command line used for an external command. This may be a commandyou typed in immediate mode, or one used in a script. Any internal commands youuse don't set CMDLINE, so it will probably still be set to: CMDLINE=start /min notepadHW.BAT (start is an external command).NT/2000/XP start is an internal command, so it won't affect CMDLINE

  • 8/7/2019 This session teaches you how to use this Course

    27/31

    Note CMDLINE is allocated a separate memory region by Windows, so it doesn't take up yourprecious environment space. Therefore, unlike other variables created transiently, there's noneed to clear it.

    System variables with lowercase names

    As we said, names of variables you set with the SET command are always forced to uppercase.However, Windows sets the two variables winbootdirand windirwith lowercase names. InWindows 95/98/ME, the SET command willnot clear variables whose names contain anylowercase letters (so they are write-protected).

    List current variables

    You can list all the variables currently set (including the global ones) by using the SET commandon its own. Try it now (Exercise 23):

    ex 23

    C:\CSW>setwinbootdir=C:\WINDOWSCOMSPEC=C:\WINDOWS\COMMAND.COMPATH=C:\WINDOWS;C:\WINDOWS\COMMANDTEMP=C:\WINDOWS\TEMPPROMPT=$p$gwindir=C:\WINDOWSCMDLINE=start /min notepad HW.BATSRC=C:\BKTESTTASK=Build fresh test folder

    C:\CSW>_

    The first six shown are the usual system variables. You may have more listed (or some set todifferent values). The two we've just set are at the end of the list: SRC and TASK.

    NT/2000/XP The normal SET list contains more variables.

    If you have a printer attached to your system, and you want to print out a complicated screenful oftext (such as the variables' listing above) from the BatchWindow , click onthe BatchWindow button on the Taskbar (so that it's the window with focus), and press the keycombination Shift + PrintScrn . This works for any DVM window.

    Clear the TASK variable

    Now try clearing one of the variables we set. You simply set it to nothing. Make sure you don't put

    a space after the = sign. Clear the variable TASK like this (Exercise 24):

    ex 24

    C:\CSW>set task=

    C:\CSW>_

    Check that it's gone

  • 8/7/2019 This session teaches you how to use this Course

    28/31

    Now list the variables that are set again, and check that TASK has been cleared from the currentlist (Exercise 25):

    ex 25

    C:\CSW>set

    winbootdir=C:\WINDOWSCOMSPEC=C:\WINDOWS\COMMAND.COMPATH=C:\WINDOWS;C:\WINDOWS\COMMANDTEMP=C:\WINDOWS\TEMPPROMPT=$p$gwindir=C:\WINDOWSCMDLINE=start /min notepad HW.BATSRC=C:\BKTEST

    C:\CSW>_

    A clear variable expands to nothing at all

    If you use a variable that is clear, Windows treats it as if you typed nothing at all. Try usingourTASK message again (Exercise 26):

    ex 26

    C:\CSW>echo.%task% - finished- finished

    C:\CSW>_

    NT/2000/XP When you expand the undefined variable in Windows NT/2000/XP, you seeliteral text instead of a blank space: %task% - finished

    ClearSRC variable

    Now clear the SRC variable in the same way (Exercise 27):

    ex 27

    C:\CSW>set src=

    C:\CSW>_

    Check SRC has gone

    Now list the variables that are set again, and check that SRC has been cleared from the currentlist (Exercise 28):

    ex 28

    C:\CSW>setwinbootdir=C:\WINDOWSCOMSPEC=C:\WINDOWS\COMMAND.COMPATH=C:\WINDOWS;C:\WINDOWS\COMMAND

  • 8/7/2019 This session teaches you how to use this Course

    29/31

    TEMP=C:\WINDOWS\TEMPPROMPT=$p$gwindir=C:\WINDOWSCMDLINE=start /min notepad HW.BAT

    C:\CSW>_

    YourSET list will differ

    Remember, there may be other variables set on your system than the standard ones we list here,and some of your standard variable contents may differ slightly from this list.

    NT/2000/XP start is an internal command, so it won't appear in CMDLINE

    The standard variables are often used in scripts

    The point of these standard variables is that your scripts can use, say the temporary folder, byreferring to %TEMP%, without having to worry exactly what folder it is.

    If you do this, your scripts will run properly on other people's machines that may have thetemporary folder in a different location. This is important, because we shall use this folder formost of our transient working files.

    Take care with spaces

    Variable names (and their contents) can contain Space s, but beware of accidents! For example:we'll set a variable called Norma Jean to the text value Marilyn Monroe. But, we'll make amistake (trying to be neat) and leave a Space either side of the = sign when we assign thevariable (Exercise 29):

    ex 29

    C:\CSW>set Norma Jean = Marilyn Monroe

    C:\CSW>_

    The Space s are used

    The text value now includes an initial Space thus: Marilyn Monroe. That's not really a

    problem, we may even want it that way. However the variable name now includes a final space,thus Norma Jean , and it's easy to miss this detail.

    If you didn't notice, or forgot, and tried to use the variable name without its accidental

    trailing Space , it wouldn't work (Exercise 30):

    ex 30

    C:\CSW>echo.Goodbye %norma jean%Goodbye

    C:\CSW>_

  • 8/7/2019 This session teaches you how to use this Course

    30/31

    NT/2000/XP When you expand the undefined variable in Windows NT/2000/XP, you seeliteral text instead of a blank space: Goodbye %norma jean%

    Check with SET

    There is no variable with that name, so we draw a blank. Check the variable list with a

    plain SET command(Exercise 31):

    ex 31

    C:\CSW>setwinbootdir=C:\WINDOWSCOMSPEC=C:\WINDOWS\COMMAND.COMPATH=C:\WINDOWS;C:\WINDOWS\COMMANDTEMP=C:\WINDOWS\TEMPPROMPT=$p$gwindir=C:\WINDOWSCMDLINE=start /min notepad HW.BATNORMA JEAN = Marilyn Monroe

    C:\CSW>_

    Include the Space to use the variable

    The SET list shows that the NORMA JEAN variable name includes a Space at the end: you

    must include it in the name in order to use the variable, like this (Exercise 32):

    ex 32

    C:\CSW>echo.Goodbye %norma jean %Goodbye Marilyn Monroe

    C:\CSW>_

    Note 1 Any leading Space s typed before the variable name don't matter, since SET alwaysignores them.

    Note 2 Any Space s in the variable content (to the right of the = ) are part of itscontent, including Space s at the end of the line.

    Clear the variable

    Let's clearMs Bakernow. Don't forget the Space before the = sign (Exercise 33):

    ex 33

    C:\CSW>set Norma Jean =

    C:\CSW>_

    Goodbye Norma Jean

  • 8/7/2019 This session teaches you how to use this Course

    31/31

    Check that she's gone (Exercise 34):

    ex 34

    C:\CSW>setwinbootdir=C:\WINDOWS

    COMSPEC=C:\WINDOWS\COMMAND.COMPATH=C:\WINDOWS;C:\WINDOWS\COMMANDTEMP=C:\WINDOWS\TEMPPROMPT=$p$gwindir=C:\WINDOWSCMDLINE=start /min notepad HW.BAT

    C:\CSW>_

    NT/2000/XP start is an internal command, so it won't appear in CMDLINE

    We'll take our cue from Norma Jean, and take our leave for this Lesson, too.