问题描述
在Windows计算机(胜7或Windows Server 2008 R2的),我有一个批处理脚本,一些.config文件复制到备份文件夹。结果我想要写另一个脚本,删除备份文件创建一个星期前。
On a windows machine (win 7 or Win server 2008 R2) I have a batch script that copies some .config files to a backup folder.
I want to write another script that deletes the backup files created a week earlier.
有大量的建议如何使用 FORFILES
(为例):
There are plenty of suggestions on how to use FORFILES
(as example):
FORFILES /P "D:\Configs_Backup" /M *.config /D -7 /C "cmd /c del @file"
但这个命令使用修改时间戳,而我需要使用的创建日期。
But this command uses the "modified" timestamp, while I need to use the creation date.
无需安装任何第三方程序,是通过命令控制台就可以实现这一目标?
Without installing any third party program, is it possible via command console to achieve this?
推荐答案
试试这个,看看输出,并删除回声
,如果它看起来不错:
try this, look at the output and remove the echo
, if it looks good:
@ECHO OFF &SETLOCAL ENABLEDELAYEDEXPANSION
SET /a XDay=7
CALL :DateToJDN "%DATE%" JDNToday
FOR /r "D:\Configs_Backup" %%a IN (*.config) DO (
FOR /f "tokens=1,4*" %%b IN ('dir /tc "%%~a"^|findstr "^[0-9]"') DO (
CALL :DateToJDN "%%b" filedate
SET /a diffdays=JDNToday-filedate
IF !diffdays! gtr %XDay% ECHO DEL /F /Q "%%~a"
)
)
GOTO :eof
:DateToJDN "DD mm/dd/yyyy" jdn=
setlocal
set date=%~1
set /A yy=%date:~-4%, mm=1%date:~-10,2% %% 100, dd=1%date:~-7,2% %% 100
set /A a=mm-14, jdn=(1461*(yy+4800+a/12))/4+(367*(mm-2-12*(a/12)))/12-(3*((yy+4900+a/12)/100))/4+dd-32075
endlocal & set %2=%jdn%
exit /B
请注意:这仅适用于 AM / PM
时间格式
Note: this works only for AM/PM
time format.
这篇关于批处理脚本删除早于X天的文件(根据创建日期,不修改日期)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!