1

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?

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.

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

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

Matthieu
  • 2,736
  • 4
  • 57
  • 87

1 Answers1

3

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. 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.

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.

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.

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.

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 & 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 /? ... explains %~dp0, %~f0 and %~nx0
  • cd /?
  • copy /?
  • del /?
  • echo /?
  • goto /?
  • if /?
  • pause /?
  • set /?

See also the Microsoft article about Using command redirection operators.

Mofi
  • 46,139
  • 17
  • 80
  • 143