July 10th, 2013

Why is the syntax for touching a file from the command prompt so strange?

The magic incantation for updating the last-modified date on a file is

COPY /B FILE+,,

What strange syntax! What’s with the plus sign and the commas, anyway?

The formal syntax is the much more straightforward

COPY /B A+B+C+D

This means to start with the file A, then append the files B, C, and D, treating them all as binary files.

If you omit the B+C+D part, then you get

COPY /B A+

This means “Start with A, then append nothing.” The side effect is that the last-write time gets updated, because the command processor opens A for append, writes nothing, then closes the handle.

That syntax has worked since at least MS-DOS 2.1 (the earliest version I still have a virtual machine for).

I dont know where the two-comma version came from, but it most likely exploited a parsing glitch in COMMAND.COM, and somehow this variant gained traction and became the version everybody used (even though the other version is two keystrokes shorter). As a result, this weird syntax has become grandfathered as a special-case in the CMD.EXE parser. Here’s some actual code from the part of CMD.EXE which parses the arguments to the COPY command:

if (parse_state == SEEN_TWO_COMMAS)
    copy_mode = TOUCH;
Topics
History

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

0 comments

Discussion are closed.