编写批处理文件,删除子目录的特定子目录中的3年历史文件

时间:2017-08-03 12:33:39

标签: windows batch-file

我正在为Windows 7编写批处理文件。

我目前有一个代码可以从我们的站点管理文件夹中的主站文件夹中删除旧备份。这是代码:

for /d %%A in ("Y:\*.*") do del /s /q /f "%%A\masters\*.bak"

但是我需要将其编码为仅删除超过3年的内容,这将是以下代码:

forfiles /P "Y:\" /S /D -1096 /M *.bak /C "cmd /C del @path"

但是我需要顶部代码中的内容,以便我可以从173个站点管理文件夹中的masters文件夹中删除所有* .bak文件。我正在扯掉我的头发搞清楚这一点。我无法从其他文件夹中删除* .bak文件。

我已尝试合并代码,但批处理文件中的命令行下方无法正常工作:

forfiles /S /D -1096 /M *.bak /C "cmd /C for /d %%A in ("Y:\*.*") do del /s /q /f "%%A\masters\*.bak"

如果文件路径中的第二个目录为masters并且所有其他* .bak文件保持较新或者在文件中第二个目录的目录中,如何删除目录树中任何位置超过3年的所有* .bak文件路径不是masters

imgur.com/a/isTOm这是目录。由于我在驱动器上的权限,我无法进行转储,所以这是我能做的最好的。

如何删除以下内容: |美国 - 美国|加拿大| GME | GMIO | GMSA |墨西哥| 仅3个年龄或更长的6个目录中的每个目录文件夹中的文件夹以及WIP和MASTERS文件夹?

提前致谢!!

@Mofi试图在这里帮助我,但是在工作之后他们改变了文件夹,MOFI建议我提出一个新问题而不是编辑自己的问题,How to delete *.bak files recursively older than a specific date depending on directory in file path?在这里。

2 个答案:

答案 0 :(得分:0)

我使用Document.txt和以下批处理代码来测试批处理文件顶部的代码:

@echo off
if "%~1" == "#Delete:Backup#" (
    for /F "tokens=4 delims=\" %%I in ("%~2") do (
        if /I "%%I" == "masters" (
            ECHO del from line %Line%: "%~2"
        ) else if /I "%%I" == "wip" (
            ECHO del from line %Line%: "%~2"
        )
    )
    goto :EOF
)

set "Line=1"
for /F "delims=" %%# in (Document.txt) do (
    call "%~f0" #Delete:Backup# "%%#"
    set /A Line+=1
)

输出是Document.txt中的1219行:

del from line 19: "Y:\America - US\Milford\WIP\Site\29678-EDM\156400000-CS1-00.bak"
del from line 20: "Y:\America - US\Milford\WIP\1908\20170721_05.59\156401908-IF1-L1.bak"
del from line 21: "Y:\America - US\Milford\WIP\1908\20170721_05.59\156401908-AE1-L1.bak"
del from line 22: "Y:\America - US\Milford\WIP\1908\20170721_05.59\30083-EDM\156401908-1500086IF1-L1.bak"
del from line 34: "Y:\America - US\Milford\Masters\1908\156401908-AE1-L1.bak"
del from line 48: "Y:\America - US\Warren Tech Center\WIP\20005\20170804_09.09\156320005-IF1-01.bak"
del from line 49: "Y:\America - US\Warren Tech Center\WIP\20005\20170804_09.09\156320005-IF1-L1.bak"
del from line 50: "Y:\America - US\Warren Tech Center\WIP\20005\20170804_09.09\156320005-IF1-02.bak"
del from line 51: "Y:\America - US\Warren Tech Center\WIP\20005\20170804_09.09\156320005-IF1-03.bak"
del from line 52: "Y:\America - US\Warren Tech Center\WIP\20005\20170804_09.09\AEB cabinet modifications Duane\156320005-IF1-02_SOM.bak"
del from line 53: "Y:\America - US\Warren Tech Center\WIP\20005\20170804_09.09\AEB cabinet modifications Duane\156320005-IF1-L1_SOM.bak"
del from line 54: "Y:\America - US\Warren Tech Center\WIP\20005\20170804_09.09\AEB cabinet modifications Duane\156320005-IF1-01_SOM.bak"
del from line 55: "Y:\America - US\Warren Tech Center\WIP\20005\20170804_09.09\AEB cabinet modifications Duane\156320005-IF1-03_SOM.bak"
del from line 56: "Y:\America - US\Warren Tech Center\WIP\1971\30147-EDM\156301971-EL1-03.bak"
del from line 57: "Y:\America - US\Warren Tech Center\WIP\1971\30147-EDM\156301971-EL1-02.bak"
del from line 58: "Y:\America - US\Warren Tech Center\WIP\1971\30138-EDM\156301971-AC1-L1.bak"
del from line 59: "Y:\America - US\Warren Tech Center\WIP\1971\30138-EDM\156301971-EL1-L1.bak"
del from line 60: "Y:\America - US\Warren Tech Center\WIP\1971\30138-EDM\156301971-AE1-L1.bak"
del from line 139: "Y:\America - US\Warren Tech Center\Masters\4606\156304606-IF1-L1g.bak"
del from line 140: "Y:\America - US\Warren Tech Center\Masters\4606\156304606-IF1-L1f.bak"
del from line 141: "Y:\America - US\Warren Tech Center\Masters\4606\156304606-IF1-L1.bak"
del from line 142: "Y:\America - US\Warren Tech Center\Masters\9227\156309227-IF1-01.bak"
del from line 143: "Y:\America - US\Warren Tech Center\Masters\20005\156320005-IF1-L1.bak"
del from line 144: "Y:\America - US\Warren Tech Center\Masters\1971\156301971-IF1-L1w.bak"
del from line 145: "Y:\America - US\Warren Tech Center\Masters\1971\156301971-IF1-L1e.bak"
del from line 146: "Y:\America - US\Warren Tech Center\Masters\1971\156301971-IF1-L1.bak"
del from line 147: "Y:\America - US\Warren Tech Center\Masters\1971\156301971-AC1-L1.bak"
del from line 148: "Y:\America - US\Warren Tech Center\Masters\1971\156301971-EL1-L1.bak"
del from line 149: "Y:\America - US\Warren Tech Center\Masters\1971\156301971-AE1-L1.bak"
del from line 150: "Y:\America - US\Warren Tech Center\Masters\1958\156301958-IF1-02.bak"
del from line 151: "Y:\America - US\Warren Tech Center\Masters\19867\156319867-IF1-09.bak"
del from line 152: "Y:\America - US\Warren Tech Center\Masters\19867\156319867-IF1-02.bak"
del from line 153: "Y:\America - US\Warren Tech Center\Masters\19867\156319867-IF1-06.bak"
del from line 154: "Y:\America - US\Warren Tech Center\Masters\19867\156319867-IF1-07.bak"
del from line 155: "Y:\America - US\Warren Tech Center\Masters\1963\156301963-IF1-02.bak"
del from line 156: "Y:\America - US\Warren Tech Center\Masters\1979\156301979-AE1-01.bak"
del from line 157: "Y:\America - US\Warren Tech Center\Masters\1979\156301979-IF1-01.bak"

