![]() |
Table of Contents |
![]() |
Writing Your First ScriptBecause I don't expect you to plop down money to start scripting, I'm going to assume that you're either using Notepad or PFE as your script editor. I do highly recommend that you at least get PFE, because it's free and provides the all-important line numbering capability to your scripts. If you've decided to purchase another script editor, great! You shouldn't have any problems following along. For your first script, I've selected a sample that will tell you which user or users, if any, have a particular file open through a shared folder on a file server. This can be a handy tool to have when you're trying to get to a file that's partially locked because someone else has it open. Listing 2.1 shows the complete script, which you can type into a text file and save as WhoHas.vbs. Listing 2.1. WhoHas.vbs. Displays the user or users who have a file open.' first, get the server name we want to work with varServer = InputBox ("Server name to check") ' get the local path of the file to check varFile= InputBox _ ("Full path and filename of the file on the " & _ "server (use the local path as if you were at the " & _ " server console)") ' bind to the server's file service set objFS = GetObject("WinNT://" & varServer & "/lamanserver,fileservice") ' scan through the open resources until we ' locate the file we want varFoundNone = True ' use a FOR...EACH loop to walk through the ' open resources For Each objRes in objFS.Resources ' does this resource match the one we're looking for? If objRes.Path = varFile then ' we found the file - show who's got it varFoundNone = False WScript.Echo objRes.Path & " is opened by " & objRes.User End If Next ' if we didn't find the file open, display a msg if varFoundNone = True then WScript.Echo "Didn't find that file opened by anyone." end if NOTE You'll notice several lines that end in an underscore (_) character. The underscore is referred to as a line continuation character, meaning that the line of script is continued on the next line simply because it wouldn't all fit on the first line. When you type this script, you can type the underscore exactly as shown. You shouldn't have to make any changes to this script to get it to run in your environment, especially if you're running it on a Windows 2000 or Windows XP computer (the script does use Active Directory Services Interface, or ADSI, which is on 2000 and XP by default). NOTE Normally in this book, I'll follow each script with a detailed, line-by-line breakdown of what it does. Because this script is just meant to be an example of editing and debugging scripts, I'm going to forgo that explanation this time. I'll be using this script again later, though, so you'll still get that line-by-line explanation. TIP Remember that all of the longer scripts in this book are also on the accompanying CD-ROM, so you don't need to type them from scratch. Each chapter has its own folder on the CD, and the script filenames match the listing numbers (2.1 in this case). If you're using a script editor like PrimalScript or VbsEdit, take a moment to browse through the script. Notice how comment lines (those that begin with a single quote) appear in a different color, helping you to focus on them when you want an explanation of what the script is doing. Also, notice the coloring for statements and commands, and for strings of text that appear in dialog boxes. Get used to how your script editor works and you'll become a much more efficient scripter. |
![]() |
Table of Contents |
![]() |