在Windows 7上批处理作业以删除7天以上的文件夹 [英] Batch job to delete folder older than 7 days on windows 7

查看:68
本文介绍了在Windows 7上批处理作业以删除7天以上的文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要删除Windows 7计算机上已存在7天以上的文件夹,并且其中有数据.

Hi I need to delete folders on my Windows 7 computer which are older than 7 days and have data in it.

我使用了以下命令,这些命令对我来说效果不佳

I have used the following commands which haven't worked out well for me

  1. 文件/S/D -10/C"cmd/c IF @isdir == TRUE rd/S/Q @path"
  2. forfiles -p"C:\ what \ ever" -s -m *.* -d<天数>-c"cmd/c del @path"

我从>要删除的批处理文件引用的第二个命令超过N天的文件也不适合我.

有人可以建议在 C:\ what \ ever 上的第二个命令中使用什么,或者建议其他命令删除7天以上的文件夹.

Can someone please suggest what to use in the second command on C:\what\ever or suggest an alternative command to delete my folders older than 7 days.

推荐答案

因此,您的任务是删除不再需要的旧备份目录.

So your task is to delete older backup directories no longer needed.

棘手的解决方案将用于删除X天之前的邮件".

The tricky solution would be working with "delete older than X days".

简单的解决方案如下:

@echo off
set "BackupDirectory=C:\Backup"
for /F "skip=7 delims=" %%D in ('dir "%BackupDirectory%" /AD /B /O-D 2^>nul') do (
    rd /Q /S "%BackupDirectory%\%%D"
)

命令 DIR 返回包含以下内容的列表

The command DIR returns a list which contains

  • 因为/AD (属性目录)只是子目录和
  • 因为/B (裸格式)只是目录名称和
  • 因为按日期排序的/O-D ,最新的排在最前,最旧的排在最后.
  • because of /AD (attribute directory) just the subdirectories and
  • because of /B (bare format) just directory names and
  • because of /O-D ordered by date with newest at top and oldest last.

命令 FOR 从列表中跳过前7行,即最新的7个子目录,并在其他(旧)目录上执行该命令以删除该子目录.

The command FOR skips the first 7 lines from list, i.e. the 7 newest subdirectories, and executes on the other (older) directories the command to remove the subdirectory.

如果您的目录名称以 yyyy-mm-dd 开头,则也可以使用/ON (按名称反向排列)代替/OD 保留 skip = x 的最新子目录并删除所有其他子目录.

In your case with directory names starting with yyyy-mm-dd it would be also possible to use /O-N (ordered reverse by name) instead of /O-D to keep skip=x newest subdirectories and delete all others.

注意:在NTFS分区上,如果添加/修改/删除了此文件夹中的任何文件/文件夹,则文件夹的最后修改日期会更改,但在FAT16,FAT32或exFAT分区上不会更改.

Note: On NTFS partitions the last modification date of a folder changes if any file/folder in this folder is added/modified/deleted, but not on FAT16, FAT32 or exFAT partitions.

/O-D 之后的 DIR 命令行上,可以添加选项/TC get输出目录列表,该目录列表按文件夹的创建日期反向排序.但是创建备份后,通常不会对备份文件夹进行修改.保留最新的文件夹以考虑对文件夹的修改或仅对文件夹的创建时间进行评分,这是一个观点.

On DIR command line after /O-D the option /TC can be added get output the directory list sorted reverse by creation date of the folders. But backup folders are usually not modified later after backup was created. It is a matter of opinion to keep the newest folders taking modifications in the folders into account or rate only folder creation time.

以我删除备份的经验来看,日期并不是那么重要.重要的是仅限制备份数以避免填充存储介质.例如,如果一个备份通常需要5 GiB,则保留的备份数量可能是10或20,但是如果一个备份通常仅需要500 KiB,则数量可以增加到100.日期并不重要,只需要总量备份所需的字节数,因为限制是存储介质的大小而不是时间跨度.

In my experience on deleting backups the date is not so important. Important is only limiting the number of backups to avoid filling a storage media. For example if a backup usually needs 5 GiB, the number of backups to keep might be 10 or 20, but if a backup usually needs only 500 KiB, the number could be increased to 100. The date does not matter, just the total amount of bytes required for the backups, as the limit is the storage media size and not the time span.

对于在每次执行备份操作时总是始终添加行的日志文件,通常必须注意日志文件的大小,而不是将行附加到同一日志文件的时间跨度,以避免该日志文件变得越来越大.使用/Y 将大小> x KiB或MiB的日志文件移动到 * _ old.log ,以覆盖已经存在的 * _ old.log ,然后将新行重定向到新的日志文件中,通常只有两个日志文件( *.log * _ old.log ),并定义了最大文件大小,其中包含最近 x 个备份操作的日志行.

And for a log file with lines always appended on each execution of a backup operation, the size of the log file must be usually observed and not the time span on which lines are appended to same log file to avoid that the log file becomes larger and larger. Moving a log file with size > x KiB or MiB to *_old.log with /Y to overwrite an already existing *_old.log and then redirect the new lines into a new log file is quite often the right strategy to have just 2 logs files (*.log and *_old.log) with a defined maximum file size containing log lines of the last x backup operations.

这篇关于在Windows 7上批处理作业以删除7天以上的文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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