批处理文件以删除除最新的10个文件以外的所有文件夹,其中有许多文件名 [英] batch file to remove all but the newest 10 files folder has numerous file names

查看:88
本文介绍了批处理文件以删除除最新的10个文件以外的所有文件夹,其中有许多文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个存放日志文件的文件夹.我们有许多以名称创建日志文件的过程.示例如下.我想保留每个进程的最后10个日志文件(日志文件名),并删除其他任何文件.我已经看到一些与此相关的帖子( batch文件已删除除了最新的10个文件),但它们删除了除最后10个文件之外的所有文件.我需要最后10个不同的命名文件.

I have a folder that houses our log files. We have numerous processes that create the log files in it's name. Examples are below. I would like to keep the last 10 log files for each process (log file name) and delete any others. I have seen some posts that are close to this (batch file remove all but the newest 10 files), but they delete all but the last 10 total files. I need the last 10 of the different named files.

rm_list_mover_20170510050901
rm_list_mover_20170427050901
rm_list_mover_20170515050902
rm_list_mover_20170514050901
jb_jump_server_20170514050901
jb_jump_server_20170515050902
jb_jump_server_20170514050901
jb_jump_server_20170510050901
lx_star_power_20170510050901
lx_star_power_20170414050902
lx_star_power_20170514050901
lx_star_power_20170515050902

我对此完全陌生,在此先感谢您的帮助.

I'm completely new to this and thanks in advance for any help.

推荐答案

尽管您没有做出自己的努力,但我决定提供一个脚本来满足您的要求,因为我觉得它很有挑战性.所以这里是:

Although you did not show any own efforts, I decided to provide a script that fulfils your request as I found it challenging; so here it is:

@echo off
setlocal EnableExtensions EnableDelayedExpansion

rem // Define constants here:
set "_LOCATION=."
set "_PATTERN=*_??????????????"
set "_EXTENSION=.log"
set /A "_KEEP=2"

rem // Retrieve unique file name prefixes (with date/time stamp truncated):
if defined _EXTENSION (set "EXT=!_EXTENSION!") else set "EXT=."
for /F "delims= eol=|" %%F in ('dir /B /O:-N "!_LOCATION!\!_PATTERN!!EXT!"') do (
    set "FILE=%%F" & set "NAME=_" & set "ITEM="
    for %%N in ("!FILE:_=" "!") do (
        set "NAME=!NAME!!ITEM!_" & set "ITEM=%%~N"
    )
    set "NAME=!NAME:~2!"
    if defined NAME set "$UNIQUE[!NAME!]=#"
)
rem // Delete all but last number of files per unique file name prefix:
if %_KEEP% GTR 0 (set "SKIP=skip=%_KEEP%") else set "SKIP="
for /F "tokens=2 delims=[=] eol==" %%L in ('2^> nul set $UNIQUE[') do (
    for /F "%SKIP% delims= eol=|" %%E in ('dir /B /O:-N "!_LOCATION!\%%L*!EXT!"') do (
        ECHO erase "%%E"
    )
)

endlocal
exit /B

在确认要删除正确的文件后,删除erase命令行前面的大写ECHO命令

After having verified that the correct files are about to be deleted, remove the upper-case ECHO command in front of the erase command line!

我假设日志文件的文件扩展名为.log.该脚本可以处理文件名中带有任何下划线_的文件.但是,如果任何文件名包含以下任何字符,则可能会失败:!^=[].

I assumed the log files to have got the file extension .log. The script can handle files with any number of underscores _ in their file names. However, it may fail in case any of the file names contain any of the following characters: !, ^, =, [ and ].

这篇关于批处理文件以删除除最新的10个文件以外的所有文件夹,其中有许多文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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