I have now a batch file where all folders older then 22 days will be deleted. When the batch file is running every week there is no problem, there will Always be 4 folders in the root. But when there is holiday, the system is turned off up to 3 weeks. When the file is running after that holiday, there will be deleted 3 folders.
我现在有一个批处理文件,其中将删除超过22天的所有文件夹。当批处理文件每周运行没有问题时,根目录中总会有4个文件夹。但是当有假期时,系统会被关闭长达3周。文件在该假期后运行时,将删除3个文件夹。
I am now searching for a command where only the oldest folder will be deleted, so I will keep 4 folders for backup at all time.
我现在正在搜索一个命令,其中只删除最旧的文件夹,因此我将始终保留4个文件夹进行备份。
The folders are named like backup_YYYY-MM-DD.
这些文件夹的名称类似于backup_YYYY-MM-DD。
Thanks for looking into my question.
感谢您查看我的问题。
1 个解决方案
#1
1
This task can be easily done using following batch code with more or less a single command line:
使用以下批处理代码可以使用或多或少的单个命令行轻松完成此任务:
@echo off
for /F "skip=3 delims=" %%I in ('dir "C:\Temp\backup_*" /AD /B /O-N 2^>nul') do rd /Q /S "C:\Temp\%%I"
The command DIR returns because of /AD
just directory names in bare format because of /B
and ordered reverse by name because of /O-N
found in directory C:\Temp
starting with backup_
in name.
命令DIR返回,因为/ AD只返回裸格式的目录名,因为/ B和名称反向排序,因为在目录C:\ Temp中找到/ O-N以名称中的backup_开头。
The reverse order by name results in using date format YYYY-MM-DD
automatically also in reverse order by date. The newest backup folder is output first and the oldest folder last by command DIR.
按名称的相反顺序导致日期格式YYYY-MM-DD也按日期以相反的顺序自动使用。首先输出最新的备份文件夹,然后通过命令DIR输出最旧的文件夹。
The command FOR skips the first 3 lines of output of command DIR and therefore ignores the 3 newest backup folders. All other backup folders are processed by FOR resulting in removing them with command RD.
命令FOR跳过命令DIR的前3行输出,因此忽略3个最新的备份文件夹。所有其他备份文件夹由FOR处理,导致使用命令RD删除它们。
The command DIR outputs an error message to STDERR if it can't find any folder matching the pattern backup_*
. This error message is suppressed with 2>nul
by redirecting the error message from STDERR to device NUL. The redirection operator >
must be escaped here with ^
as otherwise Windows command interpreter would interpret >
as redirection for command FOR
positioned wrong which would result in a syntax error message.
如果DIR命令找不到与模式backup_ *匹配的任何文件夹,则命令DIR会向STDERR输出错误消息。通过将错误消息从STDERR重定向到设备NUL,可以使用2> nul抑制此错误消息。必须使用^来转义重定向运算符>,否则Windows命令解释器会将>解释为重定向命令FOR定位错误,这将导致语法错误消息。
Note: Command DIR returns here just the backup folder names without path and therefore the parent folder path must be written twice in code, on command DIR and also on command RD.
注意:命令DIR只返回没有路径的备份文件夹名称,因此父文件夹路径必须在代码,命令DIR和命令RD上写入两次。
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
要了解使用的命令及其工作方式,请打开命令提示符窗口,执行以下命令,并完全阅读为每个命令显示的所有帮助页面。
echo /?
dir /?
for /?
rd /?
And see also the Microsoft article about Using command redirection operators.
另请参阅Microsoft有关使用命令重定向运算符的文章。
#1
1
This task can be easily done using following batch code with more or less a single command line:
使用以下批处理代码可以使用或多或少的单个命令行轻松完成此任务:
@echo off
for /F "skip=3 delims=" %%I in ('dir "C:\Temp\backup_*" /AD /B /O-N 2^>nul') do rd /Q /S "C:\Temp\%%I"
The command DIR returns because of /AD
just directory names in bare format because of /B
and ordered reverse by name because of /O-N
found in directory C:\Temp
starting with backup_
in name.
命令DIR返回,因为/ AD只返回裸格式的目录名,因为/ B和名称反向排序,因为在目录C:\ Temp中找到/ O-N以名称中的backup_开头。
The reverse order by name results in using date format YYYY-MM-DD
automatically also in reverse order by date. The newest backup folder is output first and the oldest folder last by command DIR.
按名称的相反顺序导致日期格式YYYY-MM-DD也按日期以相反的顺序自动使用。首先输出最新的备份文件夹,然后通过命令DIR输出最旧的文件夹。
The command FOR skips the first 3 lines of output of command DIR and therefore ignores the 3 newest backup folders. All other backup folders are processed by FOR resulting in removing them with command RD.
命令FOR跳过命令DIR的前3行输出,因此忽略3个最新的备份文件夹。所有其他备份文件夹由FOR处理,导致使用命令RD删除它们。
The command DIR outputs an error message to STDERR if it can't find any folder matching the pattern backup_*
. This error message is suppressed with 2>nul
by redirecting the error message from STDERR to device NUL. The redirection operator >
must be escaped here with ^
as otherwise Windows command interpreter would interpret >
as redirection for command FOR
positioned wrong which would result in a syntax error message.
如果DIR命令找不到与模式backup_ *匹配的任何文件夹,则命令DIR会向STDERR输出错误消息。通过将错误消息从STDERR重定向到设备NUL,可以使用2> nul抑制此错误消息。必须使用^来转义重定向运算符>,否则Windows命令解释器会将>解释为重定向命令FOR定位错误,这将导致语法错误消息。
Note: Command DIR returns here just the backup folder names without path and therefore the parent folder path must be written twice in code, on command DIR and also on command RD.
注意:命令DIR只返回没有路径的备份文件夹名称,因此父文件夹路径必须在代码,命令DIR和命令RD上写入两次。
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
要了解使用的命令及其工作方式,请打开命令提示符窗口,执行以下命令,并完全阅读为每个命令显示的所有帮助页面。
echo /?
dir /?
for /?
rd /?
And see also the Microsoft article about Using command redirection operators.
另请参阅Microsoft有关使用命令重定向运算符的文章。