简体   繁体   English

如何关闭从命令行创建的文件?

[英]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.

相关问题 如何关闭从命令行使用.hta打开的窗口? (不是手动的) - How do I close a window that was opened with a .hta from the command line? (not manually) 从命令行运行 m 文件时如何隐藏“MATLAB 命令行窗口”? - How do I hide “MATLAB Command Window” when I run an m-file from command line? 在将参数传递给 PS 文件时,如何使用 invoke-command 从 windows 命令行运行 PS 文件? - How do I use invoke-command to run a PS file from the windows command line while passing parameters to the PS file? 如何从Windows命令行shell脚本中打开(锁定)文件? - How do I hold a file open (locked) from a Windows command-line shell script? 如何在Imagemagick转换命令行中从文件名中提取字符 - How do I extract characters from a file name in the Imagemagick convert command line 如何在python命令行中运行.py文件? - how do I run my .py file from within python command line? 如何从 Windows 下的命令行打开文件进行编辑? - How do I open a file for edit from the command line under Windows? 如何使用命令行或批处理文件测量吞吐量/带宽 - how do i measure throughput / bandwidth with command line or batch file 如何使用命令行启动 PHP 文件 - How do I launch a PHP file using command line 如何从命令行设置时区? - How do I set the timezone from command line ?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM