Programming Field

Replace - DOS/Command Prompt Reference

Replaces all files in the target directory with the specified file in bulk. Alternatively, it adds the specified file to the target directory.

Syntax

replace[.exe] <source-file> <target-path> /A [/P] [/R] [/W]
replace[.exe] <source-file> <target-path> [/S] [/U] [/P] [/R] [/W]

Options

<source-file> Specifies the source file for replacement. Wildcards can also be used for specification.
<target-path> Specifies the destination directory for replacement.
/A

If the specified file (<source-file>) does not exist in the target directory (<target-path>), it will be added. Subdirectories will not be affected.

If the /A option is not specified, it will replace files in the target directory (<target-path>) that match the specified file (<source-file>).

/S Files in subdirectories of the target directory (<target-path>) will also be considered for replacement. This cannot be specified simultaneously with /A.
/U Replaces the file only if the replacement target file is older. /S and /U can be specified simultaneously.
/P Displays a confirmation prompt when performing file replacement or addition.
/R Includes read-only (write-protected) files as targets for replacement/addition.
/W Waits for the disk to be inserted before starting the search for <source-file>. When in this state, pressing Enter initiates the search.

Details

This command cannot update files with hidden or system file attributes. If necessary, use the Attrib command to remove the attributes before proceeding.

‘Replace’ returns specific exit codes, and their meanings are as follows:

CodeMeaning
0The command has successfully completed.
1This command is not compatible with the operating system.
2The source file (<source-file>) was not found.
3The source path (the directory if <source-file> contains a directory name) or the destination path for replacement was not found.
5There is no write permission for the file targeted for replacement. ([Windows NT] This error code is not returned in the case of insufficient write permissions due to file security settings.)
8Insufficient memory.
11Incorrect command syntax.

Samples

Sample

replace D:\*.log E:\logs /U

The files with the extension ‘log’ located in ‘D:\’ will be copied (replaced) to the ‘E:\logs’ directory if files with the same names exist there. In this case, if a file with the same name already exists in ‘E:\logs’, it will only be replaced if the file's date (modification timestamp) is older than the file in that directory.