Windows – Is it possible to get an `errorlevel 1` from `xcopy` in Windows 10

batch filecommand linewindowswindows 10xcopy

Microsoft documentation states that the xcopy exit codes are

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.

I'm running a Windows 10 64bit system (spanish locale), and I've tried everything I could think of to get an errorlevel 1 from xcopy, but I've not been able. And by everything I mean the only thing I did not test was /N with long file names in a source where short names are not enabled (in my environment I can not test it). The usual suspects non matching wildcards, non present file names, empty folders, /D, /EXCLUDE, /A, /M, /U, … were tested.

I though it should be easy (not files found?, easy), but I was wrong, it was not, so I debugged xcopy.exe and this is what I saw:

  • xcopy internally uses a method called DisplayMessageAndExit(messageID, WSTRING, exitCode) to, well, display a message and exit. It also uses several exit calls to leave the program in some cases (ex. Ctrl-C)

  • I have not found any exit call that could use a value of 1

  • From the list of calls made to DisplayMessageAndExit, none of them pass/receive a value of 1 as exit code.

  • From the list of calls made to DisplayMessageAndExit only one of them use the MessageID 0x5622 (in my install the resources associated are in C:\Windows\System32\es-ES\ulib.dll.mui, it is locale dependent), that is No se encuentra el archivo: %1 (File not found - %1), and the exit code used in the call is 4

Maybe there is a way in which xcopy is able to generate the 1 exit code, but I don't know how.

So the question is: Is there a way to get errorlevel 1 exit code from xcopy? What am I missing?

Best Answer

The only thing I can think of is some sort of race condition that between xcopy gathering the list of files it will copy and actually doing the copy, some other process deletes or moves the files.

If you can get an error 1 if only SOME of the source files were missing then it might be able to do this on purpose. About the only way I can think to do this on purpose is to have 2 or more separate processes running, one xcopy and one delete or move command, all operating on the same source files. You will need a source with lots of files in them to give the competing processes time to overtake each other.

  1. start a move, del, or robocopy /mov command on a folder with lots of files in it.
  2. in another window, and well before the first command finishes start your xcopy command

Omitting the /C switch may mean that if it is the first file xcopy tries and fails on it will give up straight away without copying any files, thus satisfying the condition of NO files copied.

Related Question