,保持文件夹中的7最新的文件批处理文件 [英] Batch file that keeps the 7 latest files in a folder

查看:150
本文介绍了,保持文件夹中的7最新的文件批处理文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能帮我创建一个批处理文件?基本上,我的目标是创建一个批处理文件,将保留最新7 .txt文件(换言之,最新的)的文件夹中,并随后删除其余。这是,如果有多于7个文件的文件夹中。

Can anyone help me create a batch file? Basically, my goal is to create a batch file that will keep the LATEST 7 .txt files (in other words, the newest) in the folder and subsequently delete the rest. That's IF there are more than 7 files in the folder.

我有现在的问题是,我创建批处理文件删除大部分的文件,因为他们的日期是一个月或两个月左右的事实。我要始终保持最新的7档,不管他们是多么的老了。

The problem I'm having right now is the fact that the batch file that I have created deletes most of the files because their date is from a month or two or so. I want to keep the latest 7 files at all times no matter how old they are.

所以这就是我 -

@echo off

setlocal enableextensions

rem ********************************************************************************
rem *******************************  LOCAL VARIABLES  ******************************
rem ********************************************************************************

SET TargetDirectory="C:\TEMP\test"

SET No_of_fles_to_keep=7

SET count=0 

set cnt=0

rem ********************************************************************************

cd /d %TargetDirectory%

REM timeout /T 500

 for %%x in (*) do set /a count+=1

 for %%A in (*.bat) do set /a cnt+=1

cd /d %TargetDirectory%

REM timeout /T 500

IF %count% gtr %No_of_fles_to_keep% forfiles -p %TargetDirectory% -s -m "*.txt" -d -%No_of_fles_to_keep% -c "cmd /c del @path"

echo %count% 

echo File count = %cnt% 

任何帮助是AP preciated。

Any help is appreciated.

推荐答案

您可以使用DIR与 / O-D 列出按降序时间戳的顺序文本文件。 FOR / F允许您遍历每个文件。 SET / A用来跟踪多少文件迄今已列出。现在到了棘手的部分。

You can use DIR with /O-D to list the text files in descending timestamp order. FOR /F allows you to iterate over each file. SET /A is used to keep track of how many files have been listed so far. Now comes the tricky part.

在您通常需要使用延迟扩展与先前在同一个块设置一个变量的值要工作code座。但推迟扩张可能会导致问题在for循环中,如果FOR变量值包含在文件中有效名。我避开这个问题,通过使用SET / A时,7号文件的名称已被读取故意除以0。这就提出了导致有条件code来执行取消定义的变量KEEP一个错误。从这一点上,所有剩余的文件被删除。

Within a code block you normally need to use delayed expansion to work with the value of a variable that was set earlier in the same block. But delayed expansion can cause problems in a FOR loop if the FOR variable value contains !, and ! is valid in file names. I get around the problem by using SET /A to intentionally divide by 0 when the 7th file name has been read. This raises an error that causes the conditional code to execute that undefines the KEEP variable. From that point on, all remaining files are deleted.

@echo off
setlocal
set /a cnt=0
set "keep=7"
for /f "eol=: delims=" %%F in ('dir /b /o-d *.txt') do (
  if defined keep (
    2>nul set /a "cnt+=1, 1/(keep-cnt)" || set "keep="
  ) else del "%%F"
)

更新

Update

我的天哪,我才意识到有一个简单的解决方案。只需使用FOR / F SKIP选项按最后修改日期排​​序,降后忽略第7项。

Oh my goodness, I just realized there is a trivial solution. Just use the FOR /F SKIP option to ignore the first 7 entries after sorting by last modified date, descending.

for /f "skip=7 eol=: delims=" %%F in ('dir /b /o-d *.txt') do @del "%%F"

您甚至不需要一个批处理文件。只要改变 %% 如果从命令提示符下运行。

You don't even need a batch file. Just change %% to % if run from the command prompt.

这篇关于,保持文件夹中的7最新的文件批处理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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