findstr在块内返回错误的错误代码

时间:2019-06-06 19:41:49

标签: windows batch-file cmd findstr

执行循环时,即使语句为true,!errorlevel!引用的错误级别也将返回1而不是0。 setlocal enabledelayedexpansion已启用

我尝试重新排列IF语句,尝试使用GOTO循环将错误检查放在功能块之外。尝试过%ERRORLEVEL%!ERRORLEVEL!

此循环的目的是检查文本文件中的用户名列表,如果文件中存在用户名,则将该用户添加到localadmins组。

rem == Count number of users in backup, create variable of user{num} for each username ==

set /p s=<"%custompath%\%uname%\users.txt"
set s=%s:~0,-1%
set /a counter=0
for %%a in (%s:,= %) do (
    set /a counter+=1 
    set user!counter!=%%a
    )

set /a count = 0

:CUSTOM_LOOP_ADMIN

rem == Adds user to Admin group on new computer if they were previously in local Administrators ==

if %count% LEQ %counter% (
    set /a count +=1
    IF NOT [!user%count%!]==[] (
        findstr /m "!user%count%!" "%custompath%\%uname%\Localadmins.txt" > nul
        IF !ERRORLEVEL!==0 (
            net localgroup Administrators NET\!user%count%! /add > nul 2> nul
            net localgroup Administrators !user%count%! /add > nul 2> nul
            echo.
            echo Added !user%count%! to local Administrators group
        )
    )

    goto :CUSTOM_LOOP_ADMIN )


GOTO :EOF

如果用户名位于文本文件中,则预期结果是!ERRORLEVEL!等于0,如果不是,则返回非0并继续循环。

编辑:

我是个白痴...

findstr默认情况下区分大小写。通过使用findstr /m /i "sometext" "sample.txt" > nul

解决

0 个答案:

没有答案