假设test1文件夹中有以下文件(3个文件为一组):
CH50_XX_201007071641.csv
CH50_YY_201007071641.csv
CH50_ZZ_201007071641.csv
CH50_XX_201006301641.csv
CH50_YY_201006301641.csv
CH50_ZZ_201006301641.csv
CH50_XX_201006221641.csv
CH50_YY_201006221641.csv
CH50_ZZ_201006221641.csv
现要提取上个月(6月)最后一组文件(日期不一定为30号,只要是上月最后一天的数据)移至文件夹test2中,结果如下:
CH60_XX_201006301641.csv
CH60_YY_201006301641.csv
CH60_ZZ_201006301641.csv
代码如下:
@echo off
set sp="D:\test1\ch50_*_!ym!!d!*.csv"
set dp="D:\test2\ch60_*_!ym!!d!*.csv"
set tp=%~dp0tmp.vbs
echo wscript.echo year(dateadd("m",-1,date))^&^
right("0"^&month(dateadd("m",-1,date)),2)>"%tp%"
setlocal enabledelayedexpansion
for /f %%a in ('cscript /nologo "%tp%"') do (set ym=%%a&del "%tp%")
for /l %%a in (31,-1,1) do (
set d=0%%a
set d=!d:~-2!
if exist %sp% (copy /y %sp% %dp%&&del /f /q %sp%&&pause&exit)
)
echo 没有找到目标文件,按任意键退出!
pause>nul
exit