创建文件夹结构/树

创建文件夹结构/树

我在一个目录(带有子目录)中有一些文件,这些文件以相同的名称开头,但附加了不同的字母。我想保留文件大小最大的版本。

文件看起来像

AAA_CAD.cvs     //   Size: 01MB
AAA_PDA.cvs     //   Size: 05MB
AAA_DEF3.cvs    //   Size: 02MB
AAA_DAG.cvs     //   Size: 03MB
...
BBB_CAD.cvs     //   Size: 10MB
BBB_PDA.cvs     //   Size: 05MB
BBB_DEF3.cvs    //   Size: 02MB
BBB_DAG.cvs     //   Size: 13MB

我只想保留最大尺寸的版本:

AAA_PDA.cvs     //   Size: 05MB
BBB_DAG.cvs     //   Size: 13MB

答案1

有一个程序可以帮助你解决这个问题。

杜佩古鲁是一款免费的开源软件,用于查找重复文件。您可以使用它来搜索重复文件,并根据文件大小、日期等选择要保存的内容

答案2

在 PowerShell 中你可以执行以下操作:

Get-ChildItem -Path 'X:\TheDirectory' -Filter '*.cvs' -File -Recurse |
    # group the files on the part of the name before the first underscore
    Group-Object @{expression = {($_.Name -split '_')[0]}} | ForEach-Object {
    # sort the items in the group on Length; skip the last one (the largest file)
    $_.Group | Sort-Object Length | Select-Object -SkipLast 1 | 
        # and remove the rest
        ForEach-Object { $_ | Remove-Item -WhatIf }
}

-WhatIf如果您满意控制台中的信息显示正确的文件将被删除,请移除安全开关。

答案3

@echo off

cd/d "X:\Root Dir"||goto :eof 
setlocal EnableDelayedExpansion 

for /f tokens^=* %%G in ('where /r .\ *_*.cvs /t'
    )do set "_n=%%~nG" & call %:^) "!_n!" "_!_n:*_=!"

%:^)
if not "%~1" == "" (set "_file=%~1" && (for /f skip^=1^tokens^=1^,4* %%I in (
    'where /r .\ !_file:%~2^=!_*.cvs /t^|sort /r')do del /q /f /a: "%%K") & exit /b 
    )else endlocal && goto :eOf

我理解,如果没有事先进行适当的备份就尝试执行此类操作,可能会导致不可逆转的错误。

我也明白,如果对所用工具的行为没有太多的了解/熟悉,那么尝试这样的任务可能会导致一些不好的(意外的)结果。


考虑到这一点,下面是一种创建测试场景的方法,不会将真实环境暴露于不可逆转的损失的风险,并在将其应用于所涉及的真实文件/文件夹之前对结果进行安全评估。

  • 范围概括如下:

    • 创建文件夹结构/树

    • 在树中创建不同大小的文件

    • 检查预设文件较大的命令的结果

    • 应用命令删除文件,保留最大的文件

    • 检查结果并查看是否保留了其他文件(唯一)


它旨在与按照此思路阐述/建议的内容类似回答


创建文件夹结构/树

for %%i in (1,2,3,4)do for %%I in (1,2,3,4
    )do 2>nul md "%~dp0\Root Dir\Dir 00%%i\Sub Dir 00%%I"

创建的文件夹树结果为:

> Tree /A 
Folder PATH listing
Volume serial number is 9A8E-862E
X:.
\---Root Dir
    +---Dir 001
    |   +---Sub Dir 001
    |   +---Sub Dir 002
    |   +---Sub Dir 003
    |   \---Sub Dir 004
    +---Dir 002
    |   +---Sub Dir 001
    |   +---Sub Dir 002
    |   +---Sub Dir 003
    |   \---Sub Dir 004
    +---Dir 003
    |   +---Sub Dir 001
    |   +---Sub Dir 002
    |   +---Sub Dir 003
    |   \---Sub Dir 004
    \---Dir 004
        +---Sub Dir 001
        +---Sub Dir 002
        +---Sub Dir 003
        \---Sub Dir 004

在树中创建不同大小的文件

