简体   繁体   English

在Windows 7中从命令行打开目录中的最新文件

[英]Opening the most recent file in a directory from the command line in Windows 7

I am using a device that generates an output file in .txt format. 我正在使用生成.txt格式的输出文件的设备。 The file name is generated using tokens for the experiment name and an incremented token: <ExperimentName><IncrementedToken>.txt. 使用实验名称的令牌和递增的令牌生成文件名:<ExperimentName> <IncrementedToken> .txt。 The output directory is filled with a number of output files from an array of experiments, and it has become difficult to find the most recent output file. 输出目录中充满了来自一系列实验的许多输出文件,并且很难找到最新的输出文件。

I am trying to come up with a script to launch the most recent output file from the directory where these files are saved using the command line. 我正在尝试提出一个脚本,以使用命令行从保存这些文件的目录中启动最新的输出文件。

So far I have been able to use dir PathToOutputFolder /b /od | head -1 到目前为止,我已经能够使用dir PathToOutputFolder /b /od | head -1 dir PathToOutputFolder /b /od | head -1 to find the most recent file, but am having trouble launching the file from the command line. dir PathToOutputFolder /b /od | head -1查找最新文件,但是无法从命令行启动文件。

The closest (I think) that I've gotten so far is something along the lines of: start "" notepad dir PathToOutputFolder /b /od | head -1 到目前为止,我最接近的(我认为)是: start "" notepad dir PathToOutputFolder /b /od | head -1 start "" notepad dir PathToOutputFolder /b /od | head -1 but this gives me a "system cannot find path specified" error. start "" notepad dir PathToOutputFolder /b /od | head -1但这给了我“系统找不到指定的路径”错误。

I'm thinking that the output of the dir command can't be passed to the start command, but would like to find a workaround. 我在想dir命令的输出不能传递给start命令,但是想找到一种解决方法。

(Caveat: This will fail if your console is set to Raster fonts and your file names contain characters that won't fit in the OEM codepage.) (注意:如果将控制台设置为Raster字体,并且文件名包含OEM代码页中不适合的字符,则此操作将失败。)

for /f "delims=" %%x in ('dir PathToOutputFolder /b /o-d') do if not set filename set "filename=%%x"
start "" "%filename%"

If you have the option of using PowerShell that is much easier, though: 但是,如果您可以选择使用PowerShell,则更加简单:

$filename = (Get-ChildItem PathToOutputFolder | sort LastWriteTime)[-1]
Invoke-Item $filename

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

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