xcopy: move files instead of copy?

I would like to use xcopy to move, not copy files across a network with the Verify flag. I could not find a switch on xcopy to move files, is there an xmove I can use that has verify?

At the moment I am using xcopy /D /V but need to get rid of the files at the source only when it is verified a file was successfully copied to the destination.


Solution 1:

You should check out robocopy, it is much more powerful than xcopy. You can easily move files with /MOV or /MOVE.

To move files only (delete from source after copying)

robocopy from_folder to_folder files_to_copy /MOV

To move files and directories (delete from source after copying)

robocopy from_folder to_folder files_to_copy /MOVE

http://ss64.com/nt/robocopy.html

Solution 2:

You could use a batch file to run your Xcopy command with the verify, followed by a check of the error level returned by Xcopy to determine if the files copied successfully or not. If they did, delete the source.

From the Xcopy documentation:

Exit
code  Description
====  ===========
  0   Files were copied without error.
  1   No files were found to copy.
  2   The user pressed CTRL+C to terminate xcopy.
  4   Initialization error occurred. There is not
      enough memory or disk space, or you entered
      an invalid drive name or invalid syntax on
      the command line.
  5   Disk write error occurred.

Example batch:

Rem Attempt file copy...
xcopy /D /V %1 %2

Rem Check result code and if it was successful (0), delete the source.
if errorlevel 0 (
    echo Copy completed successfully
    del /Q %1
    exit /B
)

Rem Not Errorlevel 0...
echo Copy failed for some reason.