Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch file flashing command prompt in endless loop after being executed

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.

like image 805
kolcinx Avatar asked Aug 10 '17 19:08

kolcinx


People also ask

How do I get out of Infinite loop in cmd?

To stop this infinite loop, press Ctrl + C and then press y and then Enter.

Why is my batch file looping?

The cause of this behaviour is the order of execution of the commands. The command you want to execute is in one of the folders in your path. But the batch file is in your current folder so it gets executed first, causing the loop.

How do you stop a command loop?

You can place a break command only in the body of a looping command or in the body of a switch command. The break keyword must be lowercase and cannot be abbreviated. break ; In a looping statement, the break command ends the loop and moves control to the next command outside the loop.

How do I keep cmd from running after a batch file?

If you want the command prompt cmd widnow to stay open after executing the last command in batch file –you should write cmd /k command at the end of your batch file. This command will prevent the command prompt window from closing and you'll get the prompt back for giving more commands in the cmd window.


1 Answers

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.

like image 94
kolcinx Avatar answered Oct 10 '22 22:10

kolcinx