robocopy cancelling batch file processing

May 30, 2008
14
0
I have a set of (.BTM) batch files that I use to update offline copies of my working directories that use the robocopy program. The top level batch files call a common batch file to perform the work in a uniform way. I have noticed that occasionally batch processing terminates prematurely. I have attempted to work around the issue by looping until a flag is set in both the common batch file and the top level batch files, the looping code never gets a chance to continue the processing because it acts like a cancel command was encountered. If I manually rerun the top level batch file it will typically run normally but it may also terminate at another point in the processing.

I recently modified my common batch file to enable command echoing just before running robocopy and got lucky when the issue occurred when synchronizing the first directory. The invocation of robocopy completed and the batch file continued running to next two statements which were on break and a return. At this point the batch file processing was cancelled with a call, 3 gosubs, an iff , and another gosub pending.

This issue occurs on multiple different PCs. All of the PCs are currently running Windows 10, but if I remember correctly the issue also occured under Windows 7. The working directories are on the C: drive and the offline mirror copies are on USB 3.0 drives (SSD or magnetic).

The common batch file can perform the processing using either robocopy or the builtin TCC copy command. I have never seen this issue occur with the copy command but robocopy is about 10 times faster than the copy command.
 
Last edited:
May 20, 2008
11,046
90
Syracuse, NY, USA
I don't know how an external EXE (robocopy) could cause a batch file to terminate unless the batch file were somehow cooperating. Can you give more information?
 
May 30, 2008
235
2
I've noticed a similar issue when using TCC 20 on Windows 10.

Often the batch file just stops prematurely after a ROBOCOPY command completes even though there are many more ROBOCOPY calls left in the file. No error messages are output.

IF you re-run the batch file then it always continues successfully after the ROBOCOPY call where it stopped last time. Probably because there are no changes to copy, and no output from robocopy to display. But a subsequent robocopy call can stop batch processing instead. So just continue re-running until all robocopy calls are completed, lol.

The batch file just calls robocopy directly and does not check the error code from it, no other batch files are called.

This only happens with TCC 20, I also have TCC 15 and there the problem never occurs with the same batch file, same Windows 10 and same computer.

I would guess it has something to do with ANSI processing myself, but have never taken the time to investigate, easier to just re-run the file when not complete.

I attached a simplified version of the batch file.
 

Attachments

  • generic_backup.btm
    567 bytes · Views: 106

rps

Jul 6, 2008
429
5
Have you tried using the START command vs CALL with the ROBOCOPY line?
That should remove any interaction with your batch file.
 
Apr 2, 2011
1,363
6
53
North Carolina, USA
From the CHM help file:

CALL

Purpose: Execute one batch file from within another.

You do not want a batch file to run from the BTM - you want a EXE to run. Have you looked at the DEFER command?

==================
Purpose:Execute a command after the batch file exits

Format:DEFER command

Usage:

A batch file can have multiple DEFER commands. They will be executed in first in, first out order when the batch file exits.

If you have variables on the DEFER command line, they will be expanded before the DEFER command is processed, not when command is executed. To delay variable expansion until command is executed, use single back quotes around the variable names, or double the %'s before the variable names.

If the last argument on the line is a single (, it is interpreted as the beginning of a command group. DEFER will append the following lines (in a batch file) or prompt you for more input (at the command line) until it gets a closing ).



 
May 30, 2008
235
2
I cannot use START since I want the batch file to wait for ROBOCOPY to complete. START will open it in a new separate console and the batch file will continue to the next robocopy directly.

Yes, I can agree that CALL is unnecessary here as an unqualified ROPOCOPY call will run robocopy.exe on my current system.

I often use CALL from batch files anyway, in case I would have a batch file with the same name as the command, in that case a direct call would replace the current batch file, while with CALL it will continue from the current line afterwards.

Haven't seen that using CALL would ever have hurt when used to run executables and aliases.
(Keep in mind that this works fine in TCC 15)

But sure, could test without it next time I use the batch file.
 
May 26, 2008
535
4
Don't use START... or if you do, use START /WAIT. But that is completely unnecessary. Just run robocopy.exe directly with nothing preceding it!
 
May 30, 2008
235
2
Just tested it again with CALL removed.

And what do you know, the problem totally disappeared, everything went through at the first attempt!

So problem solved on my part, thanks for the suggestions everyone!

Apparently the robocopy process somehow returned the CANCEL "code" at times causing the batch processing to end even though it was not a batch file. Guess it happened to work in TCC15 due to different internal implementation details.
 
Aug 23, 2010
619
9
I cannot use START since I want the batch file to wait for ROBOCOPY to complete. START will open it in a new separate console and the batch file will continue to the next robocopy directly.
START "" /B ... will run in the same console and will be nearly identical to DEFER with exception that your batch file will continue after spawning the task, not before.
Be aware that it might mess the screen, if backgrounded task wold output anything.
 