fsutil file createnew "X:\Root Dir\AAA_ABC.cvs" 11326000
fsutil file createnew "X:\Root Dir\BBB_ABC.cvs"  9506000
fsutil file createnew "X:\Root Dir\CCC_ABC.cvs" 10690000
fsutil file createnew "X:\Root Dir\DDD_ABC.cvs" 14995000
fsutil file createnew "X:\Root Dir\EEE_ABC.cvs"  7431000
fsutil file createnew "X:\Root Dir\A00_ABC.cvs"  7131000
fsutil file createnew "X:\Root Dir\Dir 001\AAA_AAC.cvs" 11964000
fsutil file createnew "X:\Root Dir\Dir 001\BBB_APD.cvs"  3307000
fsutil file createnew "X:\Root Dir\Dir 001\CCC_AED.cvs"  3878000
fsutil file createnew "X:\Root Dir\Dir 001\DDD_AAD.cvs" 13346000
fsutil file createnew "X:\Root Dir\Dir 001\EEE_ART.cvs" 11001000
fsutil file createnew "X:\Root Dir\Dir 001\B00_ART.cvs" 14115000
fsutil file createnew "X:\Root Dir\Dir 002\AAA_ADC.cvs"  9417000
fsutil file createnew "X:\Root Dir\Dir 002\BBB_AAD.cvs"  6965000
fsutil file createnew "X:\Root Dir\Dir 002\CCC_ADF.cvs"  6282000
fsutil file createnew "X:\Root Dir\Dir 002\DDD_AGD.cvs" 13628000
fsutil file createnew "X:\Root Dir\Dir 002\EEE_AXT.cvs"  6987000
fsutil file createnew "X:\Root Dir\Dir 002\C00_AXT.cvs" 12587000
fsutil file createnew "X:\Root Dir\Dir 003\AAA_ADA.cvs" 10995000
fsutil file createnew "X:\Root Dir\Dir 003\BBB_AAP.cvs" 11602000
fsutil file createnew "X:\Root Dir\Dir 003\CCC_ADE.cvs" 15938000
fsutil file createnew "X:\Root Dir\Dir 003\DDD_AGA.cvs" 13556000
fsutil file createnew "X:\Root Dir\Dir 003\EEE_ARR.cvs"  2152000
fsutil file createnew "X:\Root Dir\Dir 003\D00_ARR.cvs"  2651000
fsutil file createnew "X:\Root Dir\Dir 004\AAA_ACD.cvs"  4326000
fsutil file createnew "X:\Root Dir\Dir 004\BBB_ADP.cvs" 13552000
fsutil file createnew "X:\Root Dir\Dir 004\CCC_AFD.cvs"  7527000
fsutil file createnew "X:\Root Dir\Dir 004\DDD_ADA.cvs"  5043000
fsutil file createnew "X:\Root Dir\Dir 004\EEE_AXR.cvs"  2652000
fsutil file createnew "X:\Root Dir\Dir 004\E00_AXR.cvs"  3552000
fsutil file createnew "X:\Root Dir\Dir 001\Sub Dir 001\AAA_BCC.cvs"  4915000
fsutil file createnew "X:\Root Dir\Dir 001\Sub Dir 001\BBB_BAA.cvs"  4621000
fsutil file createnew "X:\Root Dir\Dir 001\Sub Dir 001\CCC_BFF.cvs" 15352000
fsutil file createnew "X:\Root Dir\Dir 001\Sub Dir 001\DDD_BDD.cvs"  1902000
fsutil file createnew "X:\Root Dir\Dir 001\Sub Dir 001\EEE_BYR.cvs"  4001000
fsutil file createnew "X:\Root Dir\Dir 001\Sub Dir 001\F00_BYR.cvs" 11893000
fsutil file createnew "X:\Root Dir\Dir 001\Sub Dir 002\AAA_BDD.cvs"  1778000
fsutil file createnew "X:\Root Dir\Dir 001\Sub Dir 002\BBB_BPP.cvs" 10919000
fsutil file createnew "X:\Root Dir\Dir 001\Sub Dir 002\CCC_BEE.cvs"  1031000
fsutil file createnew "X:\Root Dir\Dir 001\Sub Dir 002\DDD_BDG.cvs" 18769000
fsutil file createnew "X:\Root Dir\Dir 001\Sub Dir 002\EEE_BRY.cvs" 11401000
fsutil file createnew "X:\Root Dir\Dir 001\Sub Dir 002\G00_BRY.cvs" 10582000
fsutil file createnew "X:\Root Dir\Dir 001\Sub Dir 003\AAA_BAC.cvs" 11060000
fsutil file createnew "X:\Root Dir\Dir 001\Sub Dir 003\BBB_BAP.cvs"  5780000
fsutil file createnew "X:\Root Dir\Dir 001\Sub Dir 003\CCC_BDD.cvs"  2904000
fsutil file createnew "X:\Root Dir\Dir 001\Sub Dir 003\DDD_BGD.cvs" 15582000
fsutil file createnew "X:\Root Dir\Dir 001\Sub Dir 003\EEE_BYT.cvs" 17365000
fsutil file createnew "X:\Root Dir\Dir 001\Sub Dir 004\H00_BAA.cvs"  4123000
fsutil file createnew "X:\Root Dir\Dir 001\Sub Dir 004\AAA_BAA.cvs" 10044000
fsutil file createnew "X:\Root Dir\Dir 001\Sub Dir 004\BBB_BDA.cvs"  1124000
fsutil file createnew "X:\Root Dir\Dir 001\Sub Dir 004\CCC_BDD.cvs"  6117000
fsutil file createnew "X:\Root Dir\Dir 001\Sub Dir 004\DDD_BGG.cvs" 16101000
fsutil file createnew "X:\Root Dir\Dir 001\Sub Dir 004\EEE_BTT.cvs"  7312000
fsutil file createnew "X:\Root Dir\Dir 001\Sub Dir 004\I00_BTT.cvs"  8271000
fsutil file createnew "X:\Root Dir\Dir 002\Sub Dir 001\AAA_CXC.cvs"  1250000
fsutil file createnew "X:\Root Dir\Dir 002\Sub Dir 001\J00_CXC.cvs"  2999000
fsutil file createnew "X:\Root Dir\Dir 002\Sub Dir 001\BBB_CAA.cvs"  1264000
fsutil file createnew "X:\Root Dir\Dir 002\Sub Dir 001\CCC_CFF.cvs"  5532000
fsutil file createnew "X:\Root Dir\Dir 002\Sub Dir 001\DDD_CDD.cvs"  1198000
fsutil file createnew "X:\Root Dir\Dir 002\Sub Dir 001\EEE_CYR.cvs"  3203000
fsutil file createnew "X:\Root Dir\Dir 002\Sub Dir 001\K00_CYR.cvs"  3117000
fsutil file createnew "X:\Root Dir\Dir 002\Sub Dir 002\AAA_CXD.cvs"  3008000
fsutil file createnew "X:\Root Dir\Dir 002\Sub Dir 002\BBB_CPP.cvs" 10084000
fsutil file createnew "X:\Root Dir\Dir 002\Sub Dir 002\CCC_CEE.cvs"  1005000
fsutil file createnew "X:\Root Dir\Dir 002\Sub Dir 002\DDD_CDG.cvs"  8697000
fsutil file createnew "X:\Root Dir\Dir 002\Sub Dir 002\EEE_CRY.cvs" 11099000
fsutil file createnew "X:\Root Dir\Dir 002\Sub Dir 002\L00_CRY.cvs" 10310000
fsutil file createnew "X:\Root Dir\Dir 002\Sub Dir 003\AAA_CAX.cvs"  9005000
fsutil file createnew "X:\Root Dir\Dir 002\Sub Dir 003\BBB_CAP.cvs"  1178000
fsutil file createnew "X:\Root Dir\Dir 002\Sub Dir 003\CCC_CDD.cvs"  3655000
fsutil file createnew "X:\Root Dir\Dir 002\Sub Dir 003\DDD_CGD.cvs"  5530000
fsutil file createnew "X:\Root Dir\Dir 002\Sub Dir 003\EEE_CYT.cvs"  2900000
fsutil file createnew "X:\Root Dir\Dir 002\Sub Dir 003\M00_CYT.cvs"  2009000
fsutil file createnew "X:\Root Dir\Dir 002\Sub Dir 004\AAA_CAX.cvs"  4101000
fsutil file createnew "X:\Root Dir\Dir 002\Sub Dir 004\BBB_CDA.cvs"  2100000
fsutil file createnew "X:\Root Dir\Dir 002\Sub Dir 004\CCC_CDD.cvs"  3600000
fsutil file createnew "X:\Root Dir\Dir 002\Sub Dir 004\DDD_CGG.cvs" 15999000
fsutil file createnew "X:\Root Dir\Dir 002\Sub Dir 004\EEE_CTT.cvs"  3123000
fsutil file createnew "X:\Root Dir\Dir 002\Sub Dir 004\N00_CTT.cvs"  3923000
fsutil file createnew "X:\Root Dir\Dir 003\Sub Dir 001\AAA_DAC.cvs"  1194000
fsutil file createnew "X:\Root Dir\Dir 003\Sub Dir 001\BBB_DAA.cvs"  7017000
fsutil file createnew "X:\Root Dir\Dir 003\Sub Dir 001\CCC_DXF.cvs"  8352000
fsutil file createnew "X:\Root Dir\Dir 003\Sub Dir 001\DDD_DDD.cvs"  1199000
fsutil file createnew "X:\Root Dir\Dir 003\Sub Dir 001\EEE_DXR.cvs"  4105000
fsutil file createnew "X:\Root Dir\Dir 003\Sub Dir 001\O00_DXR.cvs"  2504000
fsutil file createnew "X:\Root Dir\Dir 003\Sub Dir 002\AAA_DXD.cvs"  3558000
fsutil file createnew "X:\Root Dir\Dir 003\Sub Dir 002\BBB_DXP.cvs" 12284000
fsutil file createnew "X:\Root Dir\Dir 003\Sub Dir 002\CCC_DXE.cvs"  2811000
fsutil file createnew "X:\Root Dir\Dir 003\Sub Dir 002\DDD_DXG.cvs"  1976000
fsutil file createnew "X:\Root Dir\Dir 003\Sub Dir 002\EEE_DXY.cvs"  5132000
fsutil file createnew "X:\Root Dir\Dir 003\Sub Dir 002\P00_DXY.cvs"  7801000
fsutil file createnew "X:\Root Dir\Dir 003\Sub Dir 003\AAA_DXC.cvs"  2021000
fsutil file createnew "X:\Root Dir\Dir 003\Sub Dir 003\BBB_DXP.cvs" 10209000
fsutil file createnew "X:\Root Dir\Dir 003\Sub Dir 003\CCC_DXD.cvs"  2104000
fsutil file createnew "X:\Root Dir\Dir 003\Sub Dir 003\DDD_DXD.cvs" 18584000
fsutil file createnew "X:\Root Dir\Dir 003\Sub Dir 003\EEE_DXT.cvs" 50111000
fsutil file createnew "X:\Root Dir\Dir 003\Sub Dir 003\Q00_DXT.cvs"  2150000
fsutil file createnew "X:\Root Dir\Dir 003\Sub Dir 004\AAA_DXA.cvs" 14133000
fsutil file createnew "X:\Root Dir\Dir 003\Sub Dir 004\BBB_DXA.cvs"  3284000
fsutil file createnew "X:\Root Dir\Dir 003\Sub Dir 004\CCC_DDD.cvs"  8867000
fsutil file createnew "X:\Root Dir\Dir 003\Sub Dir 004\DDD_DGG.cvs"  5911100
fsutil file createnew "X:\Root Dir\Dir 003\Sub Dir 004\EEE_DTT.cvs"  7130000
fsutil file createnew "X:\Root Dir\Dir 003\Sub Dir 004\R00_DTT.cvs"  9991000
fsutil file createnew "X:\Root Dir\Dir 004\Sub Dir 001\AAA_ECC.cvs"  1491000
fsutil file createnew "X:\Root Dir\Dir 004\Sub Dir 001\BBB_EAA.cvs"  6209000
fsutil file createnew "X:\Root Dir\Dir 004\Sub Dir 001\CCC_EFF.cvs" 11355000
fsutil file createnew "X:\Root Dir\Dir 004\Sub Dir 001\DDD_EDD.cvs"  8911000
fsutil file createnew "X:\Root Dir\Dir 004\Sub Dir 001\EEE_EYR.cvs"  1125000
fsutil file createnew "X:\Root Dir\Dir 004\Sub Dir 001\S00_EYR.cvs"  1893000
fsutil file createnew "X:\Root Dir\Dir 004\Sub Dir 002\AAA_EDD.cvs"  3778000
fsutil file createnew "X:\Root Dir\Dir 004\Sub Dir 002\BBB_EPP.cvs" 10704000
fsutil file createnew "X:\Root Dir\Dir 004\Sub Dir 002\CCC_EEE.cvs"  8254000
fsutil file createnew "X:\Root Dir\Dir 004\Sub Dir 002\DDD_EDG.cvs" 11769000
fsutil file createnew "X:\Root Dir\Dir 004\Sub Dir 002\EEE_ERY.cvs"  9997000
fsutil file createnew "X:\Root Dir\Dir 004\Sub Dir 002\T00_ERY.cvs"  9814000
fsutil file createnew "X:\Root Dir\Dir 004\Sub Dir 003\AAA_EAC.cvs" 13251000
fsutil file createnew "X:\Root Dir\Dir 004\Sub Dir 003\BBB_EAP.cvs"  7811000
fsutil file createnew "X:\Root Dir\Dir 004\Sub Dir 003\CCC_EDD.cvs" 12904000
fsutil file createnew "X:\Root Dir\Dir 004\Sub Dir 003\DDD_EGD.cvs"  4312000
fsutil file createnew "X:\Root Dir\Dir 004\Sub Dir 003\EEE_EYT.cvs"  8021000
fsutil file createnew "X:\Root Dir\Dir 004\Sub Dir 003\U00_EYT.cvs" 10999000
fsutil file createnew "X:\Root Dir\Dir 004\Sub Dir 004\AAA_EAA.cvs" 11150000
fsutil file createnew "X:\Root Dir\Dir 004\Sub Dir 004\BBB_EDA.cvs"  2484000
fsutil file createnew "X:\Root Dir\Dir 004\Sub Dir 004\CCC_EDD.cvs"  6167000
fsutil file createnew "X:\Root Dir\Dir 004\Sub Dir 004\DDD_EGG.cvs" 15900000
fsutil file createnew "X:\Root Dir\Dir 004\Sub Dir 004\EEE_ETT.cvs"  8540000
fsutil file createnew "X:\Root Dir\Dir 004\Sub Dir 004\V00_ETT.cvs"  7310000

