Windows 打开记事本,用.bat文件写一些东西

Windows 打开记事本,用.bat文件写一些东西,windows,batch-file,exe,Windows,Batch File,Exe,使用批处理文件,我希望: 1) 打开记事本 2) 在记事本上写些东西并保存 有可能吗。如何做到这一点?在批处理文件中,使用以下选项启动Windows记事本,并选择一个文本文件,用户可以在该文件上输入文本,一旦用户退出Windows记事本并将输入的文本保存到文件中,批处理文件将进一步处理该文本 @echo off rem Create a text file with 0 bytes. type NUL >"%TEMP%\UserText.txt" rem Start Windows No

使用批处理文件,我希望:

1) 打开记事本

2) 在记事本上写些东西并保存


有可能吗。如何做到这一点?

在批处理文件中,使用以下选项启动Windows记事本,并选择一个文本文件,用户可以在该文件上输入文本,一旦用户退出Windows记事本并将输入的文本保存到文件中,批处理文件将进一步处理该文本

@echo off
rem Create a text file with 0 bytes.
type NUL >"%TEMP%\UserText.txt"

rem Start Windows Notepad with that empty text file and halt
rem execution of batch file until user finished typing the
rem text and exiting Notepad with saving the text file.
%SystemRoot%\notepad.exe "%TEMP%\UserText.txt"

rem Delete the text file if its file size is still 0.
for %%I in ("%TEMP%\UserText.txt") do if %%~zI == 0 del "%TEMP%\UserText.txt" & goto :EOF

rem Do something with the text file like printing the text.
type "%TEMP%\UserText.txt"

rem Finally delete the text file no longer needed.
del "%TEMP%\UserText.txt"
pause
但是,如果批处理文件应自行创建文本文件,则根本不需要使用Windows记事本,如以下代码所示:

@echo off
(
echo This is a demo on how text can be written into a text file.
echo/
echo The command ECHO is used to output text to console which is redirected
echo with redirection operator ^> into a file which is created always new
echo with overwriting the text file if already existing by chance.
echo/
echo See the Microsoft article "Using command redirection operators" with URL
echo https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-xp/bb490982(v=technet.10^)
echo for details.
) >"%TEMP%\UserText.txt"

rem Do something with the text file like printing the text.
type "%TEMP%\UserText.txt"

rem Finally delete the text file no longer needed.
del "%TEMP%\UserText.txt"
pause
注意:在Windows命令处理器处理ECHO命令行时,某些字符必须用插入符号转义。
^
才能解释为文字字符。如果命令行位于以
开始并以匹配的
结束的命令块内,则重定向运算符
|&
必须用
^
转义,也必须用
转义,而不是写在双引号参数字符串内

百分比符号
%
必须在批处理文件中用一个以上的百分比符号转义,以解释为文字字符,而不是批处理文件参数引用的开始、循环变量引用的开始或环境变量引用的开始/结束。还有一个感叹号
进行转义,即使用
^

要了解所使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读为每个命令显示的所有帮助页面

  • 呼叫/?
  • del/?
  • echo/?
  • 获取/?
  • goto/?
  • 如果/?
  • 暂停/?
  • rem/?
  • 设置/?
  • 键入/?

另请参见

您尝试了什么?正如所写的,您的问题并不是在代码问题上寻求帮助;它试图走捷径,阻止你自己做研究和测试。我建议您发布一段失败代码或删除您的问题,然后将查询发布到更合适的站点,(也许)这不是完全重复的,但相关代码行是相同的: