2014-04-12 47 views
1

我有一个“信息卡”脚本,用于在FRC竞赛中记录各种团队的小笔记,我想知道是否有一种简单的方法可以添加新行文本文件。 我现在已经设置了它,以便它将标题添加到文本文件,然后允许您通过应用程序输入内容。但是,无论文本片段有多长,内容都显示在文本文件的第一行。在txt文件中添加新行Windows批处理

这是我有:

echo off 
color 0a 
title [email protected]! 
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. 

回答

0

难道这就是你的意思是设置一个变量,它是一个新的生产线? :

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

不是。我正在寻找一种方法让文本文件中的文本有多行,我当前的程序将用户在“内容”下输入的内容添加到文本文件中。但是,它只会将其添加到第一行。我试图找到一种方法来分解多行文本。我想要让程序知道每25个字符就应该在文本文件中添加一行。对不起,我的要求没有具体。 – Lpowell

+0

我建议你[编辑你的问题](http://stackoverflow.com/questions/23025483/adding-a-new-line-in-a-txt-file-windows-batch/23026502#23026502)并添加此那里的信息。 – Aacini

0
copy con filename.txt 

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

有两种类型的复制二进制和ascii。复制巧妙地选择使用哪一个,但您可以控制它 - 请参阅复制/ ?.二进制文件用于大小已知的文件等。 ASCII适用于串口和键盘等大小未知的流。文件结束,因此复制结束时,遇​​到Ctrl + Z字符(char num 26)。

如果你想添加到文件。

copy filename.txt+con filename.txt 

plus(+)连接文件。

+0

看看选择而不是set/p作为选择可以将键限制为只有1,2,3或4。 –

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 

应该打开你的文本编辑器创建该文件。

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 
0

在我看来,你的问题是如何在下面几行添加更多文本到文件。

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

该方法避免了行尾的空格。

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