简体   繁体   English

执行后无限循环中的批处理文件闪烁命令提示符

[英]Batch file flashing command prompt in endless loop after being executed

Background 背景

I want to use ROBOCOPY to backup folders. 我想使用ROBOCOPY来备份文件夹。 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). 我能够从命令行和PowerShell(使用Windows 10)ROBOCOPY源文件夹。

ROBOCOPY "RoboCopy_Files" "RoboCopy_Files_Testing" /MIR

It does exactly what I want. 它完全符合我的要求。
Now I put the command into batch file Robocopy.cmd . 现在我将命令放入批处理文件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. 但是,当我将相同的命令放入Robocopy.cmd文件时 ,在根F:\\ RoboCopy文件夹中运行它,我只得到闪烁的cmd窗口 ,我的命令在越来越多的行上重复。

How can I put the command into a CMD file (eg Robocopy.cmd) for later use/share/schedule? 如何将命令放入CMD文件(例如Robocopy.cmd)以供以后使用/共享/计划? 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. 注意:我觉得这更像是学习如何将cmd脚本放入文件,而不是如何使用ROBOCOPY。

Cause: File and Command have the same name 原因:文件和命令具有相同的名称

I wanted to use the ROBOCOPY command inside the ROBOCOPY file. 我想在ROBOCOPY文件中使用ROBOCOPY命令。 This is a mistake. 这是个错误。
The file ends up calling itself. 该文件最终调用自己。

Solution 1: Rename batch file 解决方案1:重命名批处理文件

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 解决方案2:使用更明确的命令调用

Another solution could be to use ROBOCOPY.exe or explicitly specify the full path to the exe like C:...\\robocopy.exe . 另一个解决方案可能是使用ROBOCOPY.exe或显式指定exe的完整路径,如C:... \\ robocopy.exe This would prevent the "confusion" in calling the command vs calling the batch file itself. 这可以防止调用命令与调用批处理文件本身的“混淆”。

Solution 1 x 2 解决方案1 ​​x 2

The best solution (thx Mofi again) is to combine the 1 x 2 together. 最好的解决方案(再次是Mofi)是将1 x 2组合在一起。 Use unique batch file name AND specify the full path to command (exe) inside the batch file. 使用唯一的批处理文件名并在批处理文件中指定命令(exe)的完整路径。

Useful related commands: To determine the full path to command (exe), see the WHERE command (eg Where Robocopy.exe - This should be somewhere in windows folder.), system variables (eg SS64 ), or the command SET . 有用的相关命令:要确定命令(exe)的完整路径,请参阅WHERE命令(例如,Robocopy.exe - 这应该位于Windows文件夹中的某个位置。),系统变量(例如SS64 )或命令SET

The full version in my case would be to run for example BackupRobocopyFiles.cmd with line: 在我的情况下,完整版将运行例如BackupRobocopyFiles.cmd与行:

%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. 注意:仅当cmd文件位于根文件夹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. 如果我想从不同的文件夹(或任务sheduler)运行cmd,我将指定ROBOCOPY命令的SOURCE_FOLDER和DESTINATION_FOLDER参数的完整路径。

Contributions 捐款

The answer was found based on comments by: "MC ND", "eryksun". 根据以下评论找到答案:“MC ND”,“eryksun”。 "Mofi" did point out, that the original Q was not helpful. “Mofi”确实指出,原来的Q没有帮助。 Thanks to all. 谢谢大家。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM