%errorlevel% 在循环命令中返回 0 [英] %errorlevel% returning 0 in loop command

查看:45
本文介绍了%errorlevel% 在循环命令中返回 0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我已经安装了 Dropbox,但它与我要找的东西不符,所以我用 Revo Pro 卸载了它.但是,当我打开任务管理器时,我的计算机中仍然有与其相关的进程在运行,所以我决定批量查看并删除与之相关的所有文件.

Ok, I've installed Dropbox but it didn't corresponded to what I was looking for so I uninstalled it with Revo Pro. But, when i open the taskmanager there are still processes related to it running in my computer so I decided to make a batch to look out and delete all files that are related to it.

    @echo off
    cd c:\
    :a
    set /p a=Phrase that might be realted to it 

    for /r %%d IN (*.*) DO (
        (
        findstr /i /m /c:%a% "%%d"
        if "%errorlevel%"=="0" del "%%d"
        echo %errorlevel%
        )
    )

    pause

问题是:当我使用循环运行 findstr 时,即使我的变量%a%"在分析文件中没有匹配,%errorlevel% 返回为 0.但是当我单独使用 findstr 并且没有match %ERRORLEVEL% 为匹配返回 1 和 0.如果我使用它,我会删除我所有的PC文件哈哈.代码有什么问题?

The problem is: when I run findstr using loop even when there is no match for my variable "%a%" in an analized file %errorlevel% returns as 0. But when I use findstr alone and there isn't a match %ERRORLEVEL% returns as 1 and 0 for a match. If I use it, I'll delete all my PC files haha. What's wrong with the code?

推荐答案

在括号中的一系列语句中,任何 %var% 都被替换为控制该变量的动词时的值遇到语句序列(或 block).

Within a parenthesised series of statements, any %var% is replaced by the value of that variable at the time the verb controlling that statement-sequence (or block) is encountered.

这里,块是由 for 控制的整个语句序列.%errorlevel% 在遇到 for 时被 errorlevel 的状态替换,所以可能是 0.

Here, the block is the entire sequence of statements controlled by the for. %errorlevel% is replaced by the status of errorlevel at the time the for is encountered, so probably 0.

如果你使用

    findstr /i /m /c:%a% "%%d"
    if not errorlevel 1 del "%%d"
    echo %errorlevel%

然后使用 errorlevelrun-time 值(即,当它通过循环操作发生变化时)并且命令的意思是如果 errorlevel 不是(1 或大于 1)这样做..."

then the run-time value of errorlevel is used (ie. as it changes through the operation of the loop) and the command means "if errorlevel is not (1 or greater than 1) do this..."

findstr 将在 found 上设置 errorlevel 为 0,在 not found 上设置 1code> 和 2 用于 file not found(IIRC) 所以 NOT(1 或大于 1)只选择 0.请注意,在某些深奥的情况下,errorlevel 可能会变为负数,但在 findstr 之后,我相信 0..2 是允许的范围.

The findstr will set errorlevel to 0 on found, 1 on not found and 2 for file not found(IIRC) so NOT (1 or greater than 1) selects 0 only. Note that in certain esoteric circumstances, errorlevel may become negative, but after a findstr I believe 0..2 is the allowed range.

这篇关于%errorlevel% 在循环命令中返回 0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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