我根据这个结构最多有4个文件(注意前缀是日期)
0830filename.txt
0907filename.txt
0914filename.txt
0921filename.txt
我想打开最近的一个(0921filename.txt).我怎么能在批处理文件中这样做?
谢谢.
此方法使用实际的文件修改日期,以确定哪一个是最新文件:
@echo off for /F %%i in ('dir /B /O:-D *.txt') do ( call :open "%%i" exit /B 0 ) :open start "dummy" "%~1" exit /B 0
但是,此方法按字母顺序选择最后一个文件(或以反向字母顺序选择第一个文件),因此如果文件名一致 - 它将起作用:
@echo off for /F %%i in ('dir /B *.txt^|sort /R') do ( call :open "%%i" exit /B 0 ) :open start "dummy" "%~1" exit /B 0
你实际上必须选择哪种方法更适合你.
对不起,对于发送此问题的垃圾邮件,但我真的很想发布真实答案.如果您希望BATCH脚本解析并比较文件名中的日期,那么您可以使用以下内容:
@echo off rem Enter the ending of the filenames. rem Basically, you must specify everything that comes after the date. set fn_end=filename.txt rem Do not touch anything bellow this line. set max_month=00 set max_day=00 for /F %%i in ('dir /B *%fn_end%') do call :check "%%i" call :open %max_month% %max_day% exit /B 0 :check set name=%~1 set date=%name:~0,4% set month=%date:~0,2% set day=%date:~2,2% if /I %month% GTR %max_month% ( set max_month=%month% set max_day=%day% ) else if /I %month% EQU %max_month% ( set max_month=%month% if /I %day% GTR %max_day% ( set max_day=%day% ) ) exit /B 0 :open set date=%~1 set month=%~2 set name=%date%%month%%fn_end% start "dummy" "%name%" exit /B 0