创建的文件夹和文件树的结果如下:

> Tree /F /A
Folder PATH listing
Volume serial number is 9A8E-862E
X:.
|   Create_Files.cmd
|   Q1672774.cmd
|   
\---Root Dir
    |   A00_ABC.cvs
    |   AAA_ABC.cvs
    |   BBB_ABC.cvs
    |   CCC_ABC.cvs
    |   DDD_ABC.cvs
    |   EEE_ABC.cvs
    |   
    +---Dir 001
    |   |   AAA_AAC.cvs
    |   |   B00_ART.cvs
    |   |   BBB_APD.cvs
    |   |   CCC_AED.cvs
    |   |   DDD_AAD.cvs
    |   |   EEE_ART.cvs
    |   |   
    |   +---Sub Dir 001
    |   |       AAA_BCC.cvs
    |   |       BBB_BAA.cvs
    |   |       CCC_BFF.cvs
    |   |       DDD_BDD.cvs
    |   |       EEE_BYR.cvs
    |   |       F00_BYR.cvs
    |   |       
    |   +---Sub Dir 002
    |   |       AAA_BDD.cvs
    |   |       BBB_BPP.cvs
    |   |       CCC_BEE.cvs
    |   |       DDD_BDG.cvs
    |   |       EEE_BRY.cvs
    |   |       G00_BRY.cvs
    |   |       
    |   +---Sub Dir 003
    |   |       AAA_BAC.cvs
    |   |       BBB_BAP.cvs
    |   |       CCC_BDD.cvs
    |   |       DDD_BGD.cvs
    |   |       EEE_BYT.cvs
    |   |       
    |   \---Sub Dir 004
    |           AAA_BAA.cvs
    |           BBB_BDA.cvs
    |           CCC_BDD.cvs
    |           DDD_BGG.cvs
    |           EEE_BTT.cvs
    |           H00_BAA.cvs
    |           I00_BTT.cvs
    |           
    +---Dir 002
    |   |   AAA_ADC.cvs
    |   |   BBB_AAD.cvs
    |   |   C00_AXT.cvs
    |   |   CCC_ADF.cvs
    |   |   DDD_AGD.cvs
    |   |   EEE_AXT.cvs
    |   |   
    |   +---Sub Dir 001
    |   |       AAA_CXC.cvs
    |   |       BBB_CAA.cvs
    |   |       CCC_CFF.cvs
    |   |       DDD_CDD.cvs
    |   |       EEE_CYR.cvs
    |   |       J00_CXC.cvs
    |   |       K00_CYR.cvs
    |   |       
    |   +---Sub Dir 002
    |   |       AAA_CXD.cvs
    |   |       BBB_CPP.cvs
    |   |       CCC_CEE.cvs
    |   |       DDD_CDG.cvs
    |   |       EEE_CRY.cvs
    |   |       L00_CRY.cvs
    |   |       
    |   +---Sub Dir 003
    |   |       AAA_CAX.cvs
    |   |       BBB_CAP.cvs
    |   |       CCC_CDD.cvs
    |   |       DDD_CGD.cvs
    |   |       EEE_CYT.cvs
    |   |       M00_CYT.cvs
    |   |       
    |   \---Sub Dir 004
    |           AAA_CAX.cvs
    |           BBB_CDA.cvs
    |           CCC_CDD.cvs
    |           DDD_CGG.cvs
    |           EEE_CTT.cvs
    |           N00_CTT.cvs
    |           
    +---Dir 003
    |   |   AAA_ADA.cvs
    |   |   BBB_AAP.cvs
    |   |   CCC_ADE.cvs
    |   |   D00_ARR.cvs
    |   |   DDD_AGA.cvs
    |   |   EEE_ARR.cvs
    |   |   
    |   +---Sub Dir 001
    |   |       AAA_DAC.cvs
    |   |       BBB_DAA.cvs
    |   |       CCC_DXF.cvs
    |   |       DDD_DDD.cvs
    |   |       EEE_DXR.cvs
    |   |       O00_DXR.cvs
    |   |       
    |   +---Sub Dir 002
    |   |       AAA_DXD.cvs
    |   |       BBB_DXP.cvs
    |   |       CCC_DXE.cvs
    |   |       DDD_DXG.cvs
    |   |       EEE_DXY.cvs
    |   |       P00_DXY.cvs
    |   |       
    |   +---Sub Dir 003
    |   |       AAA_DXC.cvs
    |   |       BBB_DXP.cvs
    |   |       CCC_DXD.cvs
    |   |       DDD_DXD.cvs
    |   |       EEE_DXT.cvs
    |   |       Q00_DXT.cvs
    |   |       
    |   \---Sub Dir 004
    |           AAA_DXA.cvs
    |           BBB_DXA.cvs
    |           CCC_DDD.cvs
    |           DDD_DGG.cvs
    |           EEE_DTT.cvs
    |           R00_DTT.cvs
    |           
    \---Dir 004
        |   AAA_ACD.cvs
        |   BBB_ADP.cvs
        |   CCC_AFD.cvs
        |   DDD_ADA.cvs
        |   E00_AXR.cvs
        |   EEE_AXR.cvs
        |   
        +---Sub Dir 001
        |       AAA_ECC.cvs
        |       BBB_EAA.cvs
        |       CCC_EFF.cvs
        |       DDD_EDD.cvs
        |       EEE_EYR.cvs
        |       S00_EYR.cvs
        |       
        +---Sub Dir 002
        |       AAA_EDD.cvs
        |       BBB_EPP.cvs
        |       CCC_EEE.cvs
        |       DDD_EDG.cvs
        |       EEE_ERY.cvs
        |       T00_ERY.cvs
        |       
        +---Sub Dir 003
        |       AAA_EAC.cvs
        |       BBB_EAP.cvs
        |       CCC_EDD.cvs
        |       DDD_EGD.cvs
        |       EEE_EYT.cvs
        |       U00_EYT.cvs
        |       
        \---Sub Dir 004
                AAA_EAA.cvs
                BBB_EDA.cvs
                CCC_EDD.cvs
                DDD_EGG.cvs
                EEE_ETT.cvs
                V00_ETT.cvs

