Generate error at the end of the copy if any files are not copied

1

I have a batch that copies some files on the network, this copy updates the previous files to the newest cases if they already exist in the destination folder, but if any files are in use the system does not display the error message in the end just the moment you are trying to update the file.

At the moment I do not want to solve this problem of the file being in use, I would only like the command to report only at the end of the copy if there was an error in updating some file.

I put the condition if "%errorlevel%"=="0" at the end but this condition does not work if there were errors in the middle of the copy.

Follow my command:

xcopy "C:\Origem\." "C:\Destino\" /c /d /e /h /i /k /r /y
    
asked by anonymous 07.06.2017 / 15:22

1 answer

2

Using XCopy , and as documentation , you can capture any possible errors after the command execution:

  

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

Doing some testing here, with all the parameters that passed does not return error, for example, the parameter /c , indicates to ignore error, so it does not make sense to have the parameter /d , indicates to replace files by but as it is passing /y , to replace if it exists, does not make sense either.

Soon your command would be:

xcopy "C:\Origem\." "C:\Destino\" /e /h /i /k /y /r

When you execute the above command, and a file is being used, it returns:

xcopy "C:\Origem\." "C:\Destino\" /e /h /i /k /y /r
C:\Destino\arquivo.exe
Violação de compartilhamento

And if you perform the command:

echo %errorlevel%

We have the return: 4

In this way you can do a validation if the result of ErrorLevel is different from 4, that is to say that some file was not copied, or save the xcopy message to a log file, like this:

xcopy "C:\x\." "C:\a\bat\" /e /h /i /k /y /r > log.txt

That way you will have the files that were not copied into this log file.

    
08.06.2017 / 16:25