在文本文件Windows Batch中添加新行

时间:2014-04-12 02:45:41

标签: batch-file

我有一个“信息卡”脚本,我用它来记录FRC比赛中各个团队的小笔记,我想知道是否有一种简单的方法可以在文本文件中添加新行。 我目前设置它,以便它为文本文件添加标题,然后允许您通过应用程序输入内容。但是,无论文本片段有多长,内容都会显示在文本文件的第一行。

这就是我所拥有的:

echo off
color 0a
title !@!
cls
:start
cls
echo This project was created by Liam Powell.
echo ---------------------------------------------------------------------------
echo 1) create new card
echo 2) open current card
echo 3) check current files.
echo 4) clear all files.
set input=
set /p input=
if %input%==1 goto cardcreator if NOT goto start
if %input%==2 goto open if NOT goto start
if %input%==3 goto check if NOT goto start
if %input%==4 goto clearall if NOT goto start
cls
:open
cls
echo enter the card name.
set input=
set /p input=
start C:\Users\Powell\Desktop\Batch-files\Card\cards\%input%.txt
pause
goto start
:cardcreator
cls
echo what do you want the card to be called?
set name=
set /p name=
echo what should the contents be?
set content=
set /p content=
echo %content% >C:\Users\Powell\Desktop\Batch-files\Card\cards\%name%.txt
pause
goto start
:check
cls
dir C:\Users\Powell\Desktop\Batch-files\Card\cards\  /b /o:n
pause
goto start
:clearall
cls
echo Delete all Cards?
set input=
set /p input=
if %input%==yes goto clearalltwo if NOT goto start
if %input%==Yes goto clearalltwo if NOT goto start
if %input%==no goto start 
if %input%==No goto start
:clearalltwo
cls
cd "C:\Users\Powell\Desktop\Batch-files\Card\cards\"
del *.txt
pause
goto start

我的问题是,我如何制作程序,以便我输入的文本在文本文件中添加多行,而不是将所有输入的文本都换成一行? 基本上,区别:

The quick brown fox jumps over the lazy dog. And enjoyed it.

Vs的

The quick brown fox jumps over the lazy dog.
And enjoyed it.

5 个答案:

答案 0 :(得分:0)

这是你的意思是设置一个新行的变量吗? :

@echo off
set nl=^& echo.     
echo line1%nl%line2

答案 1 :(得分:0)

copy con filename.txt

con是键盘/屏幕。按Ctrl + Z即可停止输入文本。然后输入的所有行都将复制到文件中。

复制二进制和ascii有两种类型。复制选择聪明地使用哪一个,但你可以控制它 - 请参阅copy /?。二进制用于已知大小的文件等。 ASCII用于串行端口和大小未知的键盘等流。文件结束,从而复制结束,是遇到Ctrl + Z字符(char number 26)。

如果要添加到文件中。

copy filename.txt+con filename.txt

加(+)连接文件。

答案 2 :(得分:0)

哇 - 这么多代码 - 但遗憾的是,没有明确说明需要什么。

echo what should the contents be?
:addmore
set "content="
set /p content=
if not defined content pause&goto start
echo %content% >>C:\Users\Powell\Desktop\Batch-files\Card\cards\%name%.txt
echo More content?
goto addmore

这应该允许您添加多行。请注意,>>会附加到现有文件或创建新文件(如果它尚未存在)。 >只是创建一个新文件。

使用set语法最好使用set "var=whatever"字符串,以避免在分配的值中包含尾随空格。

请注意用于级联指令的instruction&instruction语法。

实际上,它打败了我为什么不使用

start C:\Users\Powell\Desktop\Batch-files\Card\cards\%name%.txt

应打开文本编辑器以创建文件。

答案 3 :(得分:0)

以下部分获取用户在“内容”下输入的内容,并按照您的要求在文本文件中为每25个字符添加一个新行:

echo what do you want the card to be called?
set name=
set /p name=
echo what should the contents be?
set content=
set /p content=

rem Split the content in parts 25 characters each and add they as separated lines
:next25chars
echo %content:~0,25% >> C:\Users\Powell\Desktop\Batch-files\Card\cards\%name%.txt
set content=%content:~25%
if defined content goto next25chars

pause

答案 4 :(得分:0)

在我看来,您的问题是如何在以下行中将更多文本附加到文件中。

单个>创建或覆盖文件,两个>>将创建或附加到文件。

此方法也避免了行尾的空格。

>>"C:\Users\Powell\Desktop\Batch-files\Card\cards\%name%.txt" echo %content%