检查预设文件较大的命令的结果

较大的文件是:AAA_DXA BBB_ADP CCC_ADE DDD_BDG EEE_DXT

获取并检查命令中的输出,最大的文件作为按组在输出中列出的第一个项目(AAA_??? - EEE_???)。

> for %G in (A,B,C,D,E)do @echo\File %G%G%G & @for /f tokens^=* %I in ('where /r . %G%G%G_*.cvs /t ^| sort /r')do @echo\%I

where与 一起使用的命令会/t生成一个输出,该输出以文件大小作为起始行,然后重定向到sort /R,会将这些行从最大到最小反转。

因此,无论它是在当前文件夹还是子文件夹中,第一行始终包含最大的文件。

File AAA
14133000    9/4/2021   10:51:41 AM  X:\Root Dir\Dir 003\Sub Dir 004\AAA_DXA.cvs
     ...
 1194000    9/4/2021   10:51:40 AM  X:\Root Dir\Dir 003\Sub Dir 001\AAA_DAC.cvs 

File BBB
13552000    9/4/2021   10:51:38 AM  X:\Root Dir\Dir 004\BBB_ADP.cvs 
     ...
 1124000    9/4/2021   10:51:39 AM  X:\Root Dir\Dir 001\Sub Dir 004\BBB_BDA.cvs

