13

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.

1
  • 1
    You should be aware that even using /v does not guarantee that a file has been correctly written: disc and OS caches mean that a file in a write cache will be retrieved from there before it has been committed successfully. The only safe option is to make a copy and schedule a verify and delete at a future time. Heuristically I would say that 5 minutes would be enough, but there is no hard and fast rule: it depends on the disc activity and the size of the caches (I speak as one who has encountered cached write failures). – AFH Oct 21 '14 at 19:34
11

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

4
  • 2
    Robocopy does not have a verify as far as I can see. – JBurace Oct 21 '14 at 18:04
  • @JBurace If you look at the link I posted it states: /MOVE : Move files and dirs (delete from source after copying). If it doesn't copy, it won't remove the source. – imtheman Oct 21 '14 at 18:05
  • 2
    What happens if it gets moved, but the filesize doesn't match? This is why I need to do a verify. I don't see anything in the robocopy info that it actually verifies the two filesizes to ensure it wasn't just moved but moved correctly. – JBurace Oct 21 '14 at 18:11
  • @JBurace I don't think it works like that. If it doesn't copy all of the file, then it will act as if nothing copied at all, and therefore will not remove the source. – imtheman Oct 21 '14 at 18:17
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.
4
  • Does errorlevel still work if I'm doing: dir args && xcopy args >> logfile.txt? Or will errorlevel be a result of dir? – JBurace Oct 21 '14 at 20:13
  • It will contain the result of the last command run. You can test it with a simple batch file as such: pastebin.com/6GwNA7MP – Ƭᴇcʜιᴇ007 Oct 21 '14 at 20:27
  • if errorlevel 0 ALWAYS triggers. Because it is really checking for if errorlevel >= 0 so you should instead check the error case if errorlevel 1 ( ... failure case ... ) else ( ... success case ... ). Or, if you prefer the style if not errorlevel 0 (... success case ... ) Or, if you prefer the style if %ERRORLEVEL% EQU 0 ( ... success case ... ) – Jesse Chisholm Mar 8 '18 at 21:45
  • Of course this would be far less efficient (move is super cheap in the same hard drive)... – Ohad Schneider Mar 19 '19 at 16:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.