Buscar..


Sintaxis

  • [comando] [[> | >> | <| 2> | 2 >>] archivo]
  • [[> | >> | <| 2> | 2 >>] archivo] [comando]

Parámetros

Parámetro Detalles
mando Cualquier comando válido.
> Escriba STDOUT en el archivo.
>> Agregue STDOUT al archivo.
< Lea el archivo a STDIN .
2> Escribe STDERR al archivo.
2>> STDERR al archivo.
expediente La ruta a un archivo.

Observaciones

  • Puede agregar tantas redirecciones diferentes como desee, siempre que el símbolo y el archivo de la redirección permanezcan juntos y en el orden correcto.

Un ejemplo...

@echo off
setlocal
set /p "_myvar=what is your name?"
echo HELLO!>file.txt
echo %_myvar%!>>file.txt
echo done!
pause
type file.txt
endlocal
exit

Ahora file.txt se ve como:

HELLO!
John Smith!

(Suponiendo que escribiste John Smith como tu nombre).

Ahora la consola de su archivo por lotes se ve así:

what is your name?John Smith
done!
Press any key to continue...
HELLO!
John Smith!

(y debería salir tan rápido que es posible que no pueda ver nada después de la solicitud. Press any key to coninue... )

Redirigir el carácter especial con la expansión retrasada habilitada

¡Este ejemplo hace eco del carácter especial ! en un archivo. Esto solo funcionaría cuando DelayedExpansion esté deshabilitado. Cuando la expansión retrasada está habilitada, deberá utilizar tres caracteres y un signo de exclamación como este:

@echo off
setlocal enabledelayedexpansion


echo ^^^!>file
echo ^>>>file

goto :eof    

    ^> is the text
    >> is the redirect operator

pause
endlocal
exit /b

Este código hará eco del siguiente texto en el archivo.

!
>

como

^^^ escapes the ! and echos it into the file
^> escapes the > and echos it into the file

Escribir en un archivo

@echo off
cls
echo Please input the file path, surrounded by "double quotation marks" if necessary.
REM If you don't want to redirect, escape the > by preceding it with ^
set /p filepath=^> 

echo Writing a random number
echo %RANDOM% > %filepath%
echo Reading the random number
type %filepath%

REM Successive file writes will overwrite the previous file contents
echo Writing the current directory tree:
> %filepath% tree /A
echo Reading the file
type %filepath%

REM nul is a special file. It is always empty, no matter what you write to it.
echo Writing to nul
type %windir%\win.ini > nul
echo Reading from nul
type nul

echo Writing nul's contents to the file
type nul > %filepath%
echo Reading the file
type %filepath%


Modified text is an extract of the original Stack Overflow Documentation
Licenciado bajo CC BY-SA 3.0
No afiliado a Stack Overflow