1. Trang chủ
  2. » Công Nghệ Thông Tin

sams teach Yourself windows Script Host in 21 Days phần 2 ppsx

51 217 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 51
Dung lượng 1,69 MB

Nội dung

the system using the Drives collection. The output from the script is a text file called drives.log; you can see the results of running the script in Figure 3.4. Figure 3.4: The results of running the testfso.js script. Drive The Drive object represents a disk drive or network share. To use a Drive object, you must reference it from an instance of the FileSystemObject. You can’t instantiate it directly using CreateObject(). This also holds true for the Drives, Files, File, Folders, Folder, and TextStream objects. In the example for the Drive object, you see how to get it from the FileSystemObject and then use it to output information about the C: drive. Unlike the Dictionary and FileSystemObject, the Drive object has no methods. You can, however, use some of its properties to change the characteristics of drives on your system (specifically the Volume label). Drive Object Properties Table 3.5 Drive Object Properties Property Name Description AvailableSpace This returns the amount of space that is available to a user on a local drive or on a network share. DriveLetter This returns the drive letter of a local drive or a network share. It might return "" if the drive is a network share that hasn’t been mapped to a drive letter. DriveType This returns a string that indicates the type of a drive. Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com FileSystem This returns a string that indicates the file system of a drive. The types that can be returned are FAT, NTFS, or CDFS. FreeSpace This returns the amount of free space that is available on a local drive or network share. This might be different from the amount of available space if the drive is on a system that supports disk quotas. IsReady This indicates whether the drive is ready. If you access a drive that represents a floppy disk and no disk is in the drive, this returns false. Path This returns the path to the drive (for example, C:). RootFolder This returns a Folder object that represents the root folder of a drive. SerialNumber This returns the decimal serial number that is used to uniquely identify a disk volume. ShareName This returns the network share name for a drive. If the drive is a local drive, it returns "". TotalSize This returns the total space in bytes of a local or network drive. VolumeName This enables you to retrieve or set the volume name of a drive. Tip Make sure to use the IsReady() method in your JScript code before you attempt to access the properties of a drive. The current version of JScript suffers from a lack of good error handling capabilities, so if an error occurs when you attempt to access a drive, your JScript code just bombs out with an error. Support for enhanced error handling will be available in an upcoming JScript version release from Microsoft. Drive Object Methods The Drive object has no methods. Drive Object Code Sample The Drive object enables you to retrieve all the details for a drive. Here’s an example that illustrates how you can use it: Listing 3.3 drive.js—Obtaining Drive Details // FILE: drive.js // AUTH: Thomas L. Fredell // DESC: Illustrates the use of the Drive object to display information Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com // about a local or network drive. // // Copyright 1998 Macmillan Publishing // var args, fso, drv; // Retrieve arguments for the script, and check to make sure that the // user has specified a drive letter. args = WScript.arguments; if (args.length != 1) { // User didn’t specify arguments, so quit and display usage WScript.echo("USAGE: drive.js [driveletter]"); WScript.echo(""); WScript.echo(" Displays the information about a drive."); WScript.quit(); } // Now instantiate a FileSystemObject and use it to get the Drive object fso = WScript.CreateObject("Scripting.FileSystemObject"); drv = fso.Drives(args(0)); // Check to make sure that the drive is ready and quit if it isn’t if (!drv.isReady) { WScript.echo("Drive " + args(0) + " is not ready."); WScript.quit(); } // The drive is ready, so we should be able to display all of the // drive details WScript.echo("Drive " + args(0) + " details:"); WScript.echo(""); WScript.echo("availableSpace = " + drv.availableSpace); WScript.echo("driveLetter = " + drv.driveLetter); WScript.echo("driveType = " + drv.driveType); WScript.echo("fileSystem = " + drv.fileSystem); WScript.echo("freeSpace = " + drv.freeSpace); WScript.echo("isReady = " + drv.isReady); WScript.echo("path = " + drv.path); WScript.echo("rootFolder = " + drv.rootFolder); WScript.echo("serialNumber = " + drv.serialNumber); WScript.echo("shareName = " + drv.shareName); WScript.echo("totalSize = " + drv.totalSize); WScript.echo("volumeName = " + drv.volumeName); Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com The drive.js script uses a FileSystemObject to get access to the drives property, which is the Drives collection. The Drives collection can be indexed using a string much like the Dictionary. You use this capability to get a Drive object that corresponds to the argument that the user passed on the command line. Next, you output all the information about the Drive by echoing its properties to the screen. You can see the results of running the drive.js script in Figure 3.5. Figure 3.5: The results of running the drive.js script. File Object The File object represents a file stored on a local or network disk. Using a File object, you can retrieve all the properties of a file, you can open it to read or write as text, and you can copy, move, or delete the file. You can get an instance of a File object from either a FileSystemObject or Folder object. File Object Properties Table 3.6 File Object Properties Property Name Description Attributes This enables you to set or get the attributes of a file. It is a numeric value that uses a logical combination of the following values: 0—Normal; 1—ReadOnly; 2—Hidden; 4—System; 8—Disk drive Volume label; 16— Directory; 32—Archive; 64—Alias (like a link or shortcut); and 128—Compressed. DateCreated This returns the date and time that the file was created. Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com DateLastAccessed This returns the date and time that the file was last accessed. DateLastModified This returns the date and time that the file was last modified. Drive This returns the drive letter where the file resides. Name This can be used to either change or retrieve the name of a file. ParentFolder This returns the Folder object for the parent of the file. Path This returns the path for the file. ShortName This returns the name of the file under the short DOS 8.3 naming convention. ShortPath This returns the path to the file using the old DOS 8.3 naming convention. Size This returns the size of the file in bytes. Type This returns the type of the file based on the Windows association for the file’s extension. File Object Methods Table 3.7 File Object Methods Method Name Description Copy(dest, overwrite) This copies the file to a different location. If another file with the same name already exists at the destination, it is not overwritten unless the overwrite flag is specified as true. The overwrite flag is false by default. Delete(force) This deletes the file; the deletion fails if the file is read-only unless the force flag is specified as true. Move(dest) This moves the file to a different destination. If a file already exists at the destination, an error occurs. OpenAsTextStream (iomode, format) Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com ForReading—opens the file for reading only; ForWriting—opens a file for writing; ForAppending—opens a file and enables you to write to the end of the file. The format flag can be one of the following: TristateUseDefault— indicates that the file should be opened using the system default; TristateTrue—indicates that the file should be opened as Unicode; TristateFalse— indicates that the file should be opened as ASCII. File Object Code Sample The File object enables you to retrieve all the details for a file; here's an example that illustrates how you can use it: Listing 3.4 file.js—Obtaining File Details // FILE: file.js // AUTH: Thomas L. Fredell // DESC: Illustrates the use of the file object to display information // about a file. // // Copyright 1998 Macmillan Publishing // var args, fso, fil; // Retrieve arguments for the script, and check to make sure that the // user has specified a file. args = WScript.arguments; if (args.length != 1) { // User didn’t specify arguments, so quit and display usage WScript.echo("USAGE: file.js [filespec]"); WScript.echo(""); WScript.echo(" Displays the information about a file."); WScript.quit(); } // Now instantiate a FileSystemObject and use it to get the File object fso = WScript.createObject("Scripting.FileSystemObject"); fil = fso.getFile(args(0)); // Display the file details Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com WScript.echo("File " + args(0) + " details:"); WScript.echo(""); WScript.echo("dateCreated = " + fil.dateCreated); WScript.echo("dateLastAccessed = " + fil.dateLastAccessed); WScript.echo("dateLastModified = " + fil.dateLastModified); WScript.echo("drive = " + fil.drive); WScript.echo("name = " + fil.name); WScript.echo("parentFolder = " + fil.parentFolder); WScript.echo("path = " + fil.path); WScript.echo("shortName = " + fil.shortName); WScript.echo("shortPath = " + fil.shortPath); WScript.echo("size = " + fil.size); WScript.echo("type = " + fil.type); // Now call a function that outputs a listing of the // file’s attributes WScript.echo(""); ShowFileAttributes(fil); The first part of the example displays all of the information that we can gather from the properties of a file object. You’ll notice that we use a custom function, ShowFileAttributes(), to decode and display the information about the file attributes. The body of ShowfileAttributes() is listed below. // // FUNC: ShowFileAttributes(fil) // DESC: Decodes and shows the attributes for the // specified file object. // function ShowFileAttributes(fil) { var nAttribs; nAttribs = fil.Attributes; WScript.echo("File Attributes:"); if (nAttribs == 0) { WScript.echo("Normal"); return; } if (nAttribs & 1) WScript.echo("ReadOnly"); if (nAttribs & 2) WScript.echo("Hidden"); if (nAttribs & 4) WScript.echo("System"); if (nAttribs & 8) WScript.echo("Volume"); if (nAttribs & 16) Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com WScript.echo("Directory"); if (nAttribs & 32) WScript.echo("Archive"); if (nAttribs & 64) WScript.echo("Alias"); if (nAttribs & 128) WScript.echo("Compressed"); } This very simple script displays all the available information about a file, including its size, name, attributes, and more. First you instantiate a FileSystemObject; you use it to call the FileSystemObject.getFile() method, which returns a File object. Then you simply echo all the properties of the File object to the screen. Notice that, for simplicity’s sake, you’ve split out the code that checks the file attributes into a separate function called ShowFileAttributes(). The results of the script are illustrated in Figure 3.6. Figure 3.6: The results of running the file.js script. Folder Object The Folder object represents a folder (or directory) on a local or network disk. Using a Folder object, you can retrieve all the properties of a folder, you can retrieve a collection of the files or folders within it, and you can copy, move, or delete the folder. You can get an instance of a Folder object from either a FileSystemObject or Drive object. Folder Object Properties Table 3.8 Folder Object Properties Property Name Description Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Attributes This enables you to set or get the attributes of a folder. It is a numeric value that uses a logical combination of the following values: 0—Normal; 1— ReadOnly; 2—Hidden; 4—System; 8—Disk drive Volume label; 16—Directory; 32—Archive; 64—Alias (like a link or shortcut); and 128— Compressed. DateCreated This returns the date and time that the folder was created. DateLastAccessed This returns the date and time that the folder was last accessed. DateLastModified This returns the date and time that the folder was last modified. Drive This returns the drive letter where the folder resides. Files This returns a collection that lists the files stored in the folder. RootFolder This returns true if the folder is the root folder for a drive, otherwise it returns false. Name This can be used to either change or retrieve the name of a folder. ParentFolder This returns the folder object for the folder that contains the current folder. Path This returns the path for the folder. ShortName This returns the name of the folder under the short DOS 8.3 naming convention. ShortPath This returns the path to the folder using the old DOS 8.3 naming convention. Size This returns the size, in bytes, of all the files or subfolders of the folder. SubFolders This returns a collection of the folders that are contained within the folder. Folder Object Methods Table 3.9 Folder Object Methods Method Name Description Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Copy(dest, overwrite) This copies the folder to a different location. If another folder with the same name already exists at the destination, it is not overwritten unless the overwrite flag is specified as true. The overwrite flag is false by default. Delete(force) This deletes the folder and all files or subfolders that it contains; the deletion fails if the folder is read-only unless the force flag is specified as true. Move(dest) This moves the folder to a different destination. If a folder already exists at the destination, an error occurs. Folder Object Code Sample The Folder object enables you to retrieve all the details for a folder; here’s an example that illustrates how you can use it: Listing 3.5 folder.js—Obtaining Folder Details // FILE: folder.js // AUTH: Thomas L. Fredell // DESC: Illustrates the use of the folder object to display information // about a folder. // // Copyright 1998 Macmillan Publishing // var args, fso, fldr; // Retrieve arguments for the script, and check to make sure that the // user has specified a folder. args = WScript.arguments; if (args.length != 1) { // User didn’t specify arguments, so quit and display usage WScript.echo("USAGE: folder.js [folderspec]"); WScript.echo(""); WScript.echo(" Displays the information about a folder."); WScript.quit(); } // Now instantiate a FileSystemObject and use it to get the Folder object fso = WScript.createObject("Scripting.FileSystemObject"); fldr = fso.getFolder(args(0)); Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com [...]... returns collection that contains the parameters for à the script. à à à à à à FullNameà à This returns the full path to the host executable, including the name of the scripting host program (cscript.exe or Wscript.exe).à à Nameà à This returns the "friendly" name of the scripting host program (Windows Scripting Host) .à à Pathà à This returns the directory in which the scripting host program resides.à Ã... à à à Overviewà à The Windows Scripting Host provides two types of standard objects; there are objects that are part of the scripting engine, and there are objects that are specifically provided by the Windows Scripting Host In this chapter, you’ll learn about the Windows Scripting Host objects, which can be used to change network drive mappings, manipulate the Windows registry, create shortcuts, and... à ScriptFullNameÃà This returns the full path to the script that is currently running This includes the filename of the script and the path where the script resides.à à à à à à à à ScriptNameÃà ÃThis returns the filename of the currently executing script. à à à à à à à Versionà à This returns the version of the WSH scripting host program.Ãà à à à à à à à à à à à Wscript Object Methodsà à Table 4 .2 Wscript... tasks.à à Wscript Objectà à The root WSH object is the Wscript object It’s always available when a script is running in the scripting host, so you don’t have to create an instance of the object It’s not really a root object from an inheritance perspective, but it’s easy to think of it as such because it plays a central role for WSH scripts The Wscript object provides the capability to retrieve information... contains the Windows Start menu for the current user Next, you create a shortcut using the WshShell.CreateShortcut() method, which returns a new WshShortcut object You set the target path and description for the shortcut using the TargetPath and Description properties respectively Finally, you save the shortcut using the Save() method You can see the results of running the script in Figure 4.5; the Windows. .. the specified fileà fso = WScript.createObject("Scripting.FileSystemObject");à tstrm = fso.openTextFile(args(0), 1, false);à à // Read through the file line-by-lineà while (!tstrm.atEndOfStream) {à nLine = tstrm.line;à sLine = tstrm.readLine();à WScript.echo(nLine + ": " + sLine);à }à à à à à à à à à à à à à à à à à à à à à à à à à à à à à à à à à à This example uses an instance of the FileSystemObject... the scripting engine and WSH-specific objects You learned about the scripting objects on Day 3, "The Scripting Object Model," and in this chapter you'll learn about the WSH scripting objects.à à à à à à Ãà à Noteà This chapter assumes basic familiarity with Microsoft OLE and ActiveX OLE is the standard for embedding objects inside of applications; you may have used it to put a a spreadsheet excerpt inside... with most other scripting languages.à à à à To use the WshNetwork object, you need to instantiate an instance of the object using the Wscript.Network PROGID Your two basic options for doing so are to call either CreateObject("Wscript.Network") or Wscript.CreateObject("Wscript.Network") Property and method listings and examples follow to help you make use of the WshNetwork object in your scripts.à à WshNetwork... Echo(arg1, arg2, )à à à à à This enables you to echo information to the screen If Wscript is executing the script, the output pops up in a dialog box If cscript is executing the script, the output is echoed to the screen, such as the DOS ECHO batch file command.à à à à à Quit(errorcode)à à This enables you to terminate the execution of your script with a specific error code.à à à à à à à à à à à à Wscript... interface that enables you to develop external programs that control Excel Basically, any external program that can use à OLE automation objects can use Excel programmatically You can do it from Visual Basic, Delphi, or C++ In your example, you’re controlling it from a WSH script implemented using the VBScript language You could also do it using the JScript language in a WSH script; any WSH scripting . object Introduction to the WSH Object Model The Windows Scripting Host scripting environment provides basically two sets of objects: objects that are included with the scripting engine and. by the Windows Scripting Host. In this chapter, you’ll learn about the Windows Scripting Host objects, which can be used to change network drive mappings, manipulate the Windows registry, create. Windows Scripting Host provides two types of standard objects; there are objects that are part of the scripting engine, and there are objects that are specifically provided by the Windows Scripting

Ngày đăng: 13/08/2014, 08:21

TỪ KHÓA LIÊN QUAN