1

Bear with me here; I'm super new to coding something like this, and I think I've found a simple way to do it, but I'm running into problems. I also couldn't really find an answer from Googling or looking around here, but if it's already been answered, I apologize!

I'm running the following command in a .bat file:

xcopy D:\SHARENAME \\NAS-IP-ADDRESS\SHARENAME /s /e /d

When I run it once as a .bat file, it does its copy operations, and then closes the terminal window, signaling completion. I can verify that a copy operation occurred, since new files do show up in the Backup directory.

However, once I set this .bat file as a Windows Scheduled Task, it never stops running. The copy still occurs one time, but the task does not end. I want it to run every day at 3AM, which is fine, but it never gets the chance to run after its first time, since it never actually stops. Am I missing something to close out this .bat file in order to stop it properly?

Stephan
  • 53,940
  • 10
  • 58
  • 91
nightsurfer
  • 113
  • 7
  • Does it copy when it runs as a task? – FJT Apr 20 '18 at 18:17
  • @FJT yes, I can confirm that copies do occur. But they only happen the one time after the task runs at 3AM, they will not occur on the next day. – nightsurfer Apr 20 '18 at 18:18
  • It is possible that the task scheduler is running from a different working directory, _I'd suggest you use full paths in your batch file_. You may also face the scenario that any network shares are not established at the scheduled trigger time or may even require login/password or have permissions restrictions depending upon the user or group you've set the task to run under. Perhaps it would help if you were to provide those details in your question by [edit](https://stackoverflow.com/posts/49947524/edit)ing it accordingly. – Compo Apr 20 '18 at 18:59
  • Hey @Compo this isn't my first rodeo on a Stack Exchange site, but the tips are appreciated! The paths are as full as possible, however the pointer about login/password is something to consider. The task runs as SYSTEM, but it's possible that it doesn't have access to the NAS. I'll check it out. – nightsurfer Apr 20 '18 at 19:11
  • 1
    Unrelated to your issue, there's no need to use both the **`/S`** and **`/E`** options. _You only require one of them, in this case I'd suggest **`/S`**_. – Compo Apr 20 '18 at 19:23
  • @Compo Microsoft's official documentation says to use /S with /E, so I figured I might as well. Doesn't seem to be hurting anything to use them, and I want to copy empty directories anyway. – nightsurfer Apr 20 '18 at 19:57
  • why not add `&& exit` directly after the string. `xcopy D:\SHARENAME \\NAS-IP-ADDRESS\SHARENAME /s /e /d && exit` if the batch exits and task still shows as running, then something is wrong with the task scheduler config. – Gerhard Apr 21 '18 at 07:08

1 Answers1

2

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.

Mofi
  • 46,139
  • 17
  • 80
  • 143