【windows bat脚本编写】windows服务器bat删除指定日期的文件

时间:2019-11-30  来源:Windows服务器  阅读:

windows系统自带一个命令forfiles可以根据文件的时间戳来操作文件,
forfiles /? 可以查看详细的帮助


FORFILES [/P pathname] [/M searchmask] [/S] [/C command] [/D [+ | -] {yyyy/MM/dd | dd}]
/p  “目录名” 指定搜索路径
/S   递归搜索
/C “指定要执行的命令”
/D 指定时间查找
+ yyyy/MM/dd     指定绝对时间,查找并这个日期晚的文件

–  dd                   相对当前时间,并当前时间早n天的文件

显示指定目录下超过6天没修改的文件,默认会输出文件名
forfiles /p “%bak_dir%” /S /D -6
forfiles /p “%bak_dir%” /S /D -6 -C “cmd /c echo @file”

输出完整路径

forfiles /p “%bak_dir%” /S /D -6 -C “cmd /c echo @path”

删除指定目录下最后修改时间超过6天的文件

forfiles /p “%bak_dir%” /S /D -6 /C “cmd /c del /q @file”

备份指定路径下的文件,先设置环境变量,由于不内使用两次””所以无法使用变量替换的方式引用rar.exe

IF EXIST “c:\Program Files\winrar”  set Path=;”c:\Program Files\winrar”;%SystemRoot%\system32
IF EXIST “c:\Program Files (x86)\winrar” set Path=;”c:\Program Files (x86)\winrar”;%SystemRoot%\system32
forfiles /p “%bak_dir%” /S /D -6 /C “cmd /c rar.exe a backp.rar @path”


bat获取上个月份

@echo off
setlocal EnableDelayedExpansion
pause
rem ################# 获得上个月的日期 ########################
rem 获取年度
set year=%date:~0,4%
rem 获取月份
echo 当前年份: %year%
set mounth=%date:~5,2%
echo 当前月份: %mounth%
echo #####################
if "%mounth%"=="01" (
 set /A year=%year%-1
 set /A lastmounth=01
 echo 当前年月: %year%_%mounth%
 echo **************
) else (
 rem 将当前月份减一
 echo %mounth%
 set /A lastmounth=!mounth!-1
 echo 上个月份: !lastmounth!
 rem 如果当前月份减1后,小于10 则 用零补充 例如 2 = 02
 if !lastmounth! lss 10 (
 set lastmounth=!lastmounth!
 )
 echo 上个月: %year%_!lastmounth!
)
pause
方法二

?
@echo off
rem setlocal
echo 日期:%date%
REM 分离年月日
@set year=%date:~0,4%
set month=%date:~5,2%
set day=%date:~8,2%
echo this year: %year%
echo this month: %month%
echo ############# test #################
if "%month%"=="01" (
 set /A lastyear=%year%-1
 echo last year: %lastyear%
 set lastmonth=12
 echo last month: %lastmonth%
 echo last year_month: %lastyear%_%lastmonth%
 echo ############ month == 01 #############
)else (
 if "%month%"=="02" set lastmonth=01
 if "%month%"=="03" set lastmonth=02
 if "%month%"=="04" set lastmonth=03
 if "%month%"=="05" set lastmonth=04
 if "%month%"=="06" set lastmonth=05
 if "%month%"=="07" set lastmonth=06
 if "%month%"=="08" set lastmonth=07
 if "%month%"=="09" set lastmonth=08
 if "%month%"=="10" set lastmonth=09
 if "%month%"=="11" set lastmonth=10
 if "%month%"=="12" set lastmonth=11
 echo last year_month: %year%_%lastmonth%
 echo ############### month != 01 ##############
)
pause

【windows bat脚本编写】windows服务器bat删除指定日期的文件

http://m.bbyears.com/caozuoxitong/81568.html

推荐访问:
相关阅读 猜你喜欢
本类排行 本类最新