1

I have the one batch that does the copy of some files in the network, this copy updates the previous files by the newest case they already exist in the destination folder, however if any file is in use the system does not present the error message at the end only in the Moment you are trying to update the file.

At the moment I do not want to solve this file problem 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 in the end the condition if "%errorlevel%" == "0" but this condition does not work if there were errors in the middle of the copy.

My command:

xcopy "C:\origin\." "C:\destination\" /c /d /e /h /i /k /r /y
Laércio Lopes
  • 132
  • 2
  • 9

1 Answers1

3

What I suggest for you is to loop through the folder and check if each file is in use, and then you can write the name of that file to some type of log, maybe a .txt file to read later to show what could not be overwritten.

2>nul (
  >>FILE.EXT (call )
) && (echo file is not locked) || (echo file is locked)

This code, which I found at StackOverflow (dbenham deserves all the credit for this) could be used to check if a file is locked. Replace FILE.EXT with your file name, preferably in a loop to easily check every file, and if the file is locked echo the file name to a (temporary) .txt file and once completed, loop through that text file to list the results.

An added benefit to this is, if you check for files in use before trying to overwrite, you could skip files you know you can't write to thus saving a bit of time per file.

GrumpyCrouton
  • 8,486
  • 7
  • 32
  • 71