Using XCOPY for Deployment
Microsoft uses a shorthand term for the ease-of-deployment features in ASP.NETit'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 newerthe 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.

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.

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.NOTEWhen 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.

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.NOTEMicrosoft 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.