Dela via


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

  • Anonymous
    February 25, 2005
    Once I started using robocopy, i havent looked back.
  • Anonymous
    February 26, 2005
    Where have you used copy in example 3?

    Why does Microsoft always fail to keep its promises...
  • Anonymous
    February 26, 2005
    >> 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.
  • Anonymous
    February 28, 2005
    The comment has been removed
  • Anonymous
    February 28, 2005
    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.
  • Anonymous
    February 28, 2005
    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...)