批处理文件将最新的7个文件保留在一个子文件夹中 [英] Batch file that keeps the 7 latest files in a subfolder
本文介绍了批处理文件将最新的7个文件保留在一个子文件夹中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人可以帮助我创建批处理文件吗?基本上,我的目标是创建一个批处理文件,该文件将在指定目录的所有文件夹中保留最新的7个文件(换句话说,最新的).
Can anyone help me create a batch file? Basically, my goal is to create a batch file that will keep the latest 7 files (in other words, the newest) in the all folders from specify directory.
我知道了
set file2del=
for /f "skip=7" %%A in ('dir /b/o-d') do set file2del=%%A
if not "%file2del%"=="" del "%file2del%"
但这适用于当前目录.
推荐答案
尝试一下:
首先将FOR /D
与/R
开关一起使用,以递归方式循环播放所有文件夹(开始蝙蝠的位置).并在每个目录上应用FOR /F
循环.
First use FOR /D
with the /R
Switch to recursively loop on all folder (from where the bat is started). and apply the FOR /F
loop on each directory.
@echo off
for /d /r %%a in (*) do (echo Treating Diretory ==^> %%a
for /f "skip=7" %%b in ('dir /b/o-d "%%a"') do del "%%a\%%b"
)
如果您需要更详细的信息,可以使用计数器代替SKIP=7
:
If you need something more detailled you can use a counter in place of the SKIP=7
:
@echo off
setlocal enabledelayedexpansion
for /d /r %%a in (*) do (echo Treating Diretory ==^> %%a
set /a $count=1
for /f %%b in ('dir /b/o-d "%%a"') do (
if !$count! LEQ 7 (
echo Keeping File[!$count!] ==^> %%b
set /a $count+=1
) else (echo Deleting File ==^> %%a\%%b
del "%%a\%%b")
)
)
这篇关于批处理文件将最新的7个文件保留在一个子文件夹中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文