[FAQ CENTER トップ]
[Powershell FAQ トップへ]



【Powershell】robocopy 実行結果を lastexitcode で判定する



更新日:2022/03/13


robocopy コマンドはOSの copy コマンドとは異なり様々なオプションがありとても便利です。

copy は途中で失敗した場合に残りコピー処理を継続せずに終了してしまう場合があります。またcopyコマンドは同一のファイルがあっても上書きするか無視するかのみしか選択できないため、大量のコピーには向いていません。
一方でrobocopy はフォルダのミラー機能があるなどとても便利です。
企業の運用で使用するなど活用することができます。できればバッチではなくPowershellとして使用すると柔軟なのでさらによいでしょう。

このページでは robocopy コマンド成功、失敗を判定する $lastexitcode 変数に関して説明します。

robocopy コマンドの戻り値で気を付けて欲しい点は次の通りです。


成功でも 0 以外の値が戻ることがある。一般的にコマンドは 0 が成功でそれ以外は失敗だが、robocopy はコマンドが成功しても例えば3が戻ることがある。



エラーコード(lastexitcode) は次の値の AND となります。

The return code from Robocopy is a bitmap, defined as follows:

Hex Decimal Meaning if set

0×00 0 No errors occurred, and no copying was done.
The source and destination directory trees are completely synchronized.

0×01 1 One or more files were copied successfully (that is, new files have arrived).

0×02 2 Some Extra files or directories were detected. No files were copied
Examine the output log for details.

0×04 4 Some Mismatched files or directories were detected.
Examine the output log. Housekeeping might be required.

0×08 8 Some files or directories could not be copied
(copy errors occurred and the retry limit was exceeded).
Check these errors further.

0×10 16 Serious error. Robocopy did not copy any files.
Either a usage error or an error due to insufficient access privileges
on the source or destination directories.
These can be combined, giving a few extra exit codes:

0×03 3 (2+1) Some files were copied. Additional files were present. No failure was encountered.

0×05 5 (4+1) Some files were copied. Some files were mismatched. No failure was encountered.

0×06 6 (4+2) Additional files and mismatched files exist. No files were copied and no failures were encountered.
This means that the files already exist in the destination directory

0×07 7 (4+1+2) Files were copied, a file mismatch was present, and additional files were present.



(1)変化がない場合
もしrobocopyで変化がない場合は 0 が戻されます。"No errors occurred, and no copying was done." ということです。


(2)ファイルを1個以上コピーした場合
もしrobocopy で1個ファイルがコピーされた場合は 1 が戻されます。"One or more files were copied successfully"ということです。

(3)宛先ディレクトリがないない場合
宛先ディレクトリがない場合は 16 が戻されます。"Serious error. Robocopy did not copy any files"ということです。深刻なエラーということです。



その点に注意してください。サンプルプログラムを紹介していきます。

サンプルコード集

以下は Windows 10 環境におけるサンプルコードと実行結果です。


次の例ではrobocopy コマンドを実行し、戻り値である $lastexitcode をチェックしています。
8を超える場合はエラーとして扱っています。


サンプルコード:


robocopy c:\temp\1 c:\temp\2
IF ($lastexitcode -ge 8)
{
write-host "robocopy でエラーが発生しました"
}

write-host "retcode:" $lastexitcode


実行結果:


-------------------------------------------------------------------------------
ROBOCOPY :: Windows の堅牢性の高いファイル コピー
-------------------------------------------------------------------------------

開始: 2021年6月4日 13:55:15
コピー元 : c:\temp\1\
コピー先 : c:\temp\2\

ファイル: *.*

オプション: *.* /DCOPY:DA /COPY:DAT /R:1000000 /W:30

------------------------------------------------------------------------------

1 c:\temp\1\

------------------------------------------------------------------------------

合計 コピー済み スキップ 不一致 失敗 Extras
ディレクトリ: 1 0 1 0 0 0
ファイル: 1 0 1 0 0 0
バイト: 0 0 0 0 0 0
時刻: 0:00:00 0:00:00 0:00:00 0:00:00
終了: 2021年6月4日 13:55:15

retcode: 0







次の例では存在しないフォルダ(x:\temp\2)を指定しています。
$lastexitcode で16以上が戻ってきているため、$lastexitcode -ge 8でエラーチェックされエラー処理が行われています。


サンプルコード:


robocopy c:\temp\1 x:\temp\2
IF ($lastexitcode -ge 8)
{
write-host "robocopy でエラーが発生しました"
}

write-host "retcode:" $lastexitcode


実行結果:


-------------------------------------------------------------------------------
ROBOCOPY :: Windows の堅牢性の高いファイル コピー
-------------------------------------------------------------------------------

開始: 2021年6月4日 13:58:00
2021/06/04 13:58:00 エラー 3 (0x00000003) コピー先のファイル システムの種類を取得しています x:\temp\2\
指定されたパスが見つかりません。

コピー元 : c:\temp\1\
コピー先 = x:\temp\2\

ファイル: *.*

オプション: *.* /DCOPY:DA /COPY:DAT /R:1000000 /W:30

------------------------------------------------------------------------------

2021/06/04 13:58:00 エラー 3 (0x00000003) コピー先ディレクトリを作成しています x:\temp\2\
指定されたパスが見つかりません。
robocopy でエラーが発生しました
retcode: 16









[Powershell FAQ トップへ]
[FAQ CENTER トップ]


本サイト内掲載されている情報は、著作権法により保護されています。いかなる場合でも権利者の許可なくコピー、配布することはできません。 このページはリンクフリーです。(このページへの直接リンクも可能です。) Copyright(c) tooljp.com 2007-2018