File CCC
15938000    9/4/2021   10:51:38 AM  X:\Root Dir\Dir 003\CCC_ADE.cvs
     ...
 1005000    9/4/2021   10:51:40 AM  X:\Root Dir\Dir 002\Sub Dir 002\CCC_CEE.cvs 

File DDD
18769000    9/4/2021   10:51:39 AM  X:\Root Dir\Dir 001\Sub Dir 002\DDD_BDG.cvs 
     ...
 1198000    9/4/2021   10:51:40 AM  X:\Root Dir\Dir 002\Sub Dir 001\DDD_CDD.cvs

File EEE
50111000    9/4/2021   10:51:41 AM  X:\Root Dir\Dir 003\Sub Dir 003\EEE_DXT.cvs
     ...
 1125000    9/4/2021   10:51:41 AM  X:\Root Dir\Dir 004\Sub Dir 001\EEE_EYR.cvs

应用命令删除文件,保留最大的文件

由于在for /F循环中可以定义要跳过/忽略的命令的输出编号,因此添加skip=1,这将导致循环内针对小于最大文件的文件的操作是一个或多个文件,所有文件都将达到。

for /f skip^=1^tokens^=1^,4* %I in (
    'where /r .\ String_*.cvs /t^|sort /r')do del /q /f /a: "%K"

