Why in the cycle is not assigned to the variable "tes" the value of the variable cycle - %% i ?

for /f "tokens=*" %%i in ('dir /B') do ( SET tes="%%i" // не присваивает echo %tes% // Ничего не выводит... ) 

And how can you generally change the variable %% i in a loop, like for example with the others:

 %var:~0,3% 

In general, here is the whole debugging code that does not work all for the same reason:

 cd C:\Users\%USERNAME%\Desktop color 0a chcp 65001 cls ECHO OFF for /f "tokens=*" %%i in ('dir /B') do ( SET tes="!i!" echo %tes% ) pause 
  • one
    Not sure if the brackets are relevant here (the closing one is lost anyway). Mb I wanted a deferred expansion !переменных! ? - karmadro4
  • Bracket added, thanks. That's just how to change %% i in a loop? - Free_man 7:27 pm
  • one
    @Dexter, I rarely use brackets, now I will try how they work with for ... - karmadro4
  • Ok, thanks for the encouragement. I hope this case will not end to no avail ... - Free_man

1 answer 1

All understood evil in brackets. Commands enclosed in brackets are still interpreted as one line. Need deferred extension.

 @ECHO OFF REM стыдоба.cmd SETLOCAL FOR /F "tokens=*" %%f IN ('dir /b') DO ( IF "%%f" == "Porno" ( COLOR c SET bacon="%%f" REM На самом деле присваивает SET bacon REM Но не подставляет ECHO !bacon! PAUSE ) ELSE ( ECHO %%f COLOR ) ) 

Command line interpreter d. started with the /V:ON switch

  • Yes, here, in this key, rather, in the key of V. How to enable it from already running cmd? - Free_man
  • I hope you can help me ... anyway, thank you. - Free_man
  • one
    @Dexter, SETLOCAL ENABLEDELAYEDEXPANSION - karmadro4
  • @ karmadro4, thanks a lot! But it turns out that there is no "evil" in the brackets. It is the setting of the / C parameter for the terminal that solves the problem. This is because this code works fine without the condition: FOR / F "tokens = *" %% i IN ('DIR / b') DO (SET sp = "%% i" echo! Sp!) - Free_man
  • one
    @Dexter, read again the second sentence, the essence of it. Let me remind you that the batch file is interpreted line by line . This task can be solved without compound commands, through subroutines. ( ... DO CALL :SUB %%i ) - karmadro4