batch 找到最新的文件

发布时间 2023-05-27 17:32:54作者: Alan_LJP

batch命令

找到当前文件夹中最新的文件,赋值并打印出来文件名(前缀+后缀)

for /f "tokens=*" %%a in ('dir /b /od') do set newest_zip_file=%%a
echo The most recently created file is: %newest_zip_file%

举例一

找到当前文件夹中最新的txt文件,赋值并打印出来txt文件名

for /f "tokens=*" %%a in ('dir /b /od *.txt') do set newest_txt_file=%%a
echo The most recently .txt file is: %newest_txt_file%

举例二:

找到当前文件夹中最新的zip文件,赋值并打印出来zip文件名,然后解压这个zip文件

for /f "tokens=*" %%a in ('dir /b /od *.zip') do set newest_zip_file=%%a
echo The most recently .zip file is: %newest_zip_file%
tar -xf %newest_zip_file%