May 30, 2008
14
0
Has anyone noticed that that ROBOCOPY is unusual in that it modifies the window title. I have not found any other external program that modify the window title.

I have continued to periodically debug this problem and have determined that it only happens when returning from a subroutine that invokes ROBOCOPY, with the RETURN command being treated like a CANCEL command. Until today, I was never able to consistently reproduce the problem.

While working on another batch file I encountered a situation that always causes the batch file to terminate early. The batch file uses a subroutine to process one directory at a time using ROBOCOPY. The subroutine generates the source and destination directory names from a GOSUB parameter and other environment variables, invokes ROBOCOPY, and then returns. When encountering the return statement the batch file always terminates (like the return is a CANCEL statement). I determined that if I add a WINDOW command (WINDOW "") to set the window title to the default, the return works correctly with execution continuing after the GOSUB command.

Starting with this new batch file, I created a minimized batch file that consistently reproduces the error with my files. I then enhanced the minimized batch file so that it could create test files, but have not been able to generated a set of test files that will recreate the problem.

Hopefully this additional information (ROBOCOPY modifying the window title causing a RETURN command being treated as a CANCEL command) will enable JP Software to be able to find the root cause.
 
May 30, 2008
235
2
Wrapping the ROBOCOPY calls in @exec[] finally fixed this problem for me.

(The removal of CALL I wrote about earlier in the thread did not help after all)
 
May 30, 2008
14
0
My batch file already had "on break goto ..." statements before invoking ROBOCOPY and "on break" statements after (including in the destination of the "on break goto ..."), shouldn't that have handled ROBOCOPY sending a Ctrl-C / Ctrl-Break?

About 30 days ago, I modified my batch file to wrap the invocations of ROBOCOPY in @exec[] as per nikbackm's comment and have not had the batch file terminate prematurely since.
 
Jan 18, 2011
11
0
Same problem for many, many years. The robocopy is never interrupted, so that helps a lot. I run the thing for variously between 80 and 100 root directories across 6 disks and it almost always stops after just a few roots folders and then only occasionally after restarting till its done. It's annoying but I do like relying on a clean backup for my portable apps and data.

I have fixed the basic batch file so that it can skip the initial checking and start at any of the disks. Redoing a few root folders goes really quickly (partly since they are already mirrored, but maybe memory caching plays a role as well). In fact I have an alias that restarts from the beginning of whatever disk letter I give it up to 4 times. Not elegant, but not unreasonable even if, when it finally finishes, it has backed up a couple of disks two or maybe three times.

Still it makes a 3 to 5 minute job into a 4 to 6 minute one and requires at least one user intervention to complete.

I wonder if the problem occurs with cmd.exe. Might be a good job for jpsoft to check out.
 
May 30, 2008
235
2
Same problem for many, many years. The robocopy is never interrupted, so that helps a lot. I run the thing for variously between 80 and 100 root directories across 6 disks and it almost always stops after just a few roots folders and then only occasionally after restarting till its done. It's annoying but I do like relying on a clean backup for my portable apps and data.

I have fixed the basic batch file so that it can skip the initial checking and start at any of the disks. Redoing a few root folders goes really quickly (partly since they are already mirrored, but maybe memory caching plays a role as well). In fact I have an alias that restarts from the beginning of whatever disk letter I give it up to 4 times. Not elegant, but not unreasonable even if, when it finally finishes, it has backed up a couple of disks two or maybe three times.

Still it makes a 3 to 5 minute job into a 4 to 6 minute one and requires at least one user intervention to complete.

I wonder if the problem occurs with cmd.exe. Might be a good job for jpsoft to check out.

Did you try wrapping ROBOCOPY in @exec?
 
Jan 18, 2011
11
0
I did, just now. Wow! It works and speeds up the process a lot. It must be that @exec raises the priority; I wonder if a high priority start would do the same thing?

Thanks Niklas, you saved me probably ten minutes a week to say nothing of enough of an annoyance to keep me from doing backups as often as I should. And of course you saved the machine a lot more time than than.

This backup routine is supposed to be so easy and quick that I could do it between coffee sips. I timed it when there were only a couple of tiny files to update and it ran in 5 seconds. That's mirroring 74 folders and 130Gb. Such speed was probably colored by it being done not long after I had already done it the old way and so probably caching played a role.
 
May 26, 2008
535
4
@Jack Weeks if you aren't using it yet, try adding the /MT switch to robocopy. In some situations this can improve performance even more, especially when you're doing update synchronizations.
 
Jan 18, 2011
11
0
Thanks I'll try that when I update a backup on a disk that hasn't been backed up to in a while. I might start with say 30 threads and see if that helps.
 
May 26, 2008
535
4
30 threads is probably too aggressive. There is a point where you'll stop seeing speed increases as you increase threads, and it could actually start getting slower. Depends on a lot of variables. The default of 8 might even be too high. I'd probably compare 2, 4, and 8. If 8 is faster than 4, then continue testing above 8.