看起来正确,因此使用的代码是:

@echo off
if "%~1" == "#Delete:Backup#" (
    for /F "tokens=4 delims=\" %%I in ("%~2") do (
        if /I "%%I" == "masters" (
            del "%~2"
        ) else if /I "%%I" == "wip" (
            del "%~2"
        )
    )
    goto :EOF
)

rem Other commands processing the parameters.

%SystemRoot%\System32\forfiles.exe /P "Y:\" /S /D -1096 /M *.bak /C "%~f0 #Delete:Backup# @PATH"

rem More commands executed after the deletion of the backup files.

然后我可能会在将来添加其他目录,因此比这更好的代码结构将有助于您添加其他目录。所以我想了一点,然后再次使用Documen.txt使用带有完整路径的1219 * .bak文件名进行了修改后的版本。

@echo off
if "%~1" == "#Delete:Backup#" (
    for /F "tokens=4 delims=\" %%I in ("%~2") do (
        for %%J in (masters wip) do if /I "%%I" == "%%~J" ECHO del from line %Line%: "%~2" & goto :EOF
    )
    goto :EOF
)

set "Line=1"
for /F "delims=" %%# in (Document.txt) do (
    call "%~f0" #Delete:Backup# "%%#"
    set /A Line+=1
)

此代码生成的输出与第一个代码完全相同。所以你也可以使用:

@echo off
if "%~1" == "#Delete:Backup#" (
    for /F "tokens=4 delims=\" %%I in ("%~2") do (
        for %%J in (masters wip) do if /I "%%I" == "%%~J" del "%~2" & goto :EOF
    )
    goto :EOF
)

rem Other commands processing the parameters.

%SystemRoot%\System32\forfiles.exe /P "Y:\" /S /D -1096 /M *.bak /C "%~f0 #Delete:Backup# @PATH"

rem More commands executed after the deletion of the backup files.

第三目录级别的其他目录可以轻松添加到masters wip一行。

答案 1 :(得分:0)

我理解这项任务:删除路径为*\*\Masters的文件夹中的所有旧文件。

  1. 使用dir /S /B获取所有文件夹的完整路径。这将输出已满 递归地显示当前路径中所有文件夹的路径。

  2. 使用命令findstr /R /C:"Y:\\[^\\]*\\[^\\]*\\masters$"过滤文件夹。它只输出那些路径 匹配reqexp。注意加倍的斜杠。 [^\\]*\\[^\\]*\\masters$匹配恰好有三行的行 层次结构部分,以masters结束。用于批处理^符号 必须加倍,因为它是特殊的逃脱符号。

  3. 在找到的路径

  4. 上执行forfiles

    示意图看起来像

    for /f "delims=" %%f in ('dir masters /S /B ^| findstr /R /C:"Y:\\[^^\\]*\\[^^\\]*\\masters$"') do (
      :: %%f contains full path to "masters" folder
      forfiles ... 
    )