When you want to copy a file into a folder, make sure you have a folder

Raymond Chen

This story is inspired by an actual customer problem.

The program LitWare.exe is used for TPS management, and when you want to create a new TPS report, you have to pick a cover sheet. The program shows you the cover sheets that have been defined, which it loads from the C:\TPS Cover Sheets directory.

The customer found that on one of the machines, the cover sheets weren’t showing up, even though the standard system setup copies a sample cover sheet into the C:\TPS Cover Sheets directory. The error message they got was Cannot load cover sheets. The directory name is invalid.

The customer did some troubleshooting and determined that “The cover sheet directory is missing, and we have a file instead.”

 Volume in drive C is INITECH
 Volume Serial Number is BAAD-F00D
 Directory of C:\
09/18/2006  02:43 PM                24 autoexec.bat
09/18/2006  02:43 PM                10 config.sys
03/18/2009  10:30 AM    <DIR>          Program Files
11/21/2008  01:04 PM             1,677 TPS Cover Sheets
02/20/2008  10:39 AM    <DIR>          Users
05/29/2009  02:23 PM    <DIR>          Windows
               2 File(s)          1,711 bytes
               3 Dir(s)  229,031,751,680 bytes free

One of my colleagues employed psychic powers to determine that at the time the customer tried to install the sample cover sheet on the machine, the C:\TPS Cover Sheets directory did not yet exist, and that the batch file they used to set up a new computer just does a copy \\server\TPSConfig\Sample.tps "C:\TPS Cover Sheets", which results in a file being created with the name C:\TPS Cover Sheets.

The customer was surprised by this conclusion. “I would think that copy will fail if the C:\TPS Cover Sheets directory doesn’t exist, but this might be our problem. We’ll look into it.” (I guess this customer never used the copy command to copy a file to a new name.)

If the destination of a copy command exists and is a directory, then the source files are copied into that directory. If the destination of a copy command does not exists or if it exists and is a file, then the destination is treated as a file name for the destination. (If there is more than one source file, then they are concatenated as if they were text files.)

The customer went back and checked the scripts, and the line they used was almost exactly what my colleague predicted:

copy "\\INITECH\Defaults\Sample cover sheet.tps" "C:\TPS Cover Sheets" /Y

If the C:\TPS Cover Sheets directory hasn’t been created yet, then that would explain the behavior they’re seeing: The copy command sees that the destination doesn’t exist and assumes you are doing a file-to-file copy (as opposed to a file-to-directory copy). In this case, the problem was that copying a sample cover sheet was a step they added to their setup scripts, but they added it before the step that creates the cover sheet directory. Reordering the two steps fixed the problem.


Discussion is closed.

Feedback usabilla icon