General discussion


Xcopy and batch file

By Blackcurrant ·

I have created a batch file which copies new and changed files from a networked storage device to a computer

I am using the xcopy command and have used the following syntax:

start /wait /b xcopy "source" "destination" /c /d /s /e /k /y /z /i

at the start and end of the batch file I have included eventcreate commands to log when the batch file starts and finishes

Now, when I was testing the file, I see a command window, and can see that the eventcreate command successfully writes entries to the application log stating the batch file has started, and later an entry is written stating the file has finished. However, after the second eventcreate command has been processed, and I can see the event in the application log, the batch file continues.

When the batch file runs the command window shows the full path for each command in the batch file:

c:\computers\Batch files\Backup\start /wait /b xcopy "source" "destination" /c /d /s /e /k /y /z /i

After the second eventcreate command has been processed I see a long list of:

0 files copied
1 files copied
0 files copied
0 files copied
5 files copied
0 files copied
2 files copied

Does anyone know what is happening here? I assumed that the second eventcreate command at the end of the batch file would be the last command to be processed, especially as all preceeding commands use the start /wait switch.

I was wondering if this was a verification process... but think not.

This conversation is currently closed to new comments.

Thread display: Collapse - | Expand +

All Comments

Collapse -

by master3bs In reply to Xcopy and batch file

This is telling you how many files are being copied. When it says 0 files copied; it has either come across a blank directory or files that are not newer than the destination file. (as per the /d option)

the "source"filename.ext part is telling you that it has changed and started coping from a different source directory. You are using the /s and /e options which tells the batch file to copy subdirectories including empty ones.

Collapse -

by Blackcurrant In reply to

Thank you for answering. I understand what you are saying, but the /wait switch should force each command to complete before the next command is started.

Collapse -

by Toivo Talikka In reply to Xcopy and batch file

The commands in your batch file are started in sequence but seem to get executed asynchronously, in which case the second eventcreate is performed before the copy operations finish.

If you remove "start /wait /b" from the beginning of the lines containing xcopy, the event at the end of the batch file will be written to the event log after the xcopy operations are finished.

Collapse -

by Blackcurrant In reply to

Thanks for answering. If I remove the start /wait /b command, then I will have no control over how the commands are executed. As I have said above, the /wait switch is supposed to prevent the next command from starting until the current one has finished.

Collapse -

by sgt_shultz In reply to Xcopy and batch file

wow. thanks for start/wait and eventcreate samples. i never heard of them.
probably because i use robocopy, a free robust copying utility from MS downloads. it makes it's own log and very very flexible

Collapse -

by Blackcurrant In reply to Xcopy and batch file

Thanks for the answers that have been posted so far.

When I initially wrote the batch file I included a generic xcopy "source" "destination" -switches command, using *.* for the whole structure to copy an entire folder structure from the storage device to the computer. Consequently I received out of memory messages and the xcopy command could not continue.

So, I split the xcopy command up to copy between 20-30 separate folders.

This is why I used the Start /wait command to force each xcopy command to finish before the next one started. Also, I read that including the /b switch prevents a new command window from opening for each Start /wait command.

I just cannot figure out why the process continues, and why the output is so different. even though

Collapse -

by Toivo Talikka In reply to Xcopy and batch file

Yes, the start method with /wait is 'supposed' to guarantee sequential or synchronous processing of commands. I noticed that by adding /I to the start parameters, the execution of the last eventcreate command will happen much later than without the /I parameter, but it still happens before the xcopy messages get displayed.

There is an article in MSDN about multitasking and synchronization in batch files which you may find useful to find a workaround:

Collapse -

by Toivo Talikka In reply to

Another workaround, based on ideas from Dr Nitin Paranjape's article about conditional execution at

Separate each xcopy command to its own .cmd file, to be executed only if the preceding command succeeded. Here is an example - filename of each .cmd file followed by its contents:


eventcreate /t information /id 999 /l application /d "xcopy sequence starts" /so "test20" && test21.cmd


xcopy \toivo\test\aaa\*.* \toivo\test\xxx1\. /c /d /s /e /k /y /z /i && ( test22.cmd ) || ( eventcreate /t information /id 999 /l application /d "xcopy failed" /so "test21" )


xcopy \toivo\test\bbb\*.* \toivo\test\xxx2\. /c /d /s /e /k /y /z /i && ( test23.cmd ) || ( eventcreate /t information /id 999 /l application /d "xcopy failed" /so "test22" )


xcopy \toivo\test\ccc\*.* \toivo\test\xxx3\. /c /d /s /e /k /y /z /i && ( test24.cmd ) || ( eventcreate /t information /id 999 /l application /d "xcopy failed" /so "test23" )


eventcreate /L Application /T Information /ID 999 /SO "Test24" /D "End of batch file"

Collapse -

by Blackcurrant In reply to

Toivo - this is awesome stuff. Thank you for taking the time to compile this information for me. I really appreciate it, it must have taken you some time to do this.

Related Discussions

Related Forums