Erg*_*gil 4 windows command goto file batch-file
我的GOTO命令和附属标签有问题.
事实:鉴于来自文件夹的一堆文件(它们是日志错误),我需要打开它们并检查它们是否包含特定的字符串.如果是,则从文件名中删除一些字符(最后一次出现"_"后的所有字符,包括其自身)并执行其他操作.
为了切断字符,我正在循环使用GOTO命令,如我在此处所述: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
Run Code Online (Sandbox Code Playgroud)
脚本没有运行,因为标签loopEnd后面有一个空行?!如果我正在写那个标签之后的任何指令,它们将被执行但是第一个for语句的其余迭代将不会被执行(log errors文件夹包含更多的一个文件)
有人可以提供帮助吗?
jeb*_*jeb 12
你有两个问题.
一个问题是goto打破了for循环.另外,标签在括号中相当困难.
即使goto的标签位于同一个块中,goto也会断开始终和所有嵌套循环,并且for-variables在跳转后立即丢失.
括号中的标签是"两线"导向的!我试验了标签,这里有一些括号的结果.
出现标签时,下一行必须采用正确的"辅助"行格式.
这就是失败的原因.
(
: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 !
)
Run Code Online (Sandbox Code Playgroud)
对于第二行,将跳过批处理解析器的一些步骤.
@不起作用,@echo Hello尝试启动一个名为的文件@echo.bat.
括号的拆分失败,就像在echo( hello.
标签作为文件名进行处理,:echo仅检查是否:echo为有效文件名,然后跳过此部分.
::hello搜索驱动器::.
出于测试目的,::可以使用创建驱动器subst :: c:\temp.
由于标签在第二行上被忽略,因此&符号和管道都可以工作,但文件::必须存在.
(
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 ::
Run Code Online (Sandbox Code Playgroud)