Citar
La utilidad de mi descubrimiento es que por ejemplo si antes declaraban:
Código:
y querían mostrar el token 1,2,3,4 solo podíamos mostrar el token 1 y 2, porque luego de %%Y viene %%Z y después ... no sabíamos que letra venía.
Entonces ahora podemos hacer:
Código:
Código:
Código:
for /f "tokens=1-4" %%Y in ("p1 p2 p3 p4") do (
echo %%Y%%Z...
)
y querían mostrar el token 1,2,3,4 solo podíamos mostrar el token 1 y 2, porque luego de %%Y viene %%Z y después ... no sabíamos que letra venía.
Entonces ahora podemos hacer:
Código:
Código:
for /f "tokens=1-4" %%Y in ("p1 p2 p3 p4") do (
echo %%Y%%Z%%[%%\
)
He descubierto que tambien podemos usar numeros en la variable del for, de la siguiente manera.
Código:
@echo off
for %%^1 in (
hola,
adios,
si,
no,
Ok,
bye
) do (
echo %%^1
)
Código:
for /l %%^6 in (1 1 10) do (
echo %%^6
)
Código:
for /d /r %%^2 in (*) do (
echo %%^2
)
Código:
for /f "tokens=1,2,3" %%^0 in ('ver') do (
echo.%%^0 %%^1 %%^2
)
Código:
for %%^1 in ("%~nx0") do (
echo %%~nxt^1
)
Código:
for /f "tokens=1" %%^7 in ('ver') do (
echo.%%^7
)
Rob Van der Woude :
Citar
Hi Leonardo,
Amazing! Yet another new feature discovered in CMD, after all these years...
I added your tip on my Clever Tricks page (http://www.robvanderwoude.com/clevertricks.php#FORListNumbers) with a link from the Program Flow section of my Batch Techniques page (http://www.robvanderwoude.com/battech.php#Flow).
Thanks again,
Rob
Amazing! Yet another new feature discovered in CMD, after all these years...
I added your tip on my Clever Tricks page (http://www.robvanderwoude.com/clevertricks.php#FORListNumbers) with a link from the Program Flow section of my Batch Techniques page (http://www.robvanderwoude.com/battech.php#Flow).
Thanks again,
Rob
Citar
Leonardo Gutierrez Ramirez also found a way to use numbers as variables in a FOR loop:
Hi, I have discovered that we can use numbers in the variable FOR command, like this:
FOR /L %%ˆ6 IN (1 1 10) DO (
ECHO %%ˆ6
)
FOR /D /R %%ˆ2 IN (*) DO (
ECHO %%ˆ2
)
FOR /F "tokens=1,2,3" %%ˆ0 IN ('VER') DO (
ECHO.%%ˆ0 %%ˆ1 %%ˆ2
)
FOR %%ˆ1 IN ("%~nx0") DO (
ECHO %%~nxtˆ1
)
FOR /F "tokens=1" %%ˆ7 IN ('VER') DO (
ECHO.%%ˆ7
)
FOR %%ˆ1 IN ("%~nx0") DO (
ECHO %%~nxatdˆ1
)
Combined with Carlos' extension of the available variables in FOR loops, we can now, in theory at least, nest up to 75 FOR loops!
Not that I would want to maintain such code...
Thanks Leo
Hi, I have discovered that we can use numbers in the variable FOR command, like this:
FOR /L %%ˆ6 IN (1 1 10) DO (
ECHO %%ˆ6
)
FOR /D /R %%ˆ2 IN (*) DO (
ECHO %%ˆ2
)
FOR /F "tokens=1,2,3" %%ˆ0 IN ('VER') DO (
ECHO.%%ˆ0 %%ˆ1 %%ˆ2
)
FOR %%ˆ1 IN ("%~nx0") DO (
ECHO %%~nxtˆ1
)
FOR /F "tokens=1" %%ˆ7 IN ('VER') DO (
ECHO.%%ˆ7
)
FOR %%ˆ1 IN ("%~nx0") DO (
ECHO %%~nxatdˆ1
)
Combined with Carlos' extension of the available variables in FOR loops, we can now, in theory at least, nest up to 75 FOR loops!
Not that I would want to maintain such code...
Thanks Leo
http://www.robvanderwoude.com/clevertricks.php#FORListNumbers
http://www.robvanderwoude.com/battech.php#Flow
Saludos.