Tricks around the “copy” command…


The copy command can be very useful for quick operations. However, there are a number of tricks that are not widely known.

1) Copying the contents of a source directory into the current directory: just specify the source directory!

y:\privates>copy C:\WINDOWS\system32\clients\twclient\x86
C:\WINDOWS\system32\clients\twclient\x86\twcli32.msi
        1 file(s) copied.

2) Creating a text file without an editor:

y:\privates>copy con sample.txt
This is a text file.
Another line…
^Z
        1 file(s) copied.

y:\privates>type sample.txt
This is a text file.
Another line…

3) Creating a zero-size file

C:\>copy nul empty.txt
        1 file(s) copied.

C:\>dir empty.txt
 Volume in drive C has no label.
 Volume Serial Number is FCCD-E1D0

 Directory of C:\

02/26/2005  10:38 PM                 0 empty.txt
               1 File(s)              0 bytes
               0 Dir(s)  24,429,268,992 bytes free

4) Appending to a text file. Note that /A flag

y:\privates>copy /A abc.txt + con
abc.txt
con
BBB
^Z
        1 file(s) copied.

y:\privates>copy /A abc.txt + con
abc.txt
con
CCC
^Z
        1 file(s) copied.

y:\privates>type abc.txt
AAA
BBB
CCC

[update: item (3) above is fixed now.]

Comments (6)

  1. Once I started using robocopy, i havent looked back.

  2. Two Pak says:

    Where have you used copy in example 3?

    Why does Microsoft always fail to keep its promises…

  3. Adi Oltean says:

    >> Where have you used copy in example 3?

    Thanks for pointing this out. I fixed the missing copy text.

    >>> Why does Microsoft always fail to keep its promises…

    This blog represents my personal opinions only… and any mistakes here are mine.

  4. Robin says:

    Is ‘nul’ a system level ‘file’ or ‘directory’?

    ie, can you do things like

    move file.ext nul

    to delete file.ext?

    Sorry, I don’t have access to a Windows box to try it out…

  5. Adi Oltean says:

    NUL is a special "file", similar with /dev/null from Unix. Writing data to this file causes the data to be lost. Reading "data" is not possible as you will get always zero bytes at every attempt to read data from it.

    Enumerating the file fails (however, note the . prefix discovered by the DIR command…)

    Z:nt3driversstoragevolsnapvssserverhwsupport2test>dir nul

    Directory of .

    File Not Found

    Moving a file into NUL is not supported. You can copy data into NUL, which means that the data will be lost.

  6. adam says:

    I tried renaming a text file nul.txt (from Explorer rather than the prompt), which fails. The error message is rather cryptic though! Source file may be in use???

    What other special files are there? (Probably material for another blog entry rather than a comment…)