Generate error at the end of the copy in case some file is not copied

Asked

Viewed 1,450 times

2

I have a batch that copies some files on the network, this copy updates the previous files by the newer ones if they already exist in the destination folder, but if any file is in use the system does not display the error message at the end just at the time it is trying to update the file.

At the moment I don’t want to solve this file problem being in use, I just wish the command would inform only at the end of the copy if there was error in updating some file.

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

Follow my lead:

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

2 answers

2

Using the XCopy,and as per documentation, it is possible to capture possible errors after executing the command:

0 = Files were copied without error.
1 = No file found to copy.
2 = User pressed CTRL+C to finish xcopy.
4 = Initialization error occurred. Not enough memory or disk space, or you have entered an invalid drive name or invalid syntax in the command line.
5 = Disk write error occurred.

Doing some tests here, with all the parameters you passed does not return error, because for example, the parameter /c, indicates to ignore error, so it makes no sense to have, parameter /d, indicates to replace files with newer ones, but how is passing the /y, to replace if it exists, it also makes no sense.

Soon your command would be:

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

When performing 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 when performing the command:

echo %errorlevel%

We have the return: 4

This way you can make a validation if the result of the ErrorLevel is other than 4, meaning that some file has not been copied, or save the message from xcopy in a log file, thus:

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

This way you will have the files that were not copied in this log file.

  • 1

    thanks for the answer, I voted as useful but I need to resolve this question, still need the command to continue the copy if errors occur, with this command it does not continue copying, only need to inform at the end if there was any file not copied.

1


inserir a descrição da imagem aqui


How about implementing a looping for the copy action?

I suggest operating the copy of the files one by one isolating/deleting possible error message and logging if the file was not copied in your log.

Leaving the display of log content to the end of the looping in case of an error.


@echo off & setlocal EnableDelayedExpansion & cd /d "%~dp0"

set "_destino=C:\Destino" & set "_log_erro=%temp%\xCopyLog.log" & title .:^| *** Aguarde... *** ^|:.

set "_origem=C:\Origem" & cd /d "!_origem!" & type nul >"!_log_erro!" && echo/
for /f %%h in ('cmd /u /c set /p "=%_origem:"=%"^<nul^|find /c "\"')do set /a _t=%%~h

if !_t! geq 1 set /a "_td=!_t! + 2" && set "_scr_msg=Copias dos arquivos em andamento, favor aguardar^!!..."
set "_tokens=^delims^=^\^ ^tokens^=^%_td%*" && echo/f>"%temp%\_f.log"

echo/ & echo/Criando/atualizando arvore de pastas no destino^!!... 
robocopy "!_origem!" "!_destino!" /e /xf * >nul 2>nul & echo/ & echo/!_scr_msg!

for /f "tokens=*delims= " %%f in ('where /r .\ "*.*"')do for /f %_tokens% %%F in ('echo/%%~ff')do (
     set "_msg_er=<nul" & set "_msg_er=Arquivo em uso e/ou nao copiado: "%%f" "
     xcopy "%%~ff" "!_destino!\%%~F"  /c /h /r /v /y <"%temp%\_f.log" 2>nul >nul || echo/!_msg_er! >>"!_log_erro!"
     )

for /f "usebackq" %%a in (`find /V /C "^^^" ^< "!_log_erro!"`)do set "_err_cnt=%%a" & echo/
if !_err_cnt! geq 1 (echo/Total de arquivo^(s^) n copiado^(s^): !_err_cnt!) else (echo/Todos arquivos foram copiados^!!) 

echo/ & type "!_log_erro!"|findstr . | more & title <nul & type "!_log_erro!"| clip 
(del /q /f "!_log_erro!" & del /q /f "%temp%\_f.log") 2>nul >nul && timeout -1 & exit /b

Note: 1) Has 1 space after the \ in the: delims=\

Note: 2) Copy log content to your Clipboard (Ctrl+C)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.