¿Cómo puedo usar un archivo por lotes para escribir en un archivo de texto?


Necesito hacer un script que pueda escribir una línea de texto en un archivo de texto en el mismo directorio que el archivo por lotes.

Author: Mark Amery, 2013-11-09

7 answers

Puede utilizar echo, y redirigir la salida a un archivo de texto (ver notas a continuación):

rem Saved in D:\Temp\WriteText.bat
@echo off
@echo This is a test> test.txt
@echo 123>> test.txt
@echo 245.67>> test.txt

Salida:

D:\Temp>WriteText

D:\Temp>type test.txt
This is a test
123
245.67

D:\Temp>

Notas:

  • @echo off desactiva la impresión de cada comando en la consola
  • @ al principio de las líneas restantes deja de imprimirse el comando echo en sí, pero no suprime la salida echo. (Permite que se muestre el resto de la línea después de @echo.
  • A menos que le dé un nombre de ruta específico, redirección con > o >> escribirá en el directorio actual (el directorio en el que se ejecuta el código).
  • El @echo This is a test > test.txt usa un > para sobrescribir cualquier archivo que ya exista con contenido nuevo.
  • Las sentencias @echo restantes usan dos caracteres >> para agregar al archivo de texto (add to), en lugar de sobrescribirlo.
  • El type test.txt simplemente escribe la salida del archivo en la ventana de comandos.
 214
Author: Ken White,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-06-10 21:46:55

Es más fácil usar solo un bloque de código, entonces solo necesita una redirección.

(
  echo Line1
  echo Line2
  ...
  echo Last Line
) > filename.txt
 79
Author: jeb,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-04-14 19:24:05

echo "blahblah"> txt.txt borrará el txt y pondrá blahblah en su lugar

echo "blahblah">> txt.txt escribirá blahblah en una nueva línea en el txt

Creo que ambos crearán un nuevo txt si no existe ninguno (sé que el primero lo hace)

Donde "txt.txt" está escrito arriba, se puede insertar una ruta de archivo si se desea. por ejemplo, C:\Users\<username>\desktop, que lo pondrá en su escritorio.

 12
Author: Darth Tater,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-07-15 06:06:38
    @echo off

    (echo this is in the first line) > xy.txt
    (echo this is in the second line) >> xy.txt

    exit

Los dos >> significa que la segunda línea se agregará al archivo (es decir, la segunda línea comenzará después de la última línea de xy.txt).

Así es como se ve el xy.txt:

this is in the first line
this is in the second line
 10
Author: Organ,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-04-14 19:16:24

@ echo off Escritura de Títulos usando Archivos por lotes color 0a

Echo Texto de ejemplo > Nombre de archivo.txt echo Texto adicional > > Nombre de archivo.txt

@ECHO OFF
Title Writing Using Batch Files
color 0a

echo Example Text > Filename.txt
echo Additional Text >> Filename.txt
 3
Author: Ace,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-04-13 06:09:42
  • Puede usar copy con para escribir un texto largo
  • Ejemplo:

    C:\COPY CON [unidad:] [ruta] [Nombre de archivo]

    .... Contenido

    F6

    1 archivo(s) se copia

 1
Author: Pham Thanh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-05-09 09:05:37
@echo off

echo Type your text here.

:top

set /p boompanes=

pause

echo %boompanes%> practice.txt

Espero que esto ayude. debe cambiar los nombres de cadena (IDK como se llama) y el nombre del archivo

 1
Author: John Caleb Garon,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-02-08 14:53:55