与转到命令窗口批处理文件无法正常工作 [英] windows batch file with goto command not working

查看:143
本文介绍了与转到命令窗口批处理文件无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有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-pub\outbox\logs\*.*) 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.

在转到休息总是和所有嵌套的循环,即使转到的标签是相同的块,而换变量跳后立即消失。

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.

这就是为什么这个失败。

That's why this fails.

(
: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你好试图启动一个名为 @ echo.bat

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

括号分割失败,就像回声(你好。结果
标签handeled作为文件名,:回声只检查是否:回声是一个有效的文件名,然后跳过此部分。

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.

::你好驱动器 ::上搜索。结果
为了测试驱动器可以创建 SUBST :: C:\\ TEMP 结果
由于标签只是在第二行被忽略,与符号,也管工作,但在文件必须存在。

::hello searches on the drive ::.
For test purposes the drive :: can be created with subst :: c:\temp.
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%\testLabel.bat

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

这篇关于与转到命令窗口批处理文件无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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