[英]How do I close a file created from command line?
I'm writing command line output to a file and then want to open this file in notepad. 我正在将命令行输出写入文件,然后要在记事本中打开此文件。 So I wrote something like the following (simplified example): 所以我写了类似下面的内容(简化示例):
set logPath=log.txt
echo %date% %time% > %logPath%
notepad %logPath%
But the problem is that when the last command is run (notepad %logPath%), the file is not yet created (or the contents is not flushed to it), so notepad doesn't open non-existent file or opens an empty one (depends). 但是问题在于,当运行最后一个命令(记事本%logPath%)时,尚未创建文件(或未将内容刷新到该文件),因此记事本不会打开不存在的文件或打开一个空文件(要看)。
So I would like to know if there's a way to force file close from command line. 所以我想知道是否有一种方法可以从命令行强制关闭文件。
UPD: thank you for comments, you're right: this particular (simplified) example really does work. UPD:谢谢您的评论,您是对的:这个特定的(简化的)示例确实有效。 I tried on W7 and there are reports in comments that it does work in XP and Vista. 我在W7上尝试过,并且在评论中有报告说它可以在XP和Vista中使用。 It doesn't in the original script, will try to look for an error there. 它不在原始脚本中,将尝试在其中查找错误。
Your example code works for me under Microsoft Windows XP [version 5.1.2600]. 在Microsoft Windows XP [版本5.1.2600]下,您的示例代码对我有效。 As far as I know you only need to close file you opened and piping is not opening. 据我所知,您只需要关闭打开的文件而管道未打开。
So you should check your real code for some error (update your question with it?). 因此,您应该检查您的真实代码中是否有错误(使用它来更新您的问题吗?)。
Try this: 尝试这个:
echo %date% %time% > log.txt
notepad %logPath%
I think, it expects a filename on redirection and not a variable name. 我认为,它期望重定向时使用文件名而不是变量名。
这个问题看起来很奇怪,但是如果发生,为什么不等一秒钟
CHOICE /C:x /T:x,1 > NUL
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.