[英]Delete all files older than 1 day / 24hours using creation date?
I want to delete all files older than 1 day / 24 hours using a timed task in the OS and a batch file. 我想使用操作系统中的定时任务和批处理文件删除所有早于1天/ 24小时的文件。
This is needed for a simple backup mechanic I've implemented, that saves a certain file every hour and I don't want to delete old files manually. 这是我实现的简单备份机制所必需的,该机制每个小时都会保存一个文件,并且我不想手动删除旧文件。
I already implemented the following batch code, which successfully create a backup every hour. 我已经实现了以下批处理代码,该代码成功地每小时创建一个备份。 However, the delete portion of the code doesn't seem to work.
但是,代码的删除部分似乎不起作用。
Does anyone have any suggestions as to why? 有人对为什么有任何建议吗?
PS: I'm using Win7 x64. PS:我正在使用Win7 x64。
Code: 码:
rem Get day of week
SETLOCAL enabledelayedexpansion
SET /a count=0
FOR /F "skip=1" %%D IN ('wmic path win32_localtime get dayofweek') DO (
if "!count!" GTR "0" GOTO next
set dow=%%D
SET /a count+=1
)
:next
rem Check if day of week is Saturday or Sunday. If so, goto end of file.
if %dow% == 6 GOTO eof
if %dow% == 7 GOTO eof
rem Set hour
set TIMESTAMP=%TIME:~0,2%"Uhr"
rem Copy file including timestamp
xcopy "SOURCE FOLDER" "TARGET FOLDER\filename_%date%_%TIMESTAMP%.accdb*" /Y
rem Delete files older than 1 day
forfiles -p "TARGET FOLDER (from above)" -s -m *.* /D -1 /C "cmd /c del @path"
:eof
Edit: 编辑:
I found the cause of the original problem, it was the UNC paths. 我发现了最初问题的原因,这是UNC路径。 Instead of using them I just mapped the path to a drive letter now and deleting works fine now.
我没有使用它们,而是现在将路径映射到驱动器号,并且删除现在可以正常工作。
However, it deletes all my files now, instead of just the ones older than 1 day. 但是,它会立即删除我的所有文件,而不只是早于1天的文件。
I suspect that forfiles uses the last modification date instead of the creation date, as the last modification is still on 26th and the creation was like an hour ago... 我怀疑forfiles使用的是上次修改日期而不是创建日期,因为上一次修改仍在26日,并且创建就像一个小时前一样。
Is there a way to circumvent that behavior and use the creation date instead? 有没有办法避免这种行为并改用创建日期?
Alternatively, is there a way to set the modification date to a certain timestamp after copying the file? 或者,是否可以在复制文件后将修改日期设置为某个时间戳?
Maybe a little "convoluted" but this should do the work 也许有些“令人费解”,但这应该可以完成工作
@echo off
setlocal enableextensions disabledelayedexpansion
set "folder=\\server\share\somewhere"
for /f "tokens=*" %%a in (
'robocopy "%folder%" "%folder%" /l /nocopy /is /minage:1 /njh /njs /ndl /ns /nc /fp'
) do echo del "%%a"
del
commands are echoed to console. del
命令回显到控制台。 If the output is correct, remove the echo
command. 如果输出正确,请删除
echo
命令。
It uses the robocopy
command to search the required files in the UNC path. 它使用
robocopy
命令在UNC路径中搜索所需的文件。 So, natively, only for vista and later OS. 因此,本机仅适用于Vista和更高版本的操作系统。 For XP, the Windows 2003 Resource Kit Tools include a working version of robocopy.
对于XP,Windows 2003资源工具包工具包含robocopy的有效版本。
EDITED - One more. 编辑-还有一个。 Trying to avoid the timestamp on files
尝试避免文件上的时间戳
@echo off
setlocal enableextensions disabledelayedexpansion
set "folder=\\server\share\somewhere"
for /f "tokens=* skip=24" %%a in (
'dir /b /a-d /tc /o-d "%folder%"'
) do echo del "%folder%\%%a"
With a file generated every hour, keep the last 24 and delete the rest. 每小时生成一个文件,保留最后24个并删除其余的文件。
I was able to solve the problem by using the getTimestamp.bat from dbenham: http://www.dostips.com/forum/viewtopic.php?f=3&t=4847&p=27422#p27422 我可以使用dbenham的getTimestamp.bat解决问题: http : //www.dostips.com/forum/viewtopic.php? f =3& t = 4847&p = 27422#p27422
This is the only working batch file I could find that is able to calculate the date of yesterday (potentially any day for that matter). 这是我可以找到的唯一一个工作批处理文件,该文件能够计算昨天的日期(可能是该日期的任何一天)。
It is probably the only working one, because all others (and also all of the ones linked in the regarding StackOverflow threads) don't take locale settings into account, so they didn't even start for me. 它可能是唯一可行的方法,因为所有其他方法(以及所有在StackOverflow线程中链接的方法)都没有考虑到语言环境设置,因此对于我来说它们甚至都没有。
call getTimeStamp -od -1 -f {dd}.{mm}.{yyyy} -r dt
rem Copy file including timestamp
xcopy "\\unc\folder\fileToBackup.accdb" "\\unc\folder\backupedFile_%date%_%TIMESTAMP%.accdb*" /Y
rem Delete yesterdays file
del "\\unc\folder\backupedFile_%dt%_%TIMESTAMP%.accdb*"
forfiles
does indeed use the modified timestamp, not the created timestamp, so you cannot use it for what you want. forfiles
确实确实使用了修改后的时间戳,而不是创建的时间戳,因此您无法将其用于所需的时间戳。
You might like to check the answers to this question which parse the file creation date in a batch script, or if you're willing to use PowerShell then you could try this script with the arguments -CreateTime
option. 您可能希望检查此问题的答案,以批处理脚本解析文件创建日期,或者如果您愿意使用PowerShell,则可以尝试使用带有
-CreateTime
选项的参数的脚本 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.