Batch file command line in an endless loop after execution

Background

I want to use ROBOCOPY to backup folders. To find out about this, I created a test source folder containing other subfolders and dummy files.

F:\RoboCopy\RoboCopy_Files 

I can ROBOCOPY source folder from the command line and PowerShell (using Windows 10).

 ROBOCOPY "RoboCopy_Files" "RoboCopy_Files_Testing" /MIR 

He does exactly what I want.
Now I put the command in the batch file Robocopy.cmd .

Symptoms of a Problem

However, when I put the same command in the Robocopy.cmd file , in the root folder F: \ RoboCopy and run it, I get only a blinking cmd window with my command repeating on an increasing number of lines.

How can I put a command in a CMD file (e.g. Robocopy.cmd) for future use / sharing / schedule? How to prevent command line scrolling in an infinite loop without executing a command?

Note. I feel this is more like putting cmd scripts in files than using ROBOCOPY.

+5
source share
1 answer

Reason: File and command have the same name.

I wanted to use the ROBOCOPY command inside the ROBOCOPY file. This is mistake.
The file ultimately calls itself.

Solution 1. Rename the batch file.

One solution is to rename the batch file other than the command (s) in the file.

Solution 2: use a more explicit command call

Another solution might be to use ROBOCOPY.exe or explicitly specify the full path to exe, for example C: ... \ robocopy.exe. This will prevent “confusion” when invoking a command and invoking the command file itself.

1 x 2 solution

The best solution (thanks Mofi again) is to combine 1 x 2 together. Use a unique batch file name and specify the full path to the command (exe) inside the batch file.

Useful related commands: To determine the full path to the command (exe), see the WHERE command (for example, where Robocopy.exe should be somewhere in the Windows folder.), System variables (for example, SS64 ), or the SET command.

The full version in my case will be launched, for example, BackupRobocopyFiles.cmd with a line:

 %SystemRoot%\System32\robocopy.exe "RoboCopy_Files" "RoboCopy_Files_Testing" /MIR /R:2 

Note. This will only work if the cmd file is in the root folder F: \ RoboCopy. If I would like to run cmd from another folder (or sheduler), I would indicate the full path to the SOURCE_FOLDER and DESTINATION_FOLDER parameters of the ROBOCOPY command.

Contribution

The answer was found based on the comments: "MC ND", "eryksun". Mofi noted that the original Q did not help. Thanks to everyone.

+3
source

Source: https://habr.com/ru/post/1270761/


All Articles