5

Background

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

F:\RoboCopy\RoboCopy_Files

I am able to ROBOCOPY the source folder from the Command line and PowerShell (with using Windows 10).

ROBOCOPY "RoboCopy_Files" "RoboCopy_Files_Testing" /MIR

It does exactly what I want.
Now I put the command into batch file Robocopy.cmd.

Problem symptoms

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

How can I put the command into a CMD file (e.g. Robocopy.cmd) for later use/share/schedule? How to prevent command prompt from flashing in endless loop without running the command?

Note: I feel this is more about learning how to put cmd scripts into files, than how to use ROBOCOPY.

Mofi
  • 46,139
  • 17
  • 80
  • 143
kolcinx
  • 2,183
  • 1
  • 15
  • 38
  • 5
    If your batch file is called `robocopy.cmd` then your `robocopy` command inside the batch file will call the batch file itself. Change the name of the batch file or use `robocopy.exe` inside the batch file to avoid it calling itself – MC ND Aug 10 '17 at 20:28
  • The script should have a unique name. However, this doesn't have to be a problem. If it's picking up robocopy.cmd from the working directory, you can remove this directory from the implicit search path. Define the environment variable `NoDefaultCurrentDirectoryInExePath`. You can still search the working directory by adding "." to `PATH`, but after system directories. – Eryk Sun Aug 11 '17 at 02:56
  • @MCND Your comment is correct. After changing the batch file name the script works as expected. Thank you. Will accept your answer, if you make one. – kolcinx Aug 11 '17 at 04:45
  • @Mofi You are right about the helpfulness of current question. However, I could rewrite the Q to better describe what is happening (just the symptom). The point is the cmd is calling itself, because I used the same name for the file and for the command. However unlikely, this can happen to cmd newbies (like myself) again. The new title for the Q can be: Command prompt flashing in endless loop -> A: try to use different name of file or change the way the command is called, because it is calling itself. – kolcinx Aug 11 '17 at 06:22

1 Answers1

4

Cause: File and Command have the same name

I wanted to use the ROBOCOPY command inside the ROBOCOPY file. This is a mistake.
The file ends up calling itself.

Solution 1: Rename batch file

One solution is to rename the batch file to be different from the command(s) in the file.

Solution 2: Use more explicit command call

Another solution could be to use ROBOCOPY.exe or explicitly specify the full path to the exe like C:...\robocopy.exe. This would prevent the "confusion" in calling the command vs calling the batch file itself.

Solution 1 x 2

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

Useful related commands: To determine the full path to command (exe), see the WHERE command (e.g. Where Robocopy.exe - This should be somewhere in windows folder.), system variables (e.g. SS64), or the command SET.

The full version in my case would be to run for example BackupRobocopyFiles.cmd with line:

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

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

Contributions

The answer was found based on comments by: "MC ND", "eryksun". "Mofi" did point out, that the original Q was not helpful. Thanks to all.

Community
  • 1
  • 1
kolcinx
  • 2,183
  • 1
  • 15
  • 38
  • 1
    I recommend always to call in a batch file other batch files or executables like `robocopy` with file extension and full path if this is possible to avoid problems like that. Another quite often seen problem is that something is installed which adds its directory to system environment variable __PATH__ at beginning instead of its end containing by chance executables or scripts with same name as executables in `%SystemRoot%\System32`. The result is batch files containing just `findstr` or `robocopy` suddenly do not work anymore working before fine since years. Solution 1+2 should be used here. – Mofi Aug 14 '17 at 13:22
  • @Mofi Thank you mate. You helped (not only) me to better understand what is going on. Updated the answer based on comment. – kolcinx Aug 15 '17 at 18:37