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

win2008自动删除2天以前的文件夹
@echo begin
echo Dont close this windows! 
echo Delete the file 2 days ago and directory! 
@echo off&setlocal enabledelayedexpansion
echo set arg=wscript.arguments>temp.vbs
echo wscript.echo datediff("d",now(),arg(0))>>temp.vbs
for /f "delims=" %%i in ('dir /ad/b *') do (
for /f "delims=" %%a in ('cscript.exe /nologo "%~dp0temp.vbs" "%%~ti"') do set d=%%a
if !d! lss -2 (echo %%i&rd /s/q "%%i"))
del/q/f "%~dp0temp.vbs"
echo Delete OK! 

保存为BAT,双击执行没问题。
但是放在计划任务里就删除不了文件夹。


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

@echo off
echo Dont close this windows!
echo Delete the file 2 days ago and directory!
setlocal enabledelayedexpansion
echo set arg=wscript.arguments>"%temp%\temp.vbs"
echo wscript.echo datediff("d",now(),arg(0))>>"%temp%\temp.vbs"
for /f "delims=" %%i in ('dir /ad /b') do (
    for /f "delims=" %%a in ('cscript.exe /nologo "%temp%\temp.vbs" "%%~ti"') do (
        set d=%%a
    )
    if !d! lss -2 (
        echo %%i
        rd /s/q "%%i"
    )
)
echo Delete OK