日期:2014-05-17  浏览次数:20803 次

删除文件的批处理
求一批处理文件,能够删除指定目录下(D:\testfile)的今天(也就是执行批处理时当天)前创建的所有文件。小弟从没写过bat文件,请高手帮忙写一个,谢了

------解决方案--------------------
BatchFile code
@echo off
echo 今天是:%date%
echo.
set /a "today=%date:~0,4%*365+%date:~5,2%*30+%date:~8,2%"
setlocal enabledelayedexpansion
for /f "delims=" %%i in ('dir c:\test\*.* /s /b /a-d') do (
  set tt=%%~ti
  set /a "dd=!tt:~0,4!*365+!tt:~5,2!*30+!tt:~8,2!"
  if !dd! lss %today% (echo 删除当前文件:%%i !tt!)&&(del "%%i" /f /a /q)
)
echo.
echo OK
pause>nul

------解决方案--------------------
test.bat

BatchFile code
@echo off
rem 指定待删除文件的存放路径
set "SrcDir=D:\Test\"
rem 假设系统日期格式是yyyy-mm-dd
set "DstDate=%date:~0,10%"
for /f "delims=/" %%a in ('dir /s /b /a-d "%SrcDir%"') do (
    call :CompareTime "%%a"
)
goto :eof
:CompareTime
for /f "skip=5 tokens=1-2 delims= " %%h in ('dir /a-d /tc %1') do (
    if "%%h" neq "%DstDate%" (
        if exist %1 (
            del /a /f /q %1
        )
    )
    goto :eof
)