简体   繁体   English

使用创建日期删除所有早于1天/ 24小时的文件吗?

[英]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.

相关问题 在文件夹中的所有文件都超过24小时之前,请勿删除文件夹 - Don't remove a folder until all files in the folder is older than 24 hours 用于删除超过 X 天的文件的批处理脚本(基于创建日期,而不是修改日期) - Batch script to delete files older than X days (based on creation date, not modified date) 如果所有文件都超过3天,则批量文件删除目录中的子文件夹 - Batch file to delete subfolders in directory if all files is older than 3 days 使用7ZIP和CMD压缩和删除7天以上的文件 - ZIP and delete files older than 7 days using 7ZIP and CMD 如何根据文件路径中的目录递归地删除* .bak文件,早于特定日期? - How to delete *.bak files recursively older than a specific date depending on directory in file path? 批处理脚本以检查子目录中的现有文件是否早于一天 - Batch Script to check if existing files inside subdirectories older than a day 如何在powershell中删除30天以前的文件并保留每个月的最后一天文件 - How to delete 30 day older files and keep last day files for each month in powershell 从子文件夹中删除早于特定日期的专门命名的子文件夹 - Delete specifically named subfolder older than a specific date from subfolders 批处理文件删除超过N天的文件 - Batch file to delete files older than N days 如何删除以子字符串结尾且早于 N 天的文件 - How to delete files end with a substring and older than N days
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM