batch脚本查看前一天目录下的文件
时间: 2024-01-03 07:05:17 浏览: 35
以下是一个可以查看前一天目录下文件的 Windows 批处理脚本示例:
```
@echo off
setlocal enabledelayedexpansion
rem 设置需要查找的目录
set "dir_path=D:\example"
rem 获取前一天的日期
set /a "day=%date:~8,2% - 1"
set /a "month=%date:~5,2%"
set /a "year=%date:~0,4%"
if %day%==0 (
set /a "month=%month% - 1"
if %month%==0 (
set /a "year=%year% - 1"
set "month=12"
)
if %month%==1 set "days=31"
if %month%==2 set /a "leap=year %% 4, days=28 + leap - (leap / 4)"
if %month%==3 set "days=31"
if %month%==4 set "days=30"
if %month%==5 set "days=31"
if %month%==6 set "days=30"
if %month%==7 set "days=31"
if %month%==8 set "days=31"
if %month%==9 set "days=30"
if %month%==10 set "days=31"
if %month%==11 set "days=30"
if %month%==12 set "days=31"
set "day=%days%"
)
if %day% lss 10 set "day=0%day%"
if %month% lss 10 set "month=0%month%"
set "datestr=%year%-%month%-%day%"
rem 查找目录下前一天创建或修改的文件
for /f "delims=" %%a in ('dir /b /a-d "%dir_path%\*" ^| findstr /r "^.*_[0-9]\{8\}\.txt$"') do (
for /f "delims=" %%b in ("%%~ta") do (
set "filedate=%%b"
set "filedate=!filedate:~0,10!"
if !filedate! equ %datestr% echo %%a
)
)
```
解释:
1. `setlocal enabledelayedexpansion`:启用延迟环境变量扩展,以便在 for 循环中访问变量。
2. `set "dir_path=D:\example"`:设置需要查找的目录。
3. `set /a "day=%date:~8,2% - 1"`:获取当前日期的前一天。
4. `if %day%==0` 到 `set "day=%days%"`:计算前一天的日期,考虑闰年和月末的情况。
5. `if %day% lss 10 set "day=0%day%"` 和 `if %month% lss 10 set "month=0%month%"`:将日期和月份转换成两位数字。
6. `set "datestr=%year%-%month%-%day%"`:将前一天的日期保存为字符串。
7. `dir /b /a-d "%dir_path%\*"`:列出目录下的所有非目录文件。
8. `findstr /r "^.*_[0-9]\{8\}\.txt$"`:过滤出文件名以 `_yyyymmdd.txt` 结尾的文件。
9. `for /f "delims=" %%a in ('...') do (...)`:遍历文件列表。
10. `for /f "delims=" %%b in ("%%~ta") do (...)`:获取文件的修改时间。
11. `set "filedate=!filedate:~0,10!"`:将修改时间转换成日期字符串。
12. `if !filedate! equ %datestr% echo %%a`:如果文件的修改日期等于前一天的日期,则输出文件名。