Index | Syntax | Notes XCOPY | DISKCOPY



COPY--Examples

The following command copies a file and ensures that an end-of-file character is at the end of the copied file: copy memo.doc letter.doc /a To copy the NOTE.TXT file from the current drive and directory to the directory MYNOTES, and to prevent MS-DOS from prompting you before overwriting the destination file (if it already exists), type the following command: copy note.txt mynotes /y To copy a file named ROBIN.TYP from the current drive and directory to an existing directory named BIRDS that is located on drive C, type the following command: copy robin.typ c:\birds If the BIRDS directory doesn't exist, MS-DOS copies the file ROBIN.TYP into a file named BIRDS that is located in the root directory on the disk in drive C. To copy several files into one file, list any number of files as source parameters on the COPY command line. Separate filenames with a plus sign (+) and specify a filename for the resulting combined file, as the following example shows: copy mar89.rpt + apr89.rpt + may89.rpt report This command combines the files named MAR89.RPT, APR89.RPT, and MAY89.RPT from the current drive and directory and places them in a file named REPORT in the current directory on the current drive. When files are combined, the destination file is created with the current date and time. If you omit destination, MS-DOS combines the files and stores them under the name of the first specified file. For example, if a file named REPORT already exists, you can use the following command to combine all four files in REPORT: copy report + mar89.rpt + apr89.rpt + may89.rpt You can also combine several files into one by using wildcards, as the following example shows: copy *.txt combin.doc This command combines all files in the current directory on the current drive that have the extension .TXT into one file named COMBIN.DOC, also in the current directory on the current drive. If you want to combine several binary files into one by using wildcards, include the /B switch, as the following example shows: copy /b *.exe combin.exe This prevents MS-DOS from treating CTRL+Z as an end-of-file character. CAUTION: If you combine binary files, the resulting file might not be usable due to internal formatting. In the following example, COPY combines each file that has a .TXT extension with its corresponding .REF file. The result is a file with the same filename but with a .DOC extension. Thus, COPY combines FILE1.TXT with FILE1.REF to form FILE1.DOC. Then COPY combines FILE2.TXT with FILE2.REF to form FILE2.DOC, and so on. copy *.txt + *.ref *.doc The following COPY command combines first all files with the .TXT extension, then all files with the .REF extension into one file named COMBIN.DOC: copy *.txt + *.ref combin.doc Copying information from the keyboard The following COPY command copies what you type at the keyboard to the OUTPUT.TXT file: copy con output.txt After you type this command and press ENTER, MS-DOS copies everything you type to the file OUTPUT.TXT. When you are finished typing, press CTRL+Z to indicate that you want to end the file. The CTRL+Z character will appear on the screen as "Z". You can also end a COPY CON command by pressing the F6 key. When you press F6, it generates the CTRL+Z character, which appears on the screen as Z. The following example copies information from the keyboard to the printer connected to LPT1: copy con lpt1

@rem: create test files:
@echo test 2j> c:\2j.txt
@echo test 4j> c:\4j.txt
@goto batch

   Christoph Basedau wrote:

   Is it a general feature/bug* that within for-loops the append-redirecting
   doesnt work:

SNIP...
for %%t in (1 2 3 4 5 6 7 8 9) do if exist %%tj.txt type %%tj.txt >>join.txt

Notes about your syntax:
Whatever the file exist or not, DOS always see the ASCII 62 characters ">>".

It is normal, that some special characters as redirection: "<", ">" and "|",
always is seen by DOS. The method to disable such a character is to use GOTO
commands or to prefix those with double colons "::". Even the REM command is
ignored. Thereby, "IF EXIST f DEL f", is sometimes substituted with "REM>f".

:BATCH
   @echo off  %[-- the "" below: 27 = ASCII(Escape) --]%
   choice /n/c:123'''''' 1. doesn't work, 2 or 3. works: 
   for %%v in (1 2 3 EOF: ) do if errorlevel %%v goto %%v

:1 doesn't work:
   if exist \join del \join
   for %%t in (1 2 3 4 5 6 7 8 9) do if exist \%%tj.txt type\%%tj.txt>>\join
   rem. works: for %%t in (1 2 3 4 5 6 7 8 9) do        type\%%tj.txt>>\join
   goto END

:2 same as above, except it works:
   rem >c:\join.txt
   for %%t in (1 2 3 4 5 6 7 8 9) do if exist c:\%%tj.txt copy c:\join.txt +
c:\%%tj.txt c:\join.txt
   goto END

:3 suppress output to the screen and do not add an EOF character:
                          ctty nul
   rem >c:\join.txt
   copy c:\join.txt+c:\?j.txt /B c:\join.txt

                          ctty con
:END
   if exist c:\?j.txt erase c:\?j.txt
   for %%v in (echo pause cls) do %%v.

:: The COPY command uses the default switch /A on any text files.
:: The /B switch avoiding an EOF character: ^Z, (ASCII value 26).
:: Note: "if errorlevel 10" is the same as: "if errorlevel EOF:".
::       This very special syntax, is something I discovered last
::       year. It's an errorlevel checking feature, I like to use
::       in my programs. This feature has from DOS 2.0 and above,
::       always been available.
::                         Benny Pedersen,
::                         PS.: If you want a briefer answer? Use
::                              @rem erase any content> \join.txt
::                              @copy \join.txt+\?j.txt \join.txt
::                              @if exist \?j.txt erase/p \?j.txt
:EOF



Copy *.HTM but not *.HTML A batch example for Win98: @% Hide *.HTML files: % attrib +h *.html @% Create a folder X: % xcopy nul X\>nul @% Copy *.HTM into X: % copy /-y *.htm X @% Unhide HTML files: % attrib -h *.html
Links: Copying a file from drive A to A.
-Top- | Syntax | Notes XCOPY | DISKCOPY