Using XCOPY for Deployment

for RuBoard

Microsoft uses a shorthand term for the ease-of-deployment features in ASP.NET ”it's called XCOPY deployment. This refers to a command that was brought into Windows from the DOS world. An evolved version of the DOS COPY command, XCOPY adds more powerful features, including the capability to create entire folders and subfolder structures where necessary. In situations in which you use the command to copy a folder and child subfolders , XCOPY can create identical folder structures on the destination disk.

Additionally, XCOPY has the capability to copy only those files that are newer than files on the destination drive. This is a big benefit for large sites that don't want to copy all 10,000 files each time they make a few changes, but it's an even more important feature for developers who frequently make little changes to several files and then forget which files are newer ”the ones on the development machine or the ones on the server. You can painstakingly compare the dates and times that each file in your application was last modified, but that's grunt work that the computer should take care of for you. XCOPY deployment performs that grunt work for you.

The ultimate goal of XCOPY deployment is to have an automated way to send changes from your development machine to your test server and on to your production machine when everything is all ready. With that in mind, we'll run through a few scenarios that demonstrate how to use XCOPY in real life. (At the end of this section, you'll find a quick reference to all XCOPY's options in case you ever need to do something more exotic.)

In our scenarios, we'll set up two folders on the same machine, C:\SOURCE and C:\TARGET. The objective in each case will be to copy some files (and, optionally , a directory structure) from one place to another. Figure 5.10 illustrates the state of the file system when we begin.

Figure 5.10. Initial state of file system before using XCOPY.

graphics/05fig10.gif

Deploying a Single Directory

To begin, we'll copy all the files from C:\SOURCE to C:\TARGET. To do this, use the command

 XCOPY c:\source c:\target 

When you execute this command, you'll get the following message:

 C:\source\file1.aspx C:\source\file2.aspx C:\source\web.config C:\source\deploy.bat 4 File(s) copied 

This means that all four files in C:\SOURCE were copied. So far, so good.

Deploying a Directory Tree

Now let's use a somewhat more realistic example. Suppose your Web site has a few subdirectories that contain binary components and images, as illustrated in Figure 5.11.

Figure 5.11. Folder with subfolders.

graphics/05fig11.gif

In this case, using XCOPY by itself won't do the trick, because you need to tell XCOPY to copy subdirectories (and files within them) as well. To copy this structure, use the command

 XCOPY c:\source c:\target /S 

The /S switch tells XCOPY to copy over all the files, including subdirectories and files within those subdirectories. The /S switch will work no matter how many files and how many directories (including subdirectories within those subdirectories) you have.

NOTE

When you're developing a site, you may want to consider using the /E switch in place of the /S switch. The /E switch creates subdirectories on the destination even if the subdirectories on the source are empty. The /S switch won't create empty subdirectories on the destination drive.

Creating empty subdirectories on the destination drive for a site in development can be useful for a number of reasons. For example, you might do it as a placeholder, to tell other developers working on the site that a subdirectory containing files is going to be created here eventually, but you haven't gotten around to creating files to go in it yet.


Excluding Files from Deployment

But wait; the file deploy.bat is your deployment script. It's not appropriate for that file to be deployed to the test Web server. We need to be able to tell XCOPY not to copy certain files. To do this, we need to create an exclude file, a text file that contains names of files we don't want XCOPY to touch.

You can create an exclude file using a text editor such as Notepad (or our favorite Notepad replacement, TextPad, available for download at www.textpad.com). In our example, the exclude file is called exclude.txt. It contains two entries: one for itself; the other for the deployment batch file, deploy.bat. Listing 5.5 contains the complete contents of exclude.txt.

Listing 5.5 Contents of a Sample XCOPY Exclude File
 exclude.txt deploy.batv 

You use the /EXCLUDE: switch with XCOPY to denote the existence of an exclude file. Therefore, to use the exclude file in our directory structure example, you would use the command

 XCOPY c:\source c:\target /EXCLUDE:exclude.txt /S 

The resulting structure would appear as illustrated in Figure 5.12.

Figure 5.12. File and directory structure after being copied with an exclude file.

graphics/05fig12.gif

Confirmations and Overwriting

If you've been experimenting with XCOPY without deleting all the files in the destination directory each time, you may have noticed that XCOPY gives you a warning when it's about to overwrite an existing file. You have two choices here: you can either lean on the Y key on your keyboard, or you can shut these warnings off altogether by using the /Y switch (also known as Yes mode).

