蝙蝠文件和标签 [英] Bat file and labels

查看:192
本文介绍了蝙蝠文件和标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图为网络策略编写一个bat文件,如果它不存在以及其他一些功能,它将安装一个程序。我使用GOTO语句,取决于是否满足某些标准。但是,标签似乎并没有正确触发。

I am trying to write a bat file for a network policy that will install a program if it doesn't exist as well as several other functions. I am using GOTO statements depending on whether or not certain criterion are met. However, it seems that the labels are not firing correctly as all of them do.

我已经简化了脚本,以便掌握一些可能发生的事情。

I have simplified my script so as to grasp some idea of what may be happening.

@echo off


IF EXIST c:\test\test.txt (GOTO :EXISTING) ELSE GOTO :MISSING

:EXISTING
echo file exists

:MISSING
echo file missing

ping localhost -n 5 >NUL

基本上,它会检查文件test。 txt存在于文件夹c:\test中。所以它应该echo文件存在到控制台。但是,文件存在和文件丢失都回显到控制台。我发现如果我从文件夹中删除文件或只是重命名它,它只回显文件丢失

Basically it checks to see that the file "test.txt" exists in folder "c:\test" which id does. So it should echo file exists to the console. However, both "file exists" and "file missing" are echoed to the console. I find that if I remove the file from the folder or simply rename it, it only echoes "file missing"

为什么它运行两个标签?

Why is it running running both labels?

推荐答案

因为GOTO只是执行到脚本中的一个点的跳转,所以从那个点开始执行。如果你想让它在运行EXISTING之后停止,那么你需要这样做。注意额外的GOTO和新标签:

Because a GOTO is just a jump in execution to a point in the script, then execution continues sequentially from that point. If you want it to stop after running 'EXISTING', then you need to do something like this. Note the extra GOTO and new label:

@ECHO OFF
IF EXIST c:\test\test.txt (GOTO :EXISTING) ELSE GOTO :MISSING

:EXISTING
echo file exists
goto :NEXTBIT

:MISSING
echo file missing

:NEXTBIT
ping localhost -n 5 >NUL



<值得注意的是,使用cmd.exe(即基于NT的命令shell [NT,Win2k,XP,etc]),你可以做IF ... ELSE块如下:

It's worth noting though that with cmd.exe (i.e., the NT-based command shells [NT, Win2k, XP, etc]), you can do IF...ELSE blocks like this:

@ECHO OFF
IF EXIST c:\test\test.txt (
    ECHO File exists
) ELSE (
    ECHO File missing
)
ping localhost -n 5 >nul


$ b b

...所以你可以完全消除你的GOTO。

...so you can eliminate your GOTOs entirely.

这篇关于蝙蝠文件和标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