How to check candidates for modification and deletion before copying with Robocopy

PowerShellカテゴリを表すパンダのイラスト PowerShell

About this article
This article was created using an automated generation workflow leveraging generative AI. The specifications for Robocopy listing, mirroring, and exit codes were verified on Microsoft Learn. The following file layouts and results are practice examples that you can verify on your own PC.

Verification Status: 📘 Official Information Verified, Windows Device Not Tested

Supported Environments, Capabilities, and Limitations (Verified on December 2, 2026)

ItemConditions for using this procedure
TargetUsing the standard Windows robocopy.exe from PowerShell on Windows 11 or Windows 10. Windows Server is also listed in Microsoft's command documentation.
Permissions and LicenseUsers who can create practice files in their own temporary folder. No additional paid services are used. Access permissions for the destination share must be verified separately.
Target DataTwo newly created local folders and dummy text files. Actual data from shared folders will not be used.
What can be doneYou can identify items that exist only in the source or only in the destination in a list, and determine deletion candidates during mirroring.
Scope of VerificationThe list reflects the state at the time of inspection. Subsequent differences, write permissions during actual transfer, and recoverability require separate verification. Japanese log notation has not been tested on an actual device.

When migrating shared folders, is the only concern whether the new monthly report will be copied? You might also worry about the handover notes already placed by the recipient side. If you migrate using mirroring, those notes will become deletion candidates because they do not exist in the source. In Robocopy, /L Adding this lists the targets based on the selected copy conditions. You can check which files exist exclusively on one side or the other without performing any copy, delete, or timestamp modification operations.Microsoft Learn Robocopy Options Reference

Place one marker each in the source and the destination.

Open PowerShell and execute the following code block together. Because it creates practice folders with different names each time, files from the previous run will not mix into the current results. The only items created by this preparation are the samples inside the displayed temporary folder. Perform the subsequent operations in the same PowerShell window.

$lab = Join-Path $env:TEMP ("robocopy-lab-" + [guid]::NewGuid().ToString("N"))
$src = Join-Path $lab "source"
$dst = Join-Path $lab "destination"
New-Item -ItemType Directory -Path $lab | Out-Null
New-Item -ItemType Directory -Path $src, $dst | Out-Null
Set-Content -LiteralPath (Join-Path $src "monthly.txt") -Value "new report"
Set-Content -LiteralPath (Join-Path $dst "handover.txt") -Value "keep this note"
$lab

Let us predict the outcome first. The new monthly report monthly.txt exists only in the original source. The handover memo handover.txt exists only in the destination destination. If you choose to "sync the destination to match the source," the monthly report is a candidate for addition, and the memo is a candidate for deletion. Even if there is only a single differing filename, the next action you should take is reversed depending on which side it resides on.

Reviewing the list of candidates in the event of a mirror operation

Execute the following line as is. For this practice, we will append /L to all subsequent Robocopy commands as well.

robocopy.exe $src $dst /L /MIR /X /FP /R:0 /W:0
$LASTEXITCODE

The first argument is the source, and the second is the destination. Verify the direction of source and destination at the beginning of the output, and then look for the individual filenames./FP outputs the full path, and /X also reports extra files in the destination./R:0 specifies that no retries should be performed, and /W:0 sets the wait time between retries to 0 seconds. These two settings prevent the test from dragging on.

Search nameCurrent layout/MIR Interpreting candidates based on this setup
monthly.txtSource onlyCandidates to be newly placed in the destination. Even if their names appear in the output, they are not pre-created during the list display stage.
handover.txtDestination onlyCandidates to be deleted if aligned with the source. This judgment applies equally even to useful notes created by someone at the migration destination.

Log headers may vary depending on the display language. Do not decide on deletion based solely on the count or terms like "extra"; read the name and storage location together as a pair. By running the following two lines immediately after the output, you can verify what happened to your files.

Test-Path -LiteralPath (Join-Path $dst "monthly.txt")
Test-Path -LiteralPath (Join-Path $dst "handover.txt")

The expected values for this example from the top are False、True. The monthly report has not arrived yet, and the handover notes remain in place. The presence or absence of the actual files is what is used for this verification. This is not simply re-reading the same number of candidates output by Robocopy.

If the policy is to keep files that exist only in the source

