如何使用批处理写入文本文件的不同行

时间:2015-04-24 18:01:33

标签: batch-file text

我写了以下代码:

@echo off
title Kiel Configurations
rem Credits Will Go Here
rem Website Here
color 08
echo **********************************************************************
echo **************************Kiel Configuration**************************
echo **********************************************************************
echo ======================================================================
echo Use yes/no to configure the following options
pause
cls
echo Would you like it to run in fake mode? (recommended for slow computers or for the paranoid)
set /p FakeModeVar=(yes/no):
cls
echo Would you like to close skype?
set /p SkypeVar=(yes/no):
cls
echo Woud you like to close spotify?
set /p SpotifyVar=(yes/no):
cls
echo Would you like to close steam?
set /p SteamVar=(yes/no)
cls
echo Would you like to close chrome? (reccommended)
set /p ChromeVar=(yes/no):
cls
echo Which chrome profile would you like chrome to open as? (Ask me if unsure)
set /p ChromeProfile=(1,2,3...):
cls
echo Now configurating you settings...
pause >nul
if %FakeModeVar%==yes goto Locate1
if %FakeModeVar%==no goto Locate2
:Locate1
echo yes> ConfigVarini.txt
goto Locate3
:Locate2
echo no> ConfigVarini.txt
goto Locate3
:Locate3
if %SkypeVar%==yes goto Locate4
if %SkypeVar%==no goto Locate5
:Locate4
echo yes>> ConfigVarini.txt
goto Locate6
:Locate5
echo no>> ConfigVarini.txt
goto Locate6
:Locate6
if %SpotifyVar%==yes goto Locate7
if %SpotifyVar%==no goto Locate8
:Locate7
echo yes>>> ConfigVarini.txt
goto Locate9
:Locate 8
echo no>>> ConfigVarini.txt
goto locate9
:Locate9
if %SteamVar%==yes goto Locate10
if %Steamvar%==no goto Locate11
:Locate10
echo yes>>>> ConfigVarini.txt
goto Locate12
:Locate11
echo no>>>> ConfigVarini.txt
goto Locate12
:Locate12
if %ChromeVar%==yes goto Locate13
if %ChromeVar%==no goto Locate14
:Locate13
echo yes>> ConfigVarini.txt
goto Locate15
:Locate14
echo no>>>>> ConfigVarini.txt
goto Locate15
:Locate15
if %ChromeProfile%==1 goto Locate16
if %ChromeProfile%==2 goto Locate17
if %ChromeProfile%==3 goto Locate18
:Locate16
echo 1>>>>> ConfigVarini.txt
goto End
:Locate17
echo 2>>>>>> ConfigVarini.txt
goto End
:Locate18
echo 3>>>>>> ConfigVarini.txt
goto End
:End
cls
echo Files Configured!

当我运行它时,我希望它让用户完成配置过程,然后将他们的首选项保存到名为ConfigVarini.txt的文本文件中 然后我将有另一个程序读取文本文件上的设置。然而,文件上说

  

是的是

并且不保存任何其他设置。

3 个答案:

答案 0 :(得分:0)

您可以使用“>>”

附加到批处理文件
@echo off
echo Hi >> test.txt
echo Hello >> test.txt
echo. >>test.txt
echo 3 >> test.txt

包含

的名为test.txt的文件中的结果
Hi
Hello

3

除此之外,我不确定你想要什么。 在输入文件之前留出空格也是一种很好的做法,因为从1到9的数字会导致问题。

答案 1 :(得分:0)

Imho最佳实践:最初,使用

清空输出文件
type nul> ConfigVarini.txt

和所有进一步的写作

(ECHO any text)>> ConfigVarini.txt

>> ConfigVarini.txt (ECHO any text)

注意:

  • >之前没有空格,>> redirection operator也没有)左右括号。否则,该空间在输出文件中显示为尾随空格(可能至少很奇怪甚至有害);
  • ()括号:如果输出文本结束单个数字 01,...,{,则非常重要{1}}。

解释9给出的示例,但对任何单个十进制数有效):

  • 1(或空行或echo 1>> ConfigVarini.txt文本)附加到输出文件,因为CLI或批处理解析器将单个十进制数字视为数字文件句柄被重定向(参见上面的重定向链接);
  • ECHO is on/offecho 1 >> ConfigVarini.txt添加尾随空格;
  • 1追加>> ConfigVarini.txt ECHO 1,但我们无法确定是否存在不需要的(遗忘的)尾随空格;
  • 1在没有尾随空格的情况下附加>> ConfigVarini.txt (ECHO 1);
  • 1在没有尾随空格的情况下附加(echo 1)>> ConfigVarini.txt;

More explanation and discussion here

答案 2 :(得分:0)

一个单一>将覆盖ConfigVarini.txt中的任何内容并仅添加一行。两个>>将在新行上添加另一行,并保留以前写入的内容。每次启动Locate1或Locate2时,使用下面的代码将覆盖旧设置并应用新设置。 (我没有正确检查所有代码,只更改了您的有缺陷的>>用法。如果需要,有人检查和编辑)

@echo off
title Kiel Configurations
rem Credits Will Go Here
rem Website Here
color 08
echo **********************************************************************
echo **************************Kiel Configuration**************************
echo **********************************************************************
echo ======================================================================
echo Use yes/no to configure the following options
pause
cls
echo Would you like it to run in fake mode? (recommended for slow computers or for the paranoid)
set /p FakeModeVar=(yes/no):
cls
echo Would you like to close skype?
set /p SkypeVar=(yes/no):
cls
echo Woud you like to close spotify?
set /p SpotifyVar=(yes/no):
cls
echo Would you like to close steam?
set /p SteamVar=(yes/no)
cls
echo Would you like to close chrome? (reccommended)
set /p ChromeVar=(yes/no):
cls
echo Which chrome profile would you like chrome to open as? (Ask me if unsure)
set /p ChromeProfile=(1,2,3...):
cls
echo Now configurating you settings...
pause >nul
if %FakeModeVar%==yes goto Locate1
if %FakeModeVar%==no goto Locate2
:Locate1
echo yes > ConfigVarini.txt
goto Locate3
:Locate2
echo no > ConfigVarini.txt
goto Locate3
:Locate3
if %SkypeVar%==yes goto Locate4
if %SkypeVar%==no goto Locate5
:Locate4
echo yes >> ConfigVarini.txt
goto Locate6
:Locate5
echo no >> ConfigVarini.txt
goto Locate6
:Locate6
if %SpotifyVar%==yes goto Locate7
if %SpotifyVar%==no goto Locate8
:Locate7
echo yes >> ConfigVarini.txt
goto Locate9
:Locate 8
echo no >> ConfigVarini.txt
goto locate9
:Locate9
if %SteamVar%==yes goto Locate10
if %Steamvar%==no goto Locate11
:Locate10
echo yes >> ConfigVarini.txt
goto Locate12
:Locate11
echo no >> ConfigVarini.txt
goto Locate12
:Locate12
if %ChromeVar%==yes goto Locate13
if %ChromeVar%==no goto Locate14
:Locate13
echo yes >> ConfigVarini.txt
goto Locate15
:Locate14
echo no >> ConfigVarini.txt
goto Locate15
:Locate15
if %ChromeProfile%==1 goto Locate16
if %ChromeProfile%==2 goto Locate17
if %ChromeProfile%==3 goto Locate18
:Locate16
echo 1 >> ConfigVarini.txt
goto End
:Locate17
echo 2 >> ConfigVarini.txt
goto End
:Locate18
echo 3 >> ConfigVarini.txt
goto End
:End
cls
echo Files Configured!