Administering Files and Folders

[ LiB ]

Administering Files and Folders

In addition to using JScript's runtime objects to create and process files, you can use them to automate a number of file and folder administration tasks , including copying, moving, and deleting files and folders. For example, you might want to delete files after they have reached a certain age.

Using methods that belong to the FileSystemObject , you can copy, move, or delete one or more file or folders. Optionally, you can use methods belonging to the File object to administer files and methods belonging to the Folder object to automate the handling of folders.

Working with the FileSystemObject Object

The FileSystemObject provides a large collection of methods that you can use to automate any number of file administration tasks. These methods include:

  • CopyFile() . Copies one or more files to a specified folder.

  • MoveFile() . Moves one or more files to a specified folder.

  • DeleteFile() . Deletes one or more files from a specified folder.

  • FileExists() . Determines whether a file contains any data.

In addition to methods that work with files, the FileSystemObject has methods for administering folders, as shown here:

  • CopyFolder() . Copies one or more folders to a specified folder.

  • MoveFolder() . Moves one or more folders to a specified folder.

  • DeleteFolder() . Deletes one or more folders from a specified folder.

  • FolderExists() . Determines whether a folder already exists.

  • CreateFolder() . Creates a new empty folder.

NOTE

TIP

Always remember to use the FileExists() and FolderExists() methods to determine whether or not a file or folder already exists before attempting to perform a file or folder operation.That way you can avoid any unnecessary errors by either creating the file or folder if it does not exist or by terminating the execution of your script.

Copying, Moving, and Deleting Files

The FileSystemObject object's CopyFile() method provides the capability to copy one or more files to a specified folder. To use this method, you must first instantiate the FileSystemObject , as demonstrated here:

 var fsoObject = new ActiveXObject("Scripting.FileSystemObject");  fsoObject.CopyFile("c:\MyDocs\Employee.txt", "c:\Temp\ Employee.txt"); 

In this example, a file named Employee.txt is copied from the computer's c:\MyDocs folder to its c:\Temp folder. You can modify this example as shown here to copy more than one file at a time.

 var fsoObject = new ActiveXObject("Scripting.FileSystemObject"); fsoObject.CopyFile("c:\MyDocs\*.*", "c:\Temp"); 

In this example, all the files located in the c:\MyDocs folder are copied to the c:\Temp folder.

NOTE

You can use either the * or the ? wildcard characters when using any of the File SystemObject methods.Wildcard characters enable you to select one or more files based on matching character patterns in file names .The ? character is used to match a single character, whereas the * character is used to match any number of characters. For instance, if you specify Jan3?.doc ,a match will occur with any 5-character file name that begins with Jan3 and has a .doc file extension. Similarly, if you specify Jan.*, any file with a file name of Jan will match, regardless of its file extension.

If you want to move a file to a different folder instead of copying it, you can use the FileSystemObject object's MoveFile() method, as demonstrated here:

 var fsoObject = new ActiveXObject("Scripting.FileSystemObject"); fsoObject.MoveFile("c:\MyDocs\Sample.txt", "c:\Temp\Sample.txt"); 

In this example, a file named Sample.txt located in the c:\MyDocs folder is moved to the c:\Temp folder.

You also can delete one or more files using the FileSystemObject object's DeleteFile() method, as demonstrated here:

 var fsoObject = new ActiveXObject("Scripting.FileSystemObject"); fsoObject.DeleteFile("c:\Temp\*.txt"); 

In this example, all files with a .txt file extension that reside in the c:\Temp folder are deleted.

Creating, Copying, Moving, and Deleting Folders

There is not too much of a difference in working with folders and working with files. Instead of using the FileExists() method, you use the FolderExists() method. Similarly, there are like-named methods for copying, moving, and deleting folders. For example, the following JScript statements demonstrate how to use the FolderExists() method to determine whether or not the c:\Scripts folder already exists.

 var fsoObject = new ActiveXObject("Scripting.FileSystemObject"); if (!fsoObject.FolderExists("c:\JScripts")) {   var newFolder = fsoObject.CreateFolder("c:\JScripts");   WScript.Echo("JScripts folder created on C:") } else {   WScript.Echo("Folder already exists!"); } 

In this example, the JScripts folder is created if it does not exist. Otherwise a message is displayed stating that the folder already exists.

To copy a folder, you can use the FileSystemObject object's CopyFolder() method. This method copies a folder and all of its contents, as demonstrated by the following example:

 var fsoObject = new ActiveXObject("Scripting.FileSystemObject"); fsoObject.CopyFolder("c:\JScripts", "d:\JScripts"); 

In this example, the c:\JScripts folder and all its contents are copied to d:\JScripts . If you change the name of the destination folder, you can copy and rename a folder in one operation, as demonstrated here:

 var fsoObject = new ActiveXObject("Scripting.FileSystemObject"); fsoObject.CopyFolder("c:\JScripts", "d:\Scripts"); 

