cheat sheet

copy

Copy one or more files to a new location in the Windows command prompt. Covers single-file copy, wildcard batches, file concatenation, and binary vs ASCII modes.

copy — Copy Files

What it is

copy is a built-in cmd.exe command that copies one or more files to a destination file or directory, and can also concatenate multiple source files into a single destination file. Present since MS-DOS 1.0, it handles simple flat-file copying well; for recursive tree copies, preserving attributes, or network paths, prefer xcopy or robocopy instead.

Availability

copy is built into cmd.exe on every Windows version. PowerShell's equivalent is Copy-Item (aliased cp, copy).

cmd
copy /?

Output:

css
Copies one or more files to another location.

COPY [/D] [/V] [/N] [/Y | /-Y] [/Z] [/L] [/A | /B ] source [/A | /B]
     [+ source [/A | /B] [+ ...]] [destination [/A | /B]]

Syntax

The source can be a filename or wildcard; the destination can be a file name, a directory (trailing \ or existing folder name), or omitted to copy into the current directory.

cmd
copy [switches] source [destination]
copy source1 + source2 [destination]

Output: (shows count of files copied, or error)

Essential options

SwitchMeaning
/YOverwrite existing destination without prompting
/-YPrompt before overwriting (default when interactive)
/VVerify that the copy is identical to the source
/ATreat file(s) as ASCII text (stop at Ctrl+Z / EOF marker)
/BTreat file(s) as binary (copy exact byte count — default for single files)
/DAllow the destination to be decrypted if source is EFS-encrypted
/ZCopy in restartable mode (safe for network interruptions)
/NUse short (8.3) filename if available

Copying a single file

The simplest form copies one file to another name or into a directory. If the destination is an existing directory, copy places the file inside it with the same name.

cmd
rem Copy to a new name in the same folder
copy report.docx report_backup.docx

Output:

scss
        1 file(s) copied.
cmd
rem Copy into a directory (keeps original name)
copy report.docx C:\Backups\

Output:

scss
        1 file(s) copied.
cmd
rem Copy to a different drive and folder
copy notes.txt D:\Archive\notes.txt

Output:

scss
        1 file(s) copied.

Wildcard copies

Wildcards * and ? let you copy groups of files matching a pattern. The destination must be a directory when copying multiple files; specifying a filename as destination with wildcards copies only the last matching file.

cmd
rem Copy all .txt files to an archive folder
copy *.txt C:\Archive\

Output:

scss
        4 file(s) copied.
cmd
rem Copy files matching a partial name
copy report*.docx D:\Reports\

Output:

scss
        3 file(s) copied.

Overwrite behaviour

By default in an interactive session copy prompts before overwriting an existing file. Use /Y to suppress the prompt in scripts, or /-Y to force a prompt even when the COPYCMD environment variable would suppress it.

cmd
rem Silent overwrite — essential in batch scripts
copy /Y source.txt destination.txt

Output:

scss
        1 file(s) copied.
cmd
rem Force prompt even if COPYCMD=/Y is set
copy /-Y source.txt destination.txt

Output:

css
Overwrite destination.txt? (Yes/No/All):

File concatenation

Placing + between multiple sources merges them into a single destination file. This is copy's unique feature absent in newer tools. ASCII mode (/A) stops reading each source at the first Ctrl+Z (ASCII 26) character; binary mode (/B) copies all bytes verbatim.

cmd
rem Concatenate three text files into one
copy header.txt + body.txt + footer.txt combined.txt

Output:

css
header.txt
body.txt
footer.txt
        1 file(s) copied.
cmd
rem Append one file to another (source and destination may overlap)
copy existing.txt + new_section.txt existing.txt

Output:

scss
        1 file(s) copied.
cmd
rem Binary concatenation (no Ctrl+Z stripping)
copy /B part1.bin + part2.bin combined.bin

Output:

scss
        1 file(s) copied.

Copying to CON, NUL, and other devices

cmd.exe devices (CON, NUL, PRN, COM1, …) can be used as source or destination. CON is the console; copying to NUL discards output (useful for silencing command output).

cmd
rem Write console input to a file (Ctrl+Z then Enter to stop)
copy CON myfile.txt

Output:

lua
(cursor waits for input; type lines, then Ctrl+Z + Enter)
        1 file(s) copied.
cmd
rem Send a file to the default printer
copy report.txt PRN

Output:

