replaceREPLACE Command Guide - Update Files in Windows CMD
Learn how to use the REPLACE command to update files in a directory with newer versions from another location. Includes syntax, /A add mode, /S recursive, and deployment examples.
The replace command is a Windows Command Prompt utility that replaces files in a destination directory with identically named files from a source directory. Use REPLACE source destination /S for recursive replacement, REPLACE source destination /A to add new files only, or REPLACE source destination /U to update only older files—essential for deployment scripts, configuration updates, and file synchronization without full directory copies.
Whether you're deploying updated DLLs across a network share, synchronizing configuration files across multiple workstations, updating scripts in a shared folder, or refreshing template files from a master location, mastering replace provides targeted file updates that preserve directory structure and selectively overwrite only matching files. IT professionals and system administrators rely on this command for software rollouts, patch deployment, and maintenance scripts.
This comprehensive guide covers replace syntax, all parameters including /A, /R, /S, and /U, practical examples for deployment and sync scenarios, troubleshooting tips, related commands, and frequently asked questions. By the end, you'll confidently use replace for file update operations from the command line.
What Is the Replace Command?
The replace command is a built-in Windows utility for replacing or adding files between directories. Unlike copy or xcopy, replace focuses specifically on updating existing files or selectively adding new files that don't exist in the destination. Key behaviors:
- Replace mode (default) – Overwrites destination files with matching names from source
- Add mode (/A) – Adds only files that don't exist in destination; never overwrites
- Update mode (/U) – Replaces only when source file is newer than destination
- Read-only override (/R) – Replaces read-only files (normally skipped)
Replace works in Command Prompt (CMD) and batch scripts. It is available in all Windows versions from Windows NT through Windows 11 and Windows Server. The command is commonly used in login scripts, deployment automation, and maintenance batch files.
Replace Command Syntax
REPLACE [drive1:][path1]filename [drive2:][path2] [/A] [/P] [/R] [/W] [/S] [/U]
Parameters and Switches
| Parameter | Description |
|---|---|
[drive1:][path1]filename | Source file(s). Wildcards (* ?) supported. |
[drive2:][path2] | Destination directory. Required for replace operations. |
/A | Add new files to destination only. Never overwrites. Cannot use with /S or /U. |
/P | Prompts for confirmation before replacing each file. |
/R | Replaces read-only files (normally skipped). |
/W | Waits for you to insert a disk before beginning (legacy floppy support). |
/S | Replaces files in all subdirectories. Cannot use with /A. |
/U | Replaces only files that are older than source (update mode). |
Important Rules
- Source filename is required; destination path is required (except in add mode with current directory).
/Aand/Scannot be used together./Aand/Ucannot be used together.- Wildcards in source:
*matches any characters,?matches single character.
Parameters in Detail
Add Mode (/A)
Add mode copies only files that do not exist in the destination. Never overwrites. Use when deploying new files to a directory without touching existing files. Essential for initial population of shared folders.
Replace Mode (Default)
Default behavior overwrites destination files with matching names from source. Files in destination that don't exist in source are left unchanged. Use for updating existing files with newer versions.
Update Mode (/U)
Replaces only when the source file has a newer date/time than the destination. Skips files where destination is same or newer. Use for incremental updates—only outdated files get replaced.
Subdirectory Mode (/S)
Processes source directory and all subdirectories. Searches destination for matching path structure. Use for recursive updates across entire directory trees.
Examples
Basic File Replacement
Replace a single file in destination with the version from source:
replace C:\New\config.ini C:\App\Config\
Output: Replacing C:\App\Config\config.ini... 1 file(s) replaced.
Use when you have an updated config.ini and want to push it to the application directory.
Replace All Matching Files
Replace all .dll files from source to destination:
replace C:\Updates\*.dll C:\Program\MyApp\ /R
/R allows replacing read-only DLLs. Use when deploying application updates.
Recursive Replacement with /S
Replace files in destination and all subdirectories:
replace C:\Master\*.* D:\Shared\Templates\ /S
Updates all files in D:\Shared\Templates and its subdirectories with versions from C:\Master, preserving directory structure.
Update Only Older Files
Replace only when source is newer than destination:
replace C:\Patches\*.exe D:\Deploy\ /U /R
Skips files where destination is already up to date. Ideal for patch deployment without unnecessary overwrites.
Add New Files Only (No Overwrite)
Add files to destination without overwriting existing:
replace C:\NewFiles\*.* D:\Target\ /A
Only copies files that don't exist in D:\Target. Preserves existing files. Use for initial sync or adding new resources.
Prompt Before Each Replacement
Confirm each file replacement interactively:
replace C:\Updates\*.txt C:\Data\ /P
Use when you want to review each file before overwriting. Helps prevent accidental mass replacement.
Replace from Network Share
Works with UNC paths:
replace \\Server\Updates\*.* C:\Local\App\ /S /U
Deploy updates from a central network location to local application directory.
Common Use Cases
-
Deploy application updates – Push updated DLLs, EXEs, or config files from a build or patch folder to installation directories across workstations using replace in a batch script or Group Policy startup script.
-
Synchronize configuration files – Update INI, XML, or JSON config files in multiple application directories from a master template location. Use /U to update only outdated configs.
-
Script deployment – Distribute updated batch files, PowerShell scripts, or automation scripts to shared network locations. Use replace in a scheduled task or login script.
-
Template refresh – Update document templates (Word, Excel) in a shared Templates folder from a master source. Replace ensures all users get the latest template versions.
-
Driver updates – Replace outdated driver files in a driver repository with newer versions from vendor packages. Use wildcards for .sys, .inf files.
-
Web asset deployment – Update static assets (images, CSS, JS) on a web server from a build output directory. Replace only changed files with /U.
-
Patch distribution – Deploy security patches or hotfixes to multiple directories. Combine with robocopy or xcopy for initial copy, replace for targeted updates.
-
Shared folder maintenance – Keep a shared network folder in sync with a master copy. Run replace periodically via scheduled task.
-
Development to staging sync – Push updated files from development build to staging environment. Use /U to avoid overwriting environment-specific configs that may be newer.
-
Recovery file restoration – Restore specific files from backup to original locations using replace when you need to overwrite corrupted files.
-
Read-only file updates – Use /R when replacing protected or read-only files (e.g., system configs, locked DLLs) that normal copy would skip.
-
Selective add to destination – Use /A when populating a new deployment directory with files from source without overwriting any existing (e.g., partial restore).
Tips and Best Practices
-
Test with a small set first – Run replace on a test directory with a few files before deploying to production. Verify source and destination paths.
-
Use /U for safe updates – When unsure, use /U to replace only older files. Prevents accidentally overwriting newer local modifications.
-
Backup before bulk replace – Before running replace /S on critical directories, backup the destination. Accidental wrong paths can cause widespread overwrites.
-
Verify paths with DIR – Use
dir source_pathanddir destination_pathto confirm paths exist and contain expected files before replace. -
Use /P for cautious updates – When replacing critical files, use /P to confirm each replacement. Helps catch incorrect source or destination.
-
Quote paths with spaces – Enclose paths containing spaces:
replace "C:\My Files\*.*" "D:\Target Folder". -
Combine with xcopy for full sync – For initial copy use xcopy; for ongoing updates use replace. They complement each other in deployment workflows.
-
Document in scripts – Add comments in batch files explaining source, destination, and purpose of replace commands for future maintenance.
-
Check exit codes – Replace sets exit code: 0=success, 1=bad parameters, 2=no files found, 3=path not found, 5=access denied. Use
if errorlevelin scripts. -
Use full paths – Specify complete paths for source and destination to avoid ambiguity with current directory.
-
Read-only files need /R – If replace skips files, they may be read-only. Add /R to force replacement (requires appropriate permissions).
-
Wildcard caution – Test wildcard patterns with DIR first.
*.*matches all files;*.txtmatches only text files.
Troubleshooting Common Issues
"Access Denied" When Replacing Files
Problem: Replace fails with access denied on some files.
Cause: Files are read-only, in use by another process, or you lack write permissions.
Solution: Use /R to replace read-only files. Close applications using the files. Run Command Prompt as Administrator. For network paths, verify write permissions on the share.
Prevention: Check file attributes with attrib before replace. Use /R when deploying to directories with read-only files.
No Files Replaced (0 file(s) replaced)
Problem: Replace completes but reports 0 files replaced.
Cause: Source and destination have same files (no newer source with /U), destination path wrong, or filenames don't match.
Solution: Verify source path has files: dir source_path. Verify destination path: dir destination_path. Without /U, replace overwrites same-date files; with /U, only replaces if source is newer. Check path spelling and trailing backslashes.
Prevention: Run dir on both paths before replace. Use /P to see which files would be replaced.
"File Not Found" or Invalid Path
Problem: Replace reports path not found or invalid.
Cause: Typo in path, path doesn't exist, or UNC path unreachable.
Solution: Use cd to navigate and dir to verify paths. For UNC paths, ensure network share is accessible. Check for extra spaces or missing backslashes.
Prevention: Use Tab completion for path entry. Test network paths with dir \\server\share first.
/A and /S Used Together Error
Problem: Replace displays error about conflicting parameters.
Cause: /A (add mode) cannot be combined with /S (subdirectories) or /U (update).
Solution: Choose one mode. Use /A for add-only, or /S for recursive replace, or /U for update-only. Never combine /A with /S or /U.
Prevention: Review replace syntax before running. Add and replace modes are mutually exclusive.
Replaced Wrong Files
Problem: Accidentally overwrote files you didn't intend to replace.
Cause: Incorrect source or destination path, or overly broad wildcard.
Solution: Restore from backup if available. Use /P in future to confirm each file. Test with a copy of the directory first.
Prevention: Always backup critical destinations before bulk replace. Use specific wildcards (e.g., *.dll not *.*) when possible.
Replace Hangs or Is Very Slow
Problem: Replace takes excessively long or appears to hang.
Cause: Large directory trees with /S, network latency on UNC paths, or disk I/O bottlenecks.
Solution: Run replace on smaller batches. For network paths, ensure stable connection. Use /U to limit files processed. Consider robocopy for large-scale operations.
Prevention: For large deployments, use robocopy which is optimized for bulk operations. Use replace for smaller, targeted updates.
Related Commands
xcopy – Copy with Recursive and Attributes
xcopy copies files and directories with recursive, attribute, and date filtering. Use xcopy for initial full copies or when you need to create destination structure. Use replace when you only want to update existing files in place.
When to use replace: Updating existing files, add-only deployment, update-only (newer) replacement.
When to use xcopy: Full directory copy, creating new destination structure, copying with specific attributes.
copy – Simple File Copy
copy copies one or more files. Simpler than replace but doesn't support add-only or update-only modes. Use copy for one-off file copies. Use replace for batch updates with conditional logic.
When to use replace: Batch file updates, add new files only, update only older files.
When to use copy: Single file copy, appending files, simple copy operations.
robocopy – Advanced Sync and Mirror
robocopy (Robust File Copy) offers mirroring, multi-threaded copy, retry logic, and extensive options. Use robocopy for large-scale synchronization, mirror operations, or when replace lacks the features you need.
When to use replace: Simple update scenarios, scripts where replace is sufficient, legacy batch compatibility.
When to use robocopy: Large directories, network resilience, mirror sync, complex filtering.
attrib – File Attributes
attrib manages file attributes (read-only, hidden, archive, system). Use attrib to remove read-only before replace if you prefer not to use /R, or to set attributes after replace.
When to use: Clear read-only with attrib -r before replace, or use replace /R to override read-only directly.
Frequently Asked Questions
What does the replace command do?
The replace command replaces files in a destination directory with files of the same name from a source directory. It can also add new files only (/A), replace only older files (/U), or work recursively (/S) through subdirectories.
How do I replace only newer files?
Use the /U switch: replace source destination /U. Replace will only overwrite destination files when the source file has a newer date/time. Files where destination is same or newer are skipped.
Can I add files without overwriting existing ones?
Yes. Use /A: replace source destination /A. This adds only files that don't exist in the destination. No existing files are overwritten.
What is the difference between replace and copy?
Replace focuses on updating files: it can add-only, update-only (newer), and work recursively with specific behaviors. Copy simply copies files. Replace is designed for deployment and sync scenarios; copy is for basic file duplication.
How do I replace read-only files?
Use the /R switch: replace source destination /R. Without /R, replace skips read-only files. With /R, read-only files are replaced (requires write permissions).
Can replace work with network paths?
Yes. Use UNC paths: replace \\Server\Share\*.dll C:\Local\App\. Ensure you have read access to source and write access to destination.
What exit codes does replace return?
Replace returns: 0 = success, 1 = bad parameters, 2 = no files found, 3 = path not found, 5 = access denied. Use if errorlevel 1 in batch scripts to check for failures.
Why does replace say 0 file(s) replaced?
Common reasons: (1) With /U, all destination files are newer or same. (2) Source path is wrong or empty. (3) Filenames don't match. (4) Destination path is incorrect. Verify both paths and file dates.
Can I use replace in a batch script?
Yes. Replace is commonly used in batch files for deployment, login scripts, and maintenance. Check exit codes with if errorlevel and use %% for batch variables in paths.
Is replace available in PowerShell?
Replace is an external CMD command. In PowerShell, call it as replace.exe or use cmd /c replace .... For PowerShell-native alternatives, use Copy-Item with -Force or Robocopy.
Quick Reference Card
| Command | Purpose | Example |
|---|---|---|
replace source dest | Replace matching files | replace C:\New\*.txt C:\Data\ |
replace source dest /A | Add new files only | replace C:\New\*.* D:\Target\ /A |
replace source dest /U | Update older files only | replace C:\Patches\*.* D:\App\ /U |
replace source dest /S | Recursive subdirectories | replace C:\Master\*.* D:\Shared\ /S |
replace source dest /R | Replace read-only files | replace C:\DLLs\*.dll C:\App\ /R |
replace source dest /P | Prompt before each | replace C:\Updates\*.* C:\Data\ /P |
replace source dest /U /R | Update older, allow read-only | replace C:\Hotfix\*.* D:\Deploy\ /U /R |
Summary
The replace command is a targeted file update utility for Windows. Use replace for deployment scripts, configuration sync, and selective file updates. Key switches: /A for add-only (no overwrite), /U for update-only (newer files), /S for recursive subdirectories, /R for read-only override. Combine with xcopy for initial copies and replace for ongoing updates. Always verify paths before bulk operations and use /P when replacing critical files. Master replace for efficient file deployment and maintenance from the command line.
Practice the replace command in our Windows Command Simulator. Browse the Commands Reference for all Windows CMD utilities. Explore xcopy for full directory copies and robocopy for advanced synchronization.