还适用Skip=1于那些不符合其重复性的文件,并在循环中被忽略/跳过:A00_ABC.cvs - V00_ETT.cvs

观察:对于文件大小甚至位于不同文件夹中的情况,where /r .\ file.eXt /t |sort /r将首先列出最新的结果

在执行循环删除之前,可以量化每个组和文件总数

> dir /b /s /a-d|findstr \\AAA_...\...|find /v /c ""
21
> dir /b /s /a-d|findstr \\BBB_...\...|find /v /c ""
21
> dir /b /s /a-d|findstr \\CCC_...\...|find /v /c ""
21
> dir /b /s /a-d|findstr \\DDD_...\...|find /v /c ""
21
> dir /b /s /a-d|findstr \\EEE_...\...|find /v /c ""
21
> dir /b /s /a-d|findstr \\[A-V][0][0]\_.*|find /v /c ""
22
> dir /b /s /a-d *_*.cvs|find /v /c ""
127

::      AAA_???.cvs = 21
::      BBB_???.cvs = 21
::      CCC_???.cvs = 21 
::      DDD_???.cvs = 21 
::      EEE_???.cvs = 21 
::   A-Z+00_???.cvs = 22  // single/unique files for test and kept //
::      ???_???.cvs = 127

Bat 使用循环应用 String_*.cvs 组中的文件列表并删除较小的文件,以便仅保留每个组中最大的文件并保持文件的唯一性(如果存在则没有组)