Here the c:\JScripts folder is copied to the computer's D drive as Scripts .

You can use the MoveFolder() method to move a folder to a different location, as demonstrated here:

 var fsoObject = new ActiveXObject("Scripting. FileSystemObject"); fsoObject. MoveFolder("c:\JScripts", "c:\Temp\JScripts"); 

Like the CopyFolder() method, the MoveFolder() method moves the specified folder and all its contents to its new location.

You can delete folders using the FileSystemObject object's DeleteFolder() method, as demonstrated here:

 var fsoObject = new ActiveXObject("Scripting.FileSystemObject"); fsoObject.DeleteFolder("c:\Temp\JScripts"); 

In this example, the JScript subfolder and all its contents are deleted from the c:\Temp folder.

NOTE

Delete folders with great care.Remember that the folder and all its contents will be deleted. Also,be extra careful when using wildcard characters with the DeleteFolder() method to make sure that you don't accidentally delete folders that you did not intend to delete.

Working with the File Object

While the FileSystemObject object provides the capability to work with one or more files at a time, the File object is limited to just working with a single file at a time. In order to work with the File object, you must first instantiate the FileSystemObject and then use its GetFile() method to instantiate the File object. Once you have done this, you will be able to use any of the following File object methods:

  • Copy() . Copies a single file to a specified folder.

  • Delete() . Removes a single file from a specified folder.

  • Move() . Moves a single file to a specified folder.

The following example demonstrates how to use the File object's Copy() method to copy a file from one location to another:

 var fsoObject = new ActiveXObject("Scripting.FileSystemObject"); var sourceFile = fsoObject.GetFile("c:\JScripts\TestScript.js"); sourceFile.Copy("c:\Temp\TestScript.js"); 

In this example, a file called TestScript.js is copied from the c:\JScripts folder to the c:\Temp folder.

NOTE

The Copy() method works with one file at a time.Therefore, it does not support the use of wildcard characters.

The File object's Move() method works very similarly to the Copy() method, as demonstrated here:

 var fsoObject = new ActiveXObject("Scripting.FileSystemObject"); var sourceFile = fsoObject.GetFile("c:\JScripts\TestScript.js");  sourceFile.Move("c:\Temp\TestScript.js"); 

In this example, a file named TestScript.js is moved from the c:\JScripts folder to the c:\Temp folder.

The File object's Delete() method provides the capability to delete a single file, as demonstrated here:

 var fsoObject = new ActiveXObject("Scripting.FileSystemObject"); var sourceFile = fsoObject.GetFile("c:\Temp\TestScript.js"); sourceFile.Delete(); 

In this example, a file named TestScript.js is deleted from the c:\Temp folder.

Working with the Folder Object

The Folder object is very similar to the File object except that it works with folders instead of files. The Folder object provides access to several methods, which are listed here:

  • Copy() . Copies a single folder to a specified folder.

  • Delete() . Removes a single folder from a specified folder.

  • Move() . Moves a single folder to a specified folder.

NOTE

Because its methods only allow you to work with one folder at a time, you cannot use wildcard characters with any of the Folder object's methods.

The Folder object is limited to working with a single folder at a time. To use it, you must first instantiate the FileSystemObject object and then use the FileSystemObject object's GetFolder() method to set up a Folder object reference.

NOTE

Because the File and Folder objects support the same set of methods, it is easy to get mixed up when working with them.Take great care not to do this; otherwise, you might end up copying, moving, or deleting entire folders when you only meant to administer like-named files.

The Folder object's Copy() method copies a folder and all its contents to a new location. The following example demonstrates how to use this method:

 var fsoObject = new ActiveXObject("Scripting.FileSystemObject"); var sourceFolder = fsoObject.GetFolder("c:\JScripts"); sourceFolder.Copy("D:\JScripts"); 

In this example, the c:\JScripts folder is copied to the computer's D drive. As you probably expect, the Folder object's Move() method moves a folder and all its contents from one location to another, as demonstrated in the following example:

 var fsoObject = new ActiveXObject("Scripting.FileSystemObject"); var sourceFolder = fsoObject.GetFolder("c:\JScripts"); sourceFolder.Move("c:\Temp\JScripts"); 

Here the c:\JScripts folder is copied and made a subfolder of the c:\Temp folder. Finally, you can use the Folder object's Delete() method to delete a single folder, as demonstrated here:

 var fsoObject = new ActiveXObject("Scripting.FileSystemObject"); var sourceFolder = fsoObject.GetFolder("c:\Temp\JScripts"); sourceFolder.Delete(); 

In this example, the JScript subfolder and all its contents are deleted from the c:\Tempo folder.

[ LiB ]


Learn JavaScript In a Weekend
Learn JavaScript In a Weekend, Second Edition
ISBN: 159200086X
EAN: 2147483647
Year: 2003
Pages: 84

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net