删除所有文件比使用创建日期1天/ 24小时旧的? [英] Delete all files older than 1 day / 24hours using creation date?

查看:479
本文介绍了删除所有文件比使用创建日期1天/ 24小时旧的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用在OS中的定时任务,一个批处理文件删除所有文件超过1天/ 24小时。

I want to delete all files older than 1 day / 24 hours using a timed task in the OS and a batch file.

这是需要一个简单的备份机械师我实现,可以节省每小时某个文件,我不想手动删除旧文件。

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.

我已经实现了以下批处理code,成功创建备份每隔一小时。然而,code的删除部分似乎并没有工作。

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:我使用的是Win7的X64

PS: I'm using 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

编辑:

我发现,原来问题的原因,它是UNC路径。而不是使用他们的我只映射路径现在一个驱动器号和删除现在正常工作。

I found the cause of the original problem, it was the UNC paths. Instead of using them I just mapped the path to a drive letter now and deleting works fine now.

不过,现在我删除所有文件,而不只是那些超过1天以上。

However, it deletes all my files now, instead of just the ones older than 1 day.

我怀疑FORFILES使用的最后修改日期,而不是​​创建日期,作为最后的修改仍然在26日和创作就像一个小时前...

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...

有没有办法来规避该行为,使用创建日期呢?

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?

推荐答案

我是能够通过使用getTimestamp.bat从dbenham来解决这个问题:的 http://www.dostips.com/forum/viewtopic.php?f=3&t=4847&p= 27422#p27422

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

这是唯一的工作批处理文件,我可以找到能够计算昨日之日(可能为此事的任何一天)。

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).

这可能是唯一的工作之一,因为所有的人(和所有的关于StackOverflow的线程联系在一起的那些)没有考虑区域设置进去,所以他们甚至没有开始对我来说。

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.

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*"

这篇关于删除所有文件比使用创建日期1天/ 24小时旧的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