@echo off

cd/d "A:\Directory"||goto :eof 
setlocal EnableDelayedExpansion 

for /f tokens^=* %%G in ('where /r .\ *_*.cvs /t'
    )do set "_n=%%~nG" & call %:^) "!_n!" "_!_n:*_=!"

%:^)
if not "%~1" == "" (set "_file=%~1" && (for /f skip^=1^tokens^=1^,4* %%I in (
    'where /r .\ !_file:%~2^=!_*.cvs /t^|sort /r')do del /q /f /a: "%%K") & exit /b 
    )else endlocal && goto :eOf

检查是否保留了具有相同扩展名和唯一性的其他文件

> for %G in (A,B,C,D,E)do @echo\File %G%G%G & @for /f tokens^=* %I in ('where /r . %G%G%G_*.cvs /t ^| sort /r')do @echo\%I

File AAA
14133000    9/4/2021   10:51:41 AM  X:\Root Dir\Dir 003\Sub Dir 004\AAA_DXA.cvs
File BBB
13552000    9/4/2021   10:51:38 AM  X:\Root Dir\Dir 004\BBB_ADP.cvs
File CCC
15938000    9/4/2021   10:51:38 AM  X:\Root Dir\Dir 003\CCC_ADE.cvs
File DDD
18769000    9/4/2021   10:51:39 AM  X:\Root Dir\Dir 001\Sub Dir 002\DDD_BDG.cvs
File EEE
50111000    9/4/2021   10:51:41 AM  X:\Root Dir\Dir 003\Sub Dir 003\EEE_DXT.cvs

