简体   繁体   English

将输出重定向到批处理文件中的文件是否需要关闭? (批处理脚本)

[英]Redirecting output to file in batch file requires closing? (batch script)

In a batch script redirecting the output to a file like so 在批处理脚本中,将输出重定向到这样的文件

set "output=C:\output.txt"
echo Blah blah blah. >> %output%

Is it required that the file is closed after the redirection of writing stuff to it is completed (similar to the approach in other programming languages)? 是否需要在完成向其写入内容的重定向之后关闭文件(类似于其他编程语言中的方法)?

I have tried searching for related information online but could not find something on it; 我曾尝试过在线搜索相关信息,但找不到任何信息。 I assume the fact that most scripts are closed after they finish their tasks (commands) is maybe the reason why. 我认为大多数脚本在完成任务(命令)后都会关闭的事实可能就是原因。

But if say a script is to run in an endless loop where a different output file is written (eg by appending the time to the output file name) or if new output is constantly being redirected to the same output file, could the " not closing of the file " potentially lead to problems, memory or other? 但是,如果说剧本是一个无限循环 ,其中不同的运行output文件写入(例如,通过附加的时间到output文件名),或者如果新的输出不断被重定向到同一个output文件,可以在“ 不打烊文件的 “潜在地导致问题,内存或其他?

No, you don't have to close any file handles in batch scripts. 不,您不必在批处理脚本中关闭任何文件句柄。 You don't know the file handle value so you could not even close it if you wanted to. 您不知道文件句柄值,因此如果您想关闭它,甚至无法将其关闭。

On Windows, all open kernel handles are closed when a process ends/crashes but since a batch file is interpreted by cmd.exe without starting a new cmd.exe process in most cases, it cannot take advantage of the automatic handle cleanup and will manually close the file handle after each redirected operation. 在Windows上,当进程结束/崩溃时,所有打开的内核句柄都将关闭,但是由于在大多数情况下cmd.exe会解析批处理文件而不启动新的cmd.exe进程,因此它无法利用自动句柄清理功能,而是会手动进行每次重定向操作后,关闭文件句柄。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM