The scheduled task does not end because of XCOPY prompts on copying a file existing already in destination directory if it should be overwritten or not. This prompt is not answered by anybody and so XCOPY waits forever. So cmd.exe
never ends the batch file execution and for that reason scheduled task also never ends.
The solution is using the command line:
%SystemRoot%\System32\xcopy.exe "D:\SHARENAME" "\\NAS-IP-ADDRESS\SHARENAME\" /C /E /H /K /Q /R /Y
Most important to fix this issue is the option /Y
which avoids the overwrite prompt.
Microsoft's xcopy documentation contains currently the information:
By default, you are prompted to overwrite, unless you run xcopy from within a batch script.
This is a wrong information. XCOPY is an executable in system folder of Windows and does not know if being executed by cmd.exe
from within a command prompt window or from within a batch script. /Y
must be specified on command line or there is an environment variable COPYCMD
containing /Y
in value to avoid the overwrite prompt. /Y
must not be used on command line on using copy being an internal command of cmd.exe
on using COPY in a batch script.
The help of XCOPY output on running in a command prompt window xcopy /?
explains all other options used above. At bottom of output help can be read:
The switch /Y may be present in the COPYCMD environment variable.
This may be overridden with /-Y on the command line.
The help of command COPY output on running copy /?
ends with:
The switch /Y may be present in the COPYCMD environment variable.
This may be overridden with /-Y on the command line. Default is
to prompt on overwrites unless COPY command is being executed from
within a batch script.
The additional third sentence makes the difference regarding to /Y
between XCOPY and COPY on usage from within a batch script.
The help of XCOPY contains also the explanation for /S
and /E
.
/S Copies directories and subdirectories except empty ones.
/E Copies directories and subdirectories, including empty ones.
Same as /S /E. May be used to modify /T.
Same as /S /E.
is a confusing description. Many beginners think it is necessary to specify /S
and /E
to get entire directory structure with empty directories copied. But this is not true. It is meant that just /E
is enough to copy an entire directory structure with including empty directories and /S /E
is interpreted like just /E
. It is superfluous to specify /S
(copy without empty directories) in addition to /E
(copy with empty directories) on XCOPY command line.
I recommend reading the SS64 - XCOPY documentation which is currently better than Microsoft's documentation.
Very important on copying one or more files and directories with XCOPY into a specified destination directory is specifying the destination path with a backslash at end. This makes it clear for XCOPY that the destination string specifies a directory and not a file. Otherwise it is necessary to specify additionally /I
on copying multiple files or an entire directory tree to inform XCOPY that the destination argument should be interpreted as folder path. But /I
does not avoid a prompt if a single file is copied with XCOPY and the destination argument does not end with a backslash, see BATCH file asks for file or folder for details. Therefore it is highly recommended to specify destination folder path always with a backslash at end on copying one or more files to a folder using XCOPY.