scss
        1 file(s) copied.

Restartable mode for network copies

/Z enables restartable mode: if the connection drops mid-copy the transfer can be resumed rather than starting over. This only applies when copying to or from network paths.

cmd
copy /Z \\myhost\share\bigfile.iso C:\Downloads\bigfile.iso

Output:

scss
        1 file(s) copied.

Common pitfalls

  1. Wildcards with a filename destination copy only the last match — if the destination is output.txt and source is *.txt, only the final matching file ends up there. Use a directory destination for multi-file copies.
  2. copy is not recursive — it will not descend into subdirectories; use xcopy /S or robocopy for trees.
  3. Concatenation in ASCII mode may truncate binary data/A is the default when combining files; always specify /B for binary concatenation.
  4. Overwrite prompt suppressed by COPYCMD=/Y — if this environment variable is set, copy never prompts. Specify /-Y explicitly to get the prompt back.
  5. Long paths (>260 chars) may failcopy does not support extended-length paths (\\?\). Use robocopy for paths over the 260-character limit.

Real-world recipes

Back up a config file before editing

cmd
copy /Y app.config app.config.bak
notepad app.config

Output:

scss
        1 file(s) copied.

Merge CSV exports into one file

cmd
copy /B jan.csv + feb.csv + mar.csv Q1.csv

Output:

scss
jan.csv
feb.csv
mar.csv
        1 file(s) copied.

Copy all files of a type to a backup folder silently in a scheduled task

cmd
copy /Y C:\Logs\*.log C:\Backups\Logs\

Output:

scss
        7 file(s) copied.

Create an empty file

cmd
copy NUL placeholder.txt

Output:

scss
        1 file(s) copied.

ASCII vs binary modes in depth

/A and /B switches sit before or after a source/destination filename and apply to the closest preceding argument. The defaults are subtle: a single-file copy defaults to binary; a concatenation (+) defaults to ASCII for sources and binary for destination. ASCII mode stops reading at the first Ctrl+Z (0x1A) byte and appends a Ctrl+Z marker on write — which silently corrupts binary files. Always specify /B explicitly when copying anything that is not plain text.

cmd
rem Binary copy (default for single file)
copy /B installer.exe C:\Tools\installer.exe

Output:

scss
        1 file(s) copied.
cmd
rem Demonstrating ASCII truncation — DON'T do this with binaries
copy /A binary.dat copy.dat
fc /B binary.dat copy.dat

Output:

scss
        1 file(s) copied.
Comparing files binary.dat and COPY.DAT
00001A05: 1A FF
cmd
rem Per-argument switches: source binary, destination ASCII (rare)
copy source.txt /B + extra.txt destination.txt /A

Output:

scss
source.txt
extra.txt
        1 file(s) copied.

Verification with /V

/V reads back every copied file from the destination and compares it byte-for-byte to the source. It catches transient write errors that the filesystem accepts but later fails to read correctly. The cost is roughly 2x the copy time. Modern SSDs and NTFS journaling make /V largely redundant on local drives, but it remains useful for flaky USB sticks, optical media, and unreliable network shares.

cmd
copy /V important.docx D:\Backup\important.docx

Output:

scss
        1 file(s) copied.
cmd
rem Verification failure surfaces as an error
copy /V flaky_file.bin E:\backup\flaky_file.bin

Output:

csharp
The data being verified does not match the file that was copied.

Restartable mode with /Z

/Z enables a restartable transfer protocol designed for unreliable network links. If a copy is interrupted (network drop, transient share unavailability) /Z resumes from the last successful byte rather than starting over. The trade-off is significant per-block overhead, so reserve /Z for slow or unstable links — on a fast LAN or local copy it is pure overhead.

cmd
copy /Z \\myhost\share\bigfile.iso C:\Downloads\bigfile.iso

Output:

scss
        1 file(s) copied.

Long path support and /B for size accuracy

copy historically caps at MAX_PATH (260 characters) and does not honour LongPathsEnabled reliably. For multi-gigabyte files crossing the size cap on cmd.exe, switch to xcopy, robocopy, or PowerShell's Copy-Item. The size in the 1 file(s) copied summary is accurate only when /B is used; ASCII mode reports the apparent text size.

File concatenation in depth

