带有goto命令的Windows批处理文件不起作用 [英] windows batch file with goto command not working

查看:56
本文介绍了带有goto命令的Windows批处理文件不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 GOTO 命令和附属标签有疑问.

I have a problem with GOTO command and affiliated labels.

事实:给定文件夹中的一堆文件(它们是日志错误),我需要打开它们并检查它们是否包含特定字符串.如果是,则从文件名中删除一些字符(_"最后出现之后的所有字符,包括它自己)并执行其他操作.

Facts: Given a bunch of files from a folder (they are log errors) I need to open them and check if they contain a specific string. If yes then eliminate some characters (all the chars after the last appearance of "_", including itself) from the file names and do other operations.

为了切断字符,我以循环方式使用 GOTO 命令,因为我发现它在这里描述:http://www.robvanderwoude.com/battech_while_loops.php

For cutting off the chars I'm using GOTO command in a loop manner as I found it described here: http://www.robvanderwoude.com/battech_while_loops.php

脚本是:

@echo off
setlocal EnableDelayedExpansion

cls

for %%X in (D:e-puboutboxlogs*.*) do (

    for /F "tokens=7" %%S in (%%X) do (

        if /i "%%S"=="<ml>" (
            SET fisier=%%~nX
            SET cond=!fisier:~-1!
            SET fisier=!fisier:~0,-1!

            :loopStart
            rem condition to break the loop
            if !cond!==_ goto loopEnd
            SET cond=!fisier:~-1!
            SET fisier=!fisier:~0,-1!
            goto loopStart

            :loopEnd

            rem here it should be out of a loop
            rem other stuff to do with var !fisier!
            rem the following line is not executed because of the label loopEnd
            echo !fisier!
        )
    )
) 

pause

脚本没有运行,因为标签 loopEnd 后面有一个空行?!如果我在该标签之后编写任何指令,它们将被执行,但不会执行第一个 for 语句的其余迭代(日志错误文件夹包含多个文件)

The script is not running because there is an empty line after the label loopEnd?! If I'm writing any instructions right after that label they will be executed but the rest of iterations from the first for statement won't be executed (the log errors folder contains more one file)

有人可以提供帮助吗?

推荐答案

你有两个问题.

一个问题是 goto 会破坏 for 循环.另外,括号里的标签比较难.

One problem is that a goto breaks a for-loop. The other, labels are quite difficult in parenthesis.

goto 总是中断所有嵌套循环,即使 goto 的标签在同一个块中,并且 for 变量在跳转后立即丢失.

The goto breaks always and all nested loops, even if the label of the goto is in the same block, and the for-variables are lost immediately after the jump.

括号中的标签是面向两行"的!我对标签进行了实验,以下是括号中的一些结果.

In parenthesis lables are "two line" oriented! I experimented with labels and here are some results for parenthesis.

当出现标签时,下一行必须采用正确的辅助"行格式.

When a label occurs, the next line has to be in the correct format for a "secondary" line.

这就是失败的原因.

(
:this label fails with a syntax error
)

(
:this works
:because this line is a "legal" secondary line
)

(
:: The remark style
:: fails, because it's not "legal" to use a double colon, because it's not a legal path (in the most cases)
)

(
:and now I got courious & echo This will not echo'd
:but & echo You can see this !
)

对于第二行,批处理解析器的一些步骤被跳过.

For the second line some steps of the batch parser are skipped.

@ 不起作用,@echo Hello 尝试启动名为 @echo.bat 的文件.

@ doesn't work, @echo Hello tries to start a file named @echo.bat.

括号拆分失败,如 echo( hello.
标签作为文件名处理,:echo 仅检查 :echo 是否为有效文件名,然后跳过这部分.

Splitting of parenthesis fails, like in echo( hello.
Labels are handeled as a file name, :echo checks only if :echo is a valid file name and then skip this part.

::hello 搜索驱动器 ::.
出于测试目的,可以使用 subst :: c: emp 创建驱动器 ::.
由于标签在第二行被简单地忽略,与号和管道也可以工作,但 :: 上的文件必须存在.

::hello searches on the drive ::.
For test purposes the drive :: can be created with subst :: c: emp.
As labels are simply ignored on the second line, ampersands and also pipes work, but the file on :: have to exist.

(
echo @echo This is %~f0
) > %TEMP%	estLabel.bat

REM create Drive ::
subst :: %temp% 
(
:Label 
::	estLabel.bat The bat will not be executed | echo But this
)
subst /D ::

这篇关于带有goto命令的Windows批处理文件不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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