Using the same two folders, change one condition. This time, replace /MIR with /E.

robocopy.exe $src $dst /L /E /X /FP /R:0 /W:0

/E traverses including empty subfolders./MIR additionally implies removing items that exist only in the source. It is not unnatural for handover.txt to be visible in both lists./X is specified to display extra files. In the second condition, there is no specification to delete notes simply because they exist only in the source. If you hear on-site that there are "12 extra files," verify which conditions were actually used to capture the list before looking at the count.

Include files with the same name in the verification targets

Using only monthly reports and handover notes makes it easy to overlook existing files that will be overwritten, even though the difference between new files and deletion candidates is clear. Next, create files with the same name in both locations, change only the source content and update timestamp, and then regenerate the file list. Do not write to any directory other than the practice folder.

$srcFile = Join-Path $src "shared.txt"
$dstFile = Join-Path $dst "shared.txt"
Set-Content -LiteralPath $srcFile -Value "new version with extra text"
Set-Content -LiteralPath $dstFile -Value "old version"
(Get-Item -LiteralPath $srcFile).LastWriteTime = (Get-Date)
(Get-Item -LiteralPath $dstFile).LastWriteTime = (Get-Date).AddDays(-1)
robocopy.exe $src $dst /L /MIR /X /FP /R:0 /W:0
Get-Content -LiteralPath $dstFile

Under this condition, it can be anticipated that the identically named shared.txt will also become an update candidate. Even if it appears in the list, the expected value of the final Get-Content remains the old version. Confirm the actual log classification names and exit codes by testing them on Windows. If it is necessary to strictly verify whether the file contents are truly identical, perform a separate hash comparison. The Robocopy listing is based on general file selection criteria and is not a hash inspection that guarantees an exact content match.

For example, in a department's monthly backup destination, if reports with the same name exist in both the source and destination, attention must be paid to overwrite candidates even if the deletion candidate count is zero. Check whether the content added by the recipient of the handover remains, and decide whether it should be replaced with the original report or whether both should be kept and compared. The mere fact that a file's update timestamp is newer does not determine which one is the official business record.

When directions are reversed, no candidates are found, or the exit code is non-zero

  • The monthly report appears as a deletion candidate.Check the path at the beginning to ensure the source and destination were not passed in reverse. Even if the file names are the same, reversing the orientation reverses the plan.

  • Neither marker appears. $lab, and locate the two files in File Explorer. If a different window was opened or if preparation was redone, you might be looking at a different practice folder. If access denied or path errors occur, you cannot conclude "zero differences" from an empty list.

  • The exit code was non-zero.Robocopy also returns non-zero values when differences exist. Microsoft defines 8 or higher as a value indicating at least one failure. Even for values below 8, a human must decide whether notes residing only in the destination can be deleted. When reading codes, check $LASTEXITCODE immediately after Robocopy.Microsoft Learn Exit Code Table

What to determine before execution even if a list is generated

/LWhat you obtain with is a candidate list under specified conditions, not approval to execute it. Particularly in plans using /MIR, verify with the responsible person whether files residing only in the destination can be deleted. If target folder specifications are reversed or if files are added or removed after the list is retrieved, the preliminary check and the actual targets will no longer match. In actual operations, save the list to verify differences, prepare backup and recovery methods, and follow separately approved procedures. All commands in this article are exclusively for verification with /L, and actual mirror execution is not covered.

If bringing materials to a migration meeting, supplementing the candidate list with the "source and destination orientation," "time of inspection," and "whom to consult regarding files existing only in the destination" will facilitate decision-making. In shared documentation, mask full paths containing user names or customer names. If either folder is updated after verification, regenerate the list under the same conditions. The moment it is decided to keep notes located exclusively in the migration destination, you obtain one more piece of material to review the copy plan itself.

Document information

Article title
How to check candidates for modification and deletion before copying with Robocopy
Published
Updated
Source
https://papanda925.com/?p=19168&lang=en

License: Text and original figures for which this site holds the relevant rights are available under CC BY 4.0 , unless otherwise noted. This article may include content created or edited with generative AI. If code has a separate license notice or a linked GitHub repository license, that license takes precedence for the code. Quotations, third-party materials, images, and trademarks are excluded from this license. Usage policy

Copied title and URL