The + operator is copy's most distinctive feature. Multiple sources combine in order into a single destination. Sources may include wildcards: copy *.csv all.csv concatenates every matching file in the working directory (in directory-enumeration order, which is typically alphabetical on NTFS). Be aware: if the destination filename matches the wildcard, copy skips it to avoid infinite recursion — but only when it can detect the collision, which is not always.

cmd
rem Concatenate all CSVs in the folder
copy *.csv combined.csv

Output:

scss
january.csv
february.csv
march.csv
        1 file(s) copied.
cmd
rem Concatenate with explicit binary mode for safety
copy /B part1.zip + part2.zip combined.zip

Output:

python
part1.zip
part2.zip
        1 file(s) copied.
cmd
rem Append in place — source and destination overlap
copy /B existing.log + new_entries.log existing.log

Output:

scss
        1 file(s) copied.

COPYCMD environment variable

The COPYCMD environment variable provides a default flag for copy, move, and xcopy. Setting COPYCMD=/Y suppresses overwrite prompts globally — which is convenient for scripts but dangerous in interactive sessions where you may want the safety prompt. Use /-Y on any individual command to force the prompt back on.

cmd
set COPYCMD=/Y
copy source.txt destination.txt
rem No prompt even if destination exists

Output:

scss
        1 file(s) copied.
cmd
rem Force prompt despite COPYCMD=/Y
copy /-Y source.txt destination.txt

Output:

css
Overwrite destination.txt? (Yes/No/All):

PowerShell equivalents

Copy-Item (aliases cp, copy, cpi) is the PowerShell native. It handles single files, wildcards, recursive trees (-Recurse), filters (-Filter, -Include, -Exclude), and produces typed objects. Unlike copy, it always preserves binary content correctly and natively supports long paths. PowerShell's copy is not the cmd builtin — it is Copy-Item with PowerShell semantics, so /Y and /Z have no meaning there.

powershell
# Single file
Copy-Item report.docx report_backup.docx

Output: (none — silent success)

powershell
# Wildcard
Copy-Item *.txt C:\Archive\

Output: (none — silent success)

powershell
# Recursive directory copy with overwrite
Copy-Item C:\Projects\myapp C:\Backup\myapp -Recurse -Force

Output: (none — silent success)

powershell
# Copy with include/exclude filters
Copy-Item C:\Source\* C:\Dest\ -Recurse -Include '*.py','*.md' -Exclude '*.pyc'

Output: (none — silent success)

powershell
# Concatenate files (no native + operator; use Get-Content / Set-Content)
Get-Content header.txt, body.txt, footer.txt | Set-Content combined.txt

Output: (none — silent success)

powershell
# Binary concatenation
$bytes = foreach ($f in 'part1.bin','part2.bin') { [IO.File]::ReadAllBytes($f) }
[IO.File]::WriteAllBytes('combined.bin', ($bytes | ForEach-Object { $_ }))

Output: (none — silent success)

powershell
# Dry run with -WhatIf
Copy-Item C:\Source\* C:\Dest\ -Recurse -WhatIf

Output:

csharp
What if: Performing the operation "Copy File" on target "Item: C:\Source\a.txt Destination: C:\Dest\a.txt".
powershell
# Long path with literal path
Copy-Item -LiteralPath '\\?\C:\very\long\source\file.dat' -Destination 'D:\Backup\'

Output: (none — silent success)

CMD vs PowerShell vs bash comparison

GoalCMDPowerShellbash (Linux/macOS)
Single file copycopy a bCopy-Item a bcp a b
Silent overwritecopy /Y a bCopy-Item a b -Forcecp -f a b
Recursive directory(use xcopy/robocopy)Copy-Item a b -Recursecp -r a b
Preserve timestamps(not built in; robocopy /DCOPY:T /COPY:DAT)Copy-Item -Recurse (preserves)cp -p a b
Concatenate filescopy a + b + c outGet-Content a,b,c | Set-Content outcat a b c > out
Verify after copycopy /V a b(manual: compare hashes)cp -p a b; cmp a b
Restartable networkcopy /Z a b(use Start-BitsTransfer)(use rsync --partial)
Dry run / preview(none built in)Copy-Item -WhatIfcp -v or rsync --dry-run
Pattern with exclude(not built in; use xcopy /EXCLUDE)Copy-Item -Excludersync --exclude
Create empty filecopy NUL foo.txtNew-Item foo.txt or Set-Content foo.txt ''touch foo.txt
Pipe to printercopy a PRN(use Out-Printer)lpr a