> dir /b /s /a-d|findstr \\[A-V][0][0]\_.*|find /v /c ""
22

解释蝙蝠在做什么......

转到目标/根文件夹,如果不在那里,则中止 bat 文件而不执行下面/下一步的任何命令:

cd/d "X:\Root Dir"||转到:eof

where循环中的命令将递归列出名称为 AnyString_AnyString.cvs 的布局文件()/r

for /f tokens^=* %%G in ('where /r .\ *_*.cvs /t'
    )do ...

%%~nG该集合保存了文件的名称(不带扩展名“ ”),并且在同一行上,bat 调用函数(标签%:^)),将名称(!_n!)和名称中下划线之前(以及带有下划线)的字符串作为参数传递。

例如:文件循环AAA_XYZ.cvs

论点 1 =AAA_XYZ
论点 2 =XYZ

例如:文件循环%%~G

论点 1 =!_n!
论点 2 =_!_n:*_=!

观察:参数中的子字符串%2是:AAA_XYZ == > AAA_XYZ == > XYZ

 ... set "_n=%%~nG" & call %:^) "!_n!" "_!_n:*_=!"

如果传递了任何参数,则在第一个if(%1 == true)条件中使用它们,否则,如果没有传递任何参数,则结束 bat,函数不再需要处理循环文件,因此转到文件末尾,结束

if not "%~1" == "" (do some in loop files) else (endlocal && goto :eOf)

一旦函数内发生处理,就用参数中给出的文件名定义 _file%1

set "_file=%~1" ... && (....

我们将利用在循环中执行 where 所返回的字符串,以空格分隔的形式(默认)出现 1 次,并且在第 4 次之后的所有出现(4*)中跳过一个(skipˆ=1),其中跳过的项目是我们最大的文件(故意忽略)...

for /f skip^=1^tokens^=1^,4* %%I in (...
::      %%I    |--- Not In Use --| %%J                    %%K ==  *
::  1194000    9/4/2021   10:51:40  AM  X:\Full\Path\To\AAA_DAC.cvs 

::  %%I  size from file in loop 
::  %%J  just to have all strings after %%J in %%K 
::  %%K  all strings after %%J (including spaces if any)

::  %%I will be used as reference for sorting in sort /r 
::  %%J will not be used in any command
::  %%K will be used to delete the file passing the fullpath

使用子字符串删除名称中“",使用参数传递的字符串%2(在),这将通过循环内的!_file?;%2=!_*.cvs递归获取查找相关奇偶校验文件的引用,查找所有 AAAwhere* 文件,在传递的第一个文件中使用部分名称(AAAA_*),在第二个文件中使用部分名称( )BBB_*,在第三个文件中使用部分名称,等等。

'where /r .\ !_file:%~2^=!_*.cvs /t^|sort /r'

以相反的形式转换输出,从大到小排序,利用行的第一个字符(对应于文件大小的数字)。

'...^|sort /r')do ...

循环if (do (for () loop) & exit /b)直到找到并列出最后一个文件(最小),并且对于找到的每个项目使用%%k(完整路径)和del命令有效地删除..所以当循环结束时,它退出该函数并返回到它调用的位置(代码中进行调用的第一个循环(call %:^) "!_n!" "_!_n:*_=!")..

.....(for ... (...)do del /q /f /a: "%%K") & exit /b

进一步阅读:

[√]如果 /?

[√]放 /?

[√]命令 /?

[√]为了 /?

[√]对于/F

[√]寻找 /?

[√]查找字符串 /?

[√]在哪里 /?

[√]样品

[√]重定向 |,<,> 2>,...

[√]Goto :Label|Call :Label

[√]延迟扩展(参考cmd /v /c:)

[√]Set variable=variable:substrings| DOS - 字符串操作

Windows 命令解释器 cmd.exe 如何解析脚本

操作方法:在 Windows 命令行中转义字符、分隔符和引号

相关内容