|The /Z parameter may work, but also behold this:|
- What may be wrong, is that a process on the source is still using the file, and possibly locking it. This may prevent the copy command to fail on that file ..
- I see you copy to Y:\%USERNAME% ... but, how unique is that directory ? If - at the same time - multiple clients, connected as the same user, try to run the script, this may cause locking on the target server. Same here ... copy commands may fail randomly ...
- The problem with targetfile already deleted but new sourcefile not copied ... check all of the above mentioned things in this thread, and if that does not work : normal copy commands are protected for that. Maybe you then need to check the copy parameters ... or, you build your own check : start with making an extra backup on target, THEN try to copy source to target, THEN check existance of new target. If an error occured, replace old target.