For example, this command does the same thing as the previous example, but without the yes/no/always confirmation prompts:

 XCOPY c:\source c:\target /EXCLUDE:exclude.txt /S /Y 

You definitely want to use Yes mode in unattended (batch) XCOPY operations. Otherwise, there's a chance that XCOPY will sit there, possibly eternally, waiting for you to confirm a file overwrite.

NOTE

Microsoft changed the behavior of XCOPY (as well as the related commands MOVE and COPY ) in Windows 2000 to make the commands work the same way they did in DOS and Windows 95. Specifically, when copying files in Windows 2000, you'll get warning messages when attempting to overwrite existing files on the target drive. Use the /Y switch to avoid these messages. This means that if you wrote batch files that used XCOPY, MOVE , or COPY in Windows NT, you may need to change those batch files for Windows 2000.

For more information on this change, see Microsoft Knowledge Base article Q240268 located at http://support.microsoft.com/support/kb/articles/Q240/2/68.ASP.


Deploying Only Files with Changes

Now suppose you've made changes to a few files on your development machine (the source) and you want to copy to the destination only those files that have changed. This isn't a problem for small sites (just recopy the whole thing whether or not the files changed), but for larger sites (or slower network connections) it may take a long time to refresh the whole site.

You can use XCOPY to copy only those files that have changed by using the /D switch. The /D switch compares the date stamp on each file and copies those files that are newer than those on the target. The complete command looks like this:

 XCOPY c:\source c:\target /EXCLUDE:exclude.txt /S /Y /D 

If you're testing this on your machine, you can examine what happens by making a change to the file file1.aspx in your source directory and then executing this XCOPY command. XCOPY should copy the file you changed (and only the file you changed), returning the message:

 C:\source\file1.aspx 1 File(s) copied 

XCOPY Switches

Now that we've covered some common scenarios, Table 5.1 shows a list of all the switches available with XCOPY in Windows 2000.

Table 5.1. XCOPY Switches
Switch Description
/A Copies only files whose archive attribute has been set. (The archive attribute is a property of the file that tells backup utilities that the file has been backed up; if you use the archive attribute, you can use XCOPY as a sort of rudimentary backup system.)
/M Copies only files whose archive attribute has been set; turns off the archive attribute after copying.
/D Copies files that have been modified on or after a specified date. If you don't specify a date, XCOPY will copy only files that are newer than those on the target. An example is XCOPY /D:11-18-01.
/EXCLUDE:filename Excludes one or more files. To use this option, you should create an exclude file (described earlier in this section). Wildcard characters are not supported in an exclude file.
/S Copies entire subdirectories that contain files.
/E Copies subdirectories whether or not they contain files.
/V Verifies while copying. This causes the copy to take a little longer and is not normally needed for script files.
/P Provides a prompt before copying each file.
/W Prompts you to press a key before copying.
/C Continues copying even if errors occur.
/I Specifies the Destination of the copy.
/Q Quiet mode (suppresses display of filenames while copying).
/F Displays both source and destination filenames while copying. (By default, XCOPY displays only source filenames while copying.)
/L Lists files that would be copied (without actually copying them).
/H Copies hidden and system files. (By default, XCOPY does not copy these files.)
/R Overwrites read-only files in the target.
/T Creates the target directory structure that would result from a copy operation, but does not actually copy files.
/U Copies only files that already exist on the destination drive.
/K Retains the read-only attribute on copied files. (By default, XCOPY removes the read-only attribute from the files it copies. This can be useful in some situations ”most notably when copying files from a CD-ROM to your hard disk.)
/N Copies using short filenames. This is used for compatibility with 16-bit File Allocation Table (FAT) file systems (such as DOS and Windows 3.1).
/O Copies file ownership and Access Control List (ACL) information.
/X Copies file audit settings. This setting copies file ownership and ACL information as well.
/Y Answers "yes" to prompts that ask you whether you want to overwrite destination files.
/-Y Defines prompting before overwriting existing files on the target computer.
/Z Copies network files in restartable mode.
for RuBoard


C# Developer[ap]s Guide to ASP. NET, XML, and ADO. NET
C# Developer[ap]s Guide to ASP. NET, XML, and ADO. NET
ISBN: 672321556
EAN: N/A
Year: 2005
Pages: 103

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