打开记事本并使用 .bat 文件写一些东西

and*_*eek 0 windows exe batch-file

使用我想要的批处理文件:

1) 打开记事本

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

有没有可能这样做。你怎么能这样做?

Mof*_*ofi 6

在批处理文件中,使用以下命令启动带有您选择的文本文件的 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
Run Code Online (Sandbox Code Playgroud)

但是,如果批处理文件应该为自己创建一个文本文件,则完全不需要使用 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
Run Code Online (Sandbox Code Playgroud)

注意:某些字符必须用插入字符转义^才能在Windows 命令处理器处理ECHO命令行时解释为文字字符。重定向运算符<>|&必须转义,^并且)如果命令行位于(以匹配开头和结尾的命令块内),则结束圆括号未转义^且未写入双引号参数字符串中。

百分号%必须在批处理文件中再用一个百分号进行转义,才能被解释为文字字符,而不是作为批处理文件参数引用的开始、循环变量引用的开始或环境变量引用的开始/结束。并且感叹号!必须用两个插入符号转义,即^^如果启用了延迟环境变量扩展,默认情况下不是这种情况。

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

  • call /?
  • del /?
  • echo /?
  • for /?
  • goto /?
  • if /?
  • pause /?
  • rem /?
  • set /?
  • type /?

另请参阅Windows 命令解释器 (CMD.EXE) 如何解析脚本?