Common pitfalls (continued)

  1. ASCII mode truncates at Ctrl+Zcopy file.exe other.exe defaults to binary for single-file copies, but copy a + b out defaults to ASCII for sources. Always pass /B for non-text.
  2. copy does not preserve timestamps when concatenating — the destination gets the current time. Use robocopy /COPY:DAT /DCOPY:T if timestamps matter.
  3. No recursive flagcopy cannot descend into subdirectories. Reach for xcopy /S /E or robocopy /E for trees.
  4. Wildcards with filename destinationcopy *.txt out.txt writes only the last matched file to out.txt if the destination is treated as a file. Always end the destination with \ to force directory interpretation.
  5. Sparse files and alternate data streams are not preservedcopy collapses NTFS sparse files into their physical representation and drops ADS. Use robocopy /COPY:DAT (or /B for backup mode) for those.
  6. Junctions and symlinkscopy follows them and copies the target file or directory. Use xcopy /B or robocopy /SL to copy the link itself.
  7. COPYCMD=/Y in user environment — a global COPYCMD=/Y removes the safety prompt everywhere. Audit set output if interactive copies silently overwrite.

Real-world recipes (continued)

Combine log shards in chronological order

cmd
rem Each shard sorts alphabetically by ISO date prefix
copy /B shard_2026*.log combined.log

Output:

c
shard_20260101.log
shard_20260102.log
...
        1 file(s) copied.

Daily snapshot of a config file with date stamp

cmd
set STAMP=%DATE:~-4,4%%DATE:~-10,2%%DATE:~-7,2%
copy /Y app.config "C:\Backup\app.config.%STAMP%"

Output:

scss
        1 file(s) copied.

Verify copy by hash comparison

cmd
copy /Y bigfile.iso D:\bigfile.iso
for /F %a in ('certutil -hashfile bigfile.iso SHA256 ^| findstr /V ":"') do set H1=%a
for /F %a in ('certutil -hashfile D:\bigfile.iso SHA256 ^| findstr /V ":"') do set H2=%a
if "%H1%"=="%H2%" (echo Match) else (echo MISMATCH)

Output:

scss
        1 file(s) copied.
Match

Bundle CSV exports into a single archive

cmd
copy /B exports\*.csv consolidated.csv
echo Combined %ERRORLEVEL%

Output:

scss
        7 file(s) copied.
Combined 0

PowerShell: copy preserving timestamps and ACLs

powershell
Copy-Item C:\Source\* C:\Dest\ -Recurse -Force
Get-ChildItem C:\Source -Recurse | ForEach-Object {
    $dest = $_.FullName -replace [regex]::Escape('C:\Source'), 'C:\Dest'
    if (Test-Path $dest) {
        Get-Acl $_.FullName | Set-Acl -Path $dest
        (Get-Item $dest).LastWriteTime = $_.LastWriteTime
    }
}

Output: (none — silent success)

Create a sparse placeholder of an expected size

cmd
copy NUL placeholder.dat
fsutil file setvaliddata placeholder.dat 1048576

Output:

csharp
        1 file(s) copied.
Valid data length is changed

Touch — bump a file's modified time without changing content

copy documents an idiomatic "touch" trick: concatenate the file with nothing (two trailing commas indicate the destination is intentionally omitted), and the file is rewritten with the current timestamp.

cmd
rem Update mtime on existing.log to now, leave content untouched
copy /B existing.log +,,

Output:

scss
existing.log
        1 file(s) copied.

This is the cmd equivalent of touch -m existing.log on Linux. PowerShell's version is (Get-Item existing.log).LastWriteTime = Get-Date.

Sources

Microsoft Learn — copy command — Authoritative reference for /d /v /n /y /-y /z /a /b, the + concatenation operator and ASCII/binary mode interplay, the COPYCMD environment variable, device-name destinations (PRN, COMn), and the +,, timestamp-touch idiom.

See also

  • xcopy — recursive directory copy with attribute filtering; the historical step up from copy.
  • robocopy — production-grade copy and sync with retry, mirror, and logging.
  • move — copy-then-delete in one step.
  • del — remove files; inverse of copy.
  • fc — verify two files differ byte-by-byte; useful after copy.
  • certutil -hashfile — verify content integrity post-copy.
  • PowerShell Copy-Item, Start-BitsTransfer — modern alternatives with progress and resume.
  • Linux cp, rsync — Unix equivalents.