Q:

batch file for with if errorlevel errorlog

what i want is if user already exist just put the name of this user in errorlog.txt
i have this:
set var_b=123
    FOR /F %%v in (%arxiu%) do (
        net user %%v >nul 2>nul
        if errorlevel 1 ( net user %%v %var_b% /add >nul 2>nul
        )
        if errorlevel 0 ( echo %%v >> C:\Users\%username%\Desktop\errorlog.txt
        )
    )

but with this code i get the name of users that already exist and the one news that creates i dont know why any error ?

A:

See Microsoft support article Testing for a Specific Error Level in Batch Files.
if errorlevel X means if exit code of previous command is greater or equal X.
Use following batch code:
set "var_b=123"
for /F %%v in (%arxiu%) do (
    net user %%v >nul 2>nul
    if errorlevel 1 (
        net user %%v %var_b% /add >nul 2>nul
    ) else (
        echo %%v>>"%USERPROFILE%\Desktop\errorlog.txt"
    )
)

