运行脚本后命令窗口关闭,即使" PAUSE"在脚本中

时间:2015-10-17 01:16:31

标签: batch-file

运行批处理脚本后,如何防止命令窗口关闭。我想保持它的开放性,以便进行故障排除和测试。它在我输入ping之后关闭,如果是错误级别的部分。我的代码是否正确以及如何阻止它关闭?

@echo on

for /f %%a in (pclist.txt)  do ( 

ping -n 1 \\%%a > NUL
IF ERRORLEVEL 0 (goto :copyhost1) ELSE goto :skipcopyhost1

:copyhost1

ROBOCOPY "\\%%a\C$\folder" "\\servername\folder" /MOVE

ROBOCOPY "\\%%a\C$\folder2" "\\servername\folder2" /MOVE

ROBOCOPY "\\%%a\C$\folder3" "\\servername\folder3" /MOVE

:skipcopyhost1

)

PAUSE

以下是我所知道的工作:

@echo on

for /f %%a in (pclist.txt)  do ( 
ROBOCOPY "\\%%a\C$\folder" "\\servername\folder" /MOVE

ROBOCOPY "\\%%a\C$\folder2" "\\servername\folder2" /MOVE

ROBOCOPY "\\%%a\C$\folder3" "\\servername\folder3" /MOVE

)

PAUSE

3 个答案:

答案 0 :(得分:2)

可能所有问题的根源都是标签和public class HomeActivity extends Activity { TextView tv_battery; @Override public void onCreate(Bundle b) { super.onCreate(b); setContentView(R.layout.main); tv_battery = (TextView) findViewById(R.id.tv_battery); } @Override protected void onResume() { super.onResume(); registerReceiver(mBatteryReceiver, new IntentFilter(Intent.ACTION_BATTERY_LOW )); } private BroadcastReceiver mBatteryReceiver = new BroadcastReceiver(){ @Override public void onReceive(Context ctxt, Intent intent) { int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0); tv_battery.setText("Remaining Battery : "+String.valueOf(level) + "%"); } }; } 命令。

  • 不应该在代码块内定义标签(括号内的代码)。通常这些标签会导致解析错误。

  • goto循环内,使用for命令会取消goto,并且不再迭代任何值。

使用for构造时也存在问题。对于任何大于或等于if errorlevel n的错误级别值,此条件的计算结果为true。这使得除了负值之外的所有错误级别值都会将条件评估为真。

n命令也是问题的根源(有关更多信息,请阅读here)。检查机器是否在线的常用方法是使用ping命令测试输出中是否存在TTL=字符串。如果找到了字符串(errorlevel = 0),则ping成功,如果找不到(errorlevel = 1),则机器处于脱机状态。

find

答案 1 :(得分:1)

首先,我几乎是批处理脚本的新手,但我会指出我用原始代码注意到的一些事情。

@echo on <- Isn't needed as echo is enabled by default, this will only tell you that echo is enabled.

for /f %%a in (pclist.txt)  do ( <- I imagine that pclist.txt has a list of IP's, fine and dandy, we'll work with that.

ping -n 1 \\%%a > NUL <- The \\ isn't needed >NUL isn't required for my corrected proposed solution.
IF ERRORLEVEL 0 THEN goto :copyhost1 ELSE goto :skipcopyhost1 <- Ping, not really setup to handle ERRORLEVEL.

未来提示,IF%ERRORLEVEL%== 0 GOTO blah

:skipcopyhost1 <- Next couple lines, shouldn't be in the loop
echo test
:copyhost1
echo test1

) <- Should be moved up a few lines

PAUSE

现在,即使您的pclist.txt具有混合的主动/非主动主机,此解决方案仍然有效,这将持续到文件结束为止。正如我所提到的,PING并没有真正设置来处理ERRORLEVEL,因此我们将命令传递给FINDSTR以搜索仅​​在活动主机中找到的文本,这也将禁止ping对话框。找到主机后,将ip设置为%Host%,这就是您必须为robocopy引用它的方式。除了摆脱%% a之外,我没有使用robocopy命令。无论如何,希望这是你想要的。

@ECHO OFF

FOR /F %%a in (pclist.txt)  DO (
CLS
TITLE Pinging %%a
PING -n 1 %%a | FINDSTR TTL && SET Host=%%a && CALL :CopyHost
)
EXIT

:CopyHost
TITLE Copying files to: %Host%
ROBOCOPY "\\%Host%\C$\folder" "\\servername\folder" /MOVE
ROBOCOPY "\\%Host%\C$\folder2" "\\servername\folder2" /MOVE
ROBOCOPY "\\%Host%\C$\folder3" "\\servername\folder3" /MOVE
GOTO :EOF

答案 2 :(得分:0)

你确定robocopy没有失败吗?

试试这个:

@echo on

for /f %%a in (pclist.txt)  do ( 

ping -n 1 \\%%a > NUL
IF ERRORLEVEL 0 THEN goto :copyhost1 ELSE goto :skipcopyhost1

:skipcopyhost1
echo test
:copyhost1
echo test1

)

PAUSE

暂停吗?如果是,请添加第一个robocopy命令,保存文件并再次运行。如果成功,请添加下一个,依此类推。上面的批处理命令在Windows 10 32位上运行正常,我希望它也适用于你。