我正在尝试使用Robocopy替换cmd文件 [英] I'm trying to use Robocopy to replace a cmd file

查看:237
本文介绍了我正在尝试使用Robocopy替换cmd文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个cmd文件,该文件使用Robocopy命令来更新PC上的某些文件,但是我无法替换该cmd文件,因为该文件包含进行更新的Robocopy脚本。您如何替换正在执行替换的文件?

I've created a cmd file which uses the Robocopy command to update some files on the PC, but I can't replace the cmd files, because this contains the Robocopy script which is doing the updating. How do you replace a file which is doing the replacing?

我已将cmd文件移动到另一个目录,这使我可以更新大多数文件,但是我仍然无法替换cmd文件。

I've moved the cmd file to another directory, which allows me to update most of the files, but I still can't replace the cmd file.

我在Robocopy中使用的标志是 / MIR / Copy:DAT / DCOPY:T

The Flags I'm using in Robocopy are /MIR /Copy:DAT /DCOPY:T

Robocopy在cmd文件处停止,我无法替换。

The Robocopy stopped at the cmd file and I can't replace it.

推荐答案

我没有看到%SystemRoot%\System32\robocopy.exe 无法复制当前原因的任何原因由 cmd.exe 处理后,此批处理文件会在文本编辑器之类的应用程序中另外打开,只要在应用程序中打开该文件,就可以防止文件的写访问和删除。

I don't see any reason for %SystemRoot%\System32\robocopy.exe failing to copy the batch file currently processed by cmd.exe than this batch file is additionally opened in an application like a text editor which prevents write access and deletion of the file as long as it is opened in the application.

但是,将以下代码添加到您的批处理文件中时,内容未知。

However, the following code added to your batch file with unknown content could solve the problem.

@echo off
if /I not "%~dp0" == "%TEMP%\" (
    copy /Y "%~f0" "%TEMP%" >nul 2>&1
    if exist "%TEMP%\%~nx0" (
        set "CurrentDirectory=%CD%"
        set "InitialExecution=%~dp0"
        cd /D "%TEMP%"
        "%TEMP%\%~nx0" %*
    )
)

rem Insert here other commands to execute by the batch
rem file now running from directory of temporary files.
rem The next three commands are only for demonstration.

if defined CurrentDirectory echo Initial current directory: %CurrentDirectory%
if defined InitialExecution echo Initial execution path:    %InitialExecution%
pause

set "InitialExecution="
if defined CurrentDirectory set "CurrentDirectory=" & cd /D "%CurrentDirectory%" 2>nul & (goto) 2>nul & del "%~f0"

此批处理文件首先检查是否从目录启动以查找临时文件。双击批处理文件时不是这种情况,除了用户偶然将批处理文件存储在目录中作为临时文件,然后在该目录中双击它。如果批处理文件未存储在临时文件的目录中,则会执行以下操作:

This batch file first checks if it is started from directory for temporary files. This is not the case on double clicking on the batch file, except the batch file is stored by chance in directory for temporary files by the user and double clicked on it in this directory. If batch file is not stored in directory for temporary files, it does following:


  1. 批处理文件将自身复制到临时文件目录(仅

  2. 它验证文件复制是否真的成功,并且应该始终为真。

  3. 它定义两个环境变量,它们的当前路径为目录和初始执行路径以供以后使用。

  4. 它将当前目录设置为临时文件的目录。

    这样甚至可以删除包含批处理文件的目录。双击当前用户执行的本地驱动器上存储的批处理文件时,批处理文件目录也为当前目录。

  5. 该批处理文件从目录中运行,用于临时文件。在初始执行时将传递给批处理文件的所有参数进一步传递给副本。

  1. The batch file copies itself to directory of temporary files (only read access).
  2. It verifies if the file copy was really successful which should be always true.
  3. It defines two environment variables with path of current directory and initial execution path for later usage.
  4. It sets the current directory to directory for temporary files.
    This makes it possible to even delete the directory containing batch file on batch file directory being also current directory as typical on double clicking on a batch file stored on a local drive executed by current user.
  5. The batch file runs itself from within directory for temporary files with passing all arguments passed to the batch file on initial execution further on its copy.

Windows命令处理器 cmd .exe 执行批处理f ile继续在临时文件目录中的副本上继续执行批处理文件处理,第一行 @echo off 从不返回用户启动的初始批处理文件。

The Windows command processor cmd.exe executing the batch file continues batch file processing on its copy in temporary files directory with first line @echo off returning never to the initial batch file as started by the user.

现在,对临时文件目录中的初始批处理文件的副本执行批处理文件处理,并且当前目录也是临时文件的目录,批处理文件中的其他命令可以在初始当前状态下执行所有操作目录分别是批处理文件的初始执行目录,例如更新这些目录中的文件,甚至临时或永久删除这些目录。

Now with batch file processing done on a copy of initial batch file in temporary files directory and with current directory being also the directory for temporary files, the other commands in batch file can do everything in initial current directory respectively initial execution directory of the batch file like updating the files in these directories or even deleting these directories temporarily or permanently.

使用命令<$ c $的三个注释行c> rem 和接下来的三行仅演示了此处可以执行的操作以及在初始执行时如何使用批处理文件设置的环境变量。在最初存储在临时文件目录中并由用户从该目录执行的批处理文件中,这两个环境变量不存在(很可能)。

The three comment lines with command rem and the next three lines just demonstrate what can be done here and how to use the environment variables set by the batch file on initial execution. The two environment variables do not exist (most likely) on batch file being initially stored in directory for temporary files and executed by the user from this directory.

批处理文件将删除环境变量 InitialExecution 独立于其存在,以在从命令提示符窗口执行批处理文件的情况下恢复初始环境。

The batch file deletes the environment variable InitialExecution independent on its existence to restore initial environment in case of batch file executed from within a command prompt window.

最后,最初不在临时文件目录中执行批处理文件,它还会删除环境变量 CurrentDirectory ,如果该目录仍然存在,则将当前目录更改回初始当前目录,并从目录中删除自己以获取临时文件。

Finally with batch file initially not executed from temporary files directory it deletes also the environment variable CurrentDirectory, changes the current directory back to initial current directory, if that directory still exists, and deletes itself from directory for temporary files.

(goto)2> nul&我删除了Dave Benham在,Windows命令处理器没有输出任何错误消息。 20333575/3074564>如何使批处理文件自行删除?

(goto) 2>nul & del "%~f0" for batch file deletion without any error message output by Windows command processor was copied by me from Dave Benham's answer on How to make a batch file delete itself?

要了解所使用的命令及其工作方式,请打开命令提示符窗口,执行那里有以下命令,并非常仔细地阅读了每个命令显示的所有帮助页面。

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.


  • call /? ...说明%〜dp0 %〜f0 %〜nx0

  • cd /?

  • copy /?

  • del /?

  • echo /?

  • goto /?

  • 如果/?

  • 暂停/?

  • set /?

  • call /? ... explains %~dp0, %~f0 and %~nx0
  • cd /?
  • copy /?
  • del /?
  • echo /?
  • goto /?
  • if /?
  • pause /?
  • set /?

请参阅也是有关使用命令重定向运算符

这篇关于我正在尝试使用Robocopy替换cmd文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