xcopy - 高度なファイルコピー
にメンテナンス済み
xcopy コマンドとは
xcopyコマンドは、ファイルやディレクトリをコピーするための高度なコマンドです。基本的なcopyコマンドと比較して、以下のような特徴があります:
- サブディレクトリを含むコピー: ディレクトリ構造全体を再帰的にコピー可能
- 豊富なオプション: 日付やアーカイブ属性によるフィルタリング、エラー処理など
- バックアップ機能: 変更されたファイルのみをコピーする増分バックアップに対応
- ネットワーク対応: ネットワーク越しのコピーで中断からの再開が可能
この記事では、xcopyコマンドの基本的な使い方から、実践的なオプションの組み合わせまで分かり易く説明しています。
xcopy コマンドの基本
xcopyコマンドの基本的な構文は以下の通りです。
xcopy [コピー元] [コピー先] [オプション]
基本的なファイルコピー
最もシンプルな使い方は、単一のファイルをコピーする方法です。
- □ ×
コマンド プロンプト
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2025 Ribbit App Development All rights reserved.
C:\users\user>xcopy document.txt C:\Backup\
このコマンドを実行すると、document.txtがC:\Backup\ディレクトリにコピーされます。
- □ ×
コマンド プロンプト
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2025 Ribbit App Development All rights reserved.
C:\users\user>
xcopy document.txt C:\Backup\ document.txt
1 個のファイルをコピーしました
C:\users\user>
ディレクトリ全体のコピー
/sオプションを使用すると、サブディレクトリを含むディレクトリ全体をコピーできます。
- □ ×
コマンド プロンプト
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2025 Ribbit App Development All rights reserved.
C:\users\user>xcopy MyFolder C:\Backup\MyFolder\ /s
このコマンドを実行すると、MyFolderとその中の全てのサブディレクトリ(空でないもの)がコピーされます。
- □ ×
コマンド プロンプト
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2025 Ribbit App Development All rights reserved.
C:\users\user>
xcopy MyFolder C:\Backup\MyFolder\ /s MyFolder\file1.txt
MyFolder\file2.txt
MyFolder\SubFolder\file3.txt
3 個のファイルをコピーしました
C:\users\user>
基本的なcopyコマンドはサブディレクトリを扱えませんが、xcopyは/sオプションを使用することで、ディレクトリ構造全体をコピーできます。詳しくは、
をご覧ください。
主要なオプション
/s - サブディレクトリをコピー
空でないサブディレクトリを含めてコピーします。
- □ ×
コマンド プロンプト
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2025 Ribbit App Development All rights reserved.
C:\users\user>xcopy Source Destination /s
/e - 空のサブディレクトリもコピー
/sオプションと組み合わせて使用し、空のサブディレクトリもコピーします。
- □ ×
コマンド プロンプト
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2025 Ribbit App Development All rights reserved.
C:\users\user>xcopy Source Destination /s /e
/s /eオプションを組み合わせることで、空のディレクトリを含む完全なディレクトリ構造をコピーできます。バックアップやミラーリングに便利です。
/i - コピー先をディレクトリとして扱う
コピー先が存在しない場合、自動的にディレクトリとして作成します。
- □ ×
コマンド プロンプト
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2025 Ribbit App Development All rights reserved.
C:\users\user>xcopy Source NewDestination /s /i
このオプションを使用すると、「ファイル名ですか、ディレクトリ名ですか?」という確認メッセージが表示されなくなります。
- □ ×
コマンド プロンプト
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2025 Ribbit App Development All rights reserved.
C:\users\user>
xcopy Source NewDestination /s /i Source\file1.txt
Source\file2.txt
2 個のファイルをコピーしました
C:\users\user>
/y - 上書き確認を省略
既存のファイルを自動的に上書きします(確認メッセージを表示しない)。
- □ ×
コマンド プロンプト
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2025 Ribbit App Development All rights reserved.
C:\users\user>xcopy Source Destination /y
/yオプションを使用すると、既存のファイルが確認なしで上書きされます。重要なファイルを誤って上書きしないよう注意してください。
/d - 日付による増分コピー
コピー元のファイルがコピー先のファイルより新しい場合のみコピーします。
- □ ×
コマンド プロンプト
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2025 Ribbit App Development All rights reserved.
C:\users\user>xcopy Source Destination /d /s
指定した日付以降に変更されたファイルのみをコピーすることもできます。
- □ ×
コマンド プロンプト
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2025 Ribbit App Development All rights reserved.
C:\users\user>xcopy Source Destination /d:01-15-2025 /s
/u - 既存ファイルのみ更新
コピー先に既に存在するファイルのみを更新します。
- □ ×
コマンド プロンプト
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2025 Ribbit App Development All rights reserved.
C:\users\user>xcopy Source Destination /u /s
/h - 隠しファイルとシステムファイルもコピー
通常はコピーされない隠しファイルやシステムファイルもコピーします。
- □ ×
コマンド プロンプト
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2025 Ribbit App Development All rights reserved.
C:\users\user>xcopy Source Destination /s /e /h
/k - 読み取り専用属性を保持
コピー元ファイルの読み取り専用属性をコピー先にも適用します。
- □ ×
コマンド プロンプト
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2025 Ribbit App Development All rights reserved.
C:\users\user>xcopy Source Destination /s /k
/r - 読み取り専用ファイルも上書き
読み取り専用属性が設定されたファイルも上書きコピーします。
- □ ×
コマンド プロンプト
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2025 Ribbit App Development All rights reserved.
C:\users\user>xcopy Source Destination /r
/v - コピーの検証
コピーしたファイルが正しくコピーされたか検証します。
- □ ×
コマンド プロンプト
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2025 Ribbit App Development All rights reserved.
C:\users\user>xcopy Source Destination /v /s
/q - 静音モード
コピー中のファイル名を表示しません。
- □ ×
コマンド プロンプト
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2025 Ribbit App Development All rights reserved.
C:\users\user>
xcopy Source Destination /s /q 15 個のファイルをコピーしました
C:\users\user>
/f - コピー中にファイル名を表示
コピー元とコピー先の完全なパスを表示します。
- □ ×
コマンド プロンプト
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2025 Ribbit App Development All rights reserved.
C:\users\user>
xcopy Source Destination /s /f C:\Source\file1.txt -> C:\Destination\file1.txt
C:\Source\file2.txt -> C:\Destination\file2.txt
2 個のファイルをコピーしました
C:\users\user>
/l - コピーのシミュレーション
実際にコピーせず、コピーされるファイルのリストのみを表示します。
- □ ×
コマンド プロンプト
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2025 Ribbit App Development All rights reserved.
C:\users\user>
xcopy Source Destination /s /l Source\file1.txt
Source\file2.txt
Source\SubFolder\file3.txt
3 個のファイル
C:\users\user>
/lオプションを使用すると、実際にコピーする前にどのファイルがコピーされるか確認できます。大量のファイルをコピーする前の確認に便利です。
/z - 再起動可能モード
ネットワーク経由でコピー中に接続が切断された場合、再接続後にコピーを再開できます。
- □ ×
コマンド プロンプト
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2025 Ribbit App Development All rights reserved.
C:\users\user>xcopy \\Server\Share C:\Backup\ /s /z
実践的な使用例
バックアップの作成
プロジェクトフォルダ全体をバックアップする例です。
- □ ×
コマンド プロンプト
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2025 Ribbit App Development All rights reserved.
C:\users\user>xcopy C:\Projects D:\Backup\Projects\ /s /e /h /i /y
このコマンドは:
/s /e: 空のディレクトリも含めて全てコピー
/h: 隠しファイルもコピー
/i: コピー先を自動的にディレクトリとして作成
/y: 確認なしで上書き
増分バックアップ
変更されたファイルのみをバックアップする例です。
- □ ×
コマンド プロンプト
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2025 Ribbit App Development All rights reserved.
C:\users\user>xcopy C:\Projects D:\Backup\Projects\ /s /d /y
/dオプションにより、前回のバックアップ以降に変更されたファイルのみがコピーされます。
特定の日付以降のファイルをコピー
- □ ×
コマンド プロンプト
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2025 Ribbit App Development All rights reserved.
C:\users\user>xcopy C:\Logs C:\Archive\Logs\ /s /d:01-01-2025
2025 年 1 月 1 日以降に変更されたファイルのみをコピーします。
ディレクトリ構造のみをコピー
- □ ×
コマンド プロンプト
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2025 Ribbit App Development All rights reserved.
C:\users\user>xcopy Source Destination /t /e
/tオプションにより、ファイルはコピーせず、ディレクトリ構造のみを作成します。
読み取り専用ファイルを含む完全コピー
- □ ×
コマンド プロンプト
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2025 Ribbit App Development All rights reserved.
C:\users\user>xcopy Source Destination /s /e /r /k /h
このコマンドは:
/s /e: 全てのサブディレクトリをコピー
/r: 読み取り専用ファイルも上書き
/k: 読み取り専用属性を保持
/h: 隠しファイルもコピー
バッチファイルでの使用例
日次バックアップスクリプト
@echo off
setlocal
REM バックアップ設定
set SOURCE=C:\Projects
set DESTINATION=D:\Backup\Projects
set LOGFILE=C:\Backup\backup_log.txt
echo ======================================== >> %LOGFILE%
echo バックアップ開始: %DATE% %TIME% >> %LOGFILE%
echo ======================================== >> %LOGFILE%
REM 増分バックアップの実行
xcopy %SOURCE% %DESTINATION% /s /e /d /y /h /r >> %LOGFILE% 2>&1
if errorlevel 0 (
echo バックアップが正常に完了しました >> %LOGFILE%
) else (
echo エラー: バックアップに失敗しました >> %LOGFILE%
)
echo バックアップ終了: %DATE% %TIME% >> %LOGFILE%
echo. >> %LOGFILE%
endlocal
exit
コピー前の確認スクリプト
@echo off
setlocal
set SOURCE=C:\Source
set DESTINATION=C:\Destination
echo コピーされるファイルのリスト:
echo ================================
xcopy %SOURCE% %DESTINATION% /s /l
echo.
echo 上記のファイルをコピーしますか? (Y/N)
choice /c YN /n
if errorlevel 2 goto :cancel
if errorlevel 1 goto :copy
:copy
echo.
echo コピーを開始します...
xcopy %SOURCE% %DESTINATION% /s /e /i /y
echo コピーが完了しました。
goto :end
:cancel
echo コピーがキャンセルされました。
goto :end
:end
endlocal
exit
エラーコードと対処法
xcopyコマンドは、実行結果に応じて以下の終了コード(ErrorLevel)を返します。
| 終了コード | 説明 |
|---|
| 0 | ファイルは正常にコピーされました |
| 1 | コピーするファイルが見つかりませんでした |
| 2 | ユーザーが Ctrl+C を押して xcopy を終了しました |
| 4 | 初期化エラーが発生しました(メモリ不足、ディスク容量不足、無効な構文など) |
| 5 | ディスク書き込みエラーが発生しました |
エラーハンドリングの例
@echo off
xcopy Source Destination /s /e
if errorlevel 5 goto :disk_error
if errorlevel 4 goto :init_error
if errorlevel 2 goto :user_cancel
if errorlevel 1 goto :no_files
if errorlevel 0 goto :success
:disk_error
echo エラー: ディスク書き込みエラーが発生しました
goto :end
:init_error
echo エラー: メモリ不足またはディスク容量不足です
goto :end
:user_cancel
echo ユーザーによってキャンセルされました
goto :end
:no_files
echo コピーするファイルが見つかりませんでした
goto :end
:success
echo ファイルは正常にコピーされました
:end
exit
まとめ
この記事では、xcopyコマンドについて以下の内容を解説しました:
- 基本的な使い方: ファイルとディレクトリのコピー方法
- 主要なオプション: サブディレクトリコピー、日付フィルター、属性保持など
- 実践的な使用例: バックアップ、増分コピー、ディレクトリ構造の複製
- バッチファイルでの活用: 日次バックアップスクリプトなど
- エラーハンドリング: 終了コードを使った適切なエラー処理
xcopyコマンドは、copyコマンドよりも高度なファイルコピー機能を提供し、バックアップやファイル管理の自動化に非常に便利です。特に、/s /e /dなどのオプションを組み合わせることで、効率的な増分バックアップシステムを構築できます。
より高度なファイルコピーやミラーリングが必要な場合は、robocopyコマンドの使用を検討してください。robocopyは、xcopyよりもさらに豊富な機能を提供します。
類似コマンド
練習問題
#コマンドプロンプト
#バッチファイル
#ファイル操作
#コピー
#コマンド