如何从批处理中编辑txt文件?

时间:2017-10-12 07:02:33

标签: batch-file text

所以现在,我正在试图弄清楚如何使批处理程序的用户可以编辑.txt文件。我已经有批处理文件创建文件,但现在我需要这样做,以便.txt文件具有用户为该问题回答的内容。这就是我现在所拥有的:

@echo off
echo Welcome to the Space Mapping Discord Report Maker
pause
echo Made by N3ther, version 0.1 ALPHA
pause
@echo off
break>"%userdomain%\%username%\Desktop\report.txt"
echo Question 1: What is your username on Discord?`

(这是报告制作人,我是主持人。)

1 个答案:

答案 0 :(得分:0)

看起来像下面的内容就是你需要的东西:

@echo off
setlocal EnableExtensions EnableDelayedExpansion
set "ReportFile=%USERPROFILE%\Desktop\report.txt"
del "%ReportFile%" 2>nul

echo Welcome to the Space Mapping Discord Report Maker
echo/
echo Made by N3ther, version 0.1 ALPHA
echo/

call :PromptUser 1 "What is your username on Discord?"
call :PromptUser 2 "What is ...?"

rem Restore saved environment and exit batch processing.
endlocal
goto :EOF

rem This small subroutine prompts the user for an input until something is
rem entered at all and then appends the entered input to the report file.

:PromptUser
set "Input="
:PromptAgain
set /P "Input=Question %~1: %~2 "
rem Has the user entered anything at all?
if not defined Input goto PromptAgain
echo !Input!>>"%ReportFile%"
goto :EOF

要了解使用的命令及其工作原理,请打开命令提示符窗口,执行以下命令,并完全阅读为每个命令显示的所有帮助页面。

  • call /?
  • del /?
  • echo /?
  • endlocal /?
  • goto /?
  • if /?
  • rem /?
  • set /?
  • setlocal /?

另请阅读Microsoft关于Using Command Redirection Operators的文章和有关Windows Environment Variables的维基百科文章。