如何根据文件路径中的目录递归地删除* .bak文件,早于特定日期? [英] How to delete *.bak files recursively older than a specific date depending on directory in file path?

查看:54
本文介绍了如何根据文件路径中的目录递归地删除* .bak文件,早于特定日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Windows 7编写一个批处理文件.

我目前有一个代码,可从站点管理文件夹中的master文件夹中删除旧备份.这是代码:

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,如何删除目录树中任何位置超过3年的所有* .bak文件,并保持所有其他* .bak文件较新或位于文件路径中第二个目录所在的目录中不是masters?

解决方案

首先使用以下命令创建批处理文件C:\Temp\DeleteBackup.bat:

@echo off
set "BackupFileName=%~1"
if not "%BackupFileName:\masters\=%" == "%BackupFileName%" ECHO del "%BackupFileName%"

此批处理代码通过从字符串比较的左参数中删除不区分大小写的字符串,来检查具有完整路径和文件扩展名的文件名是否包含\masters\.

如果剩余的字符串由于路径中包含\masters\而与未修改的文件名字符串不相等,则 IF 条件为true,如果没有命令,则将删除备份文件 ECHO ,这只会显示 DEL 命令行.

例如,备份文件的完整列表是:

Y:\masters\Level2\Level3\Level4\Level5\Test1.bak
Y:\Folder2\masters\Level3\Test2.bak
Y:\Folder3\Level2\masters\Level4\Test3.bak
Y:\Folder4\Level2\Level3\Level4\Level5\Test4.bak
Y:\Folder5\Level2\Test5.bak
Y:\Folder6\Level2\Level3\Level4\Level5\Level6\Test6.bak
Y:\Folder7\masters\Test7.bak

删除的文件为:

Y:\masters\Level2\Level3\Level4\Level5\Test1.bak
Y:\Folder2\masters\Level3\Test2.bak
Y:\Folder3\Level2\masters\Level4\Test3.bak
Y:\Folder7\masters\Test7.bak

剩下的文件将是:

Y:\Folder4\Level2\Level3\Level4\Level5\Test4.bak
Y:\Folder5\Level2\Test5.bak
Y:\Folder6\Level2\Level3\Level4\Level5\Level6\Test6.bak

然后在您的批处理文件中使用:

forfiles /P "Y:\" /S /D -1096 /M *.bak /C "C:\Temp\DeleteBackup.bat @PATH"

当然可以修改DeleteBackup.bat来检查第二目录层次结构级别中的目录是否为masters.

@echo off
for /F "tokens=3 delims=\" %%I in ("%~1") do if /I "%%I" == "masters" ECHO del "%~1"

此代码将从文件上方的完整列表中删除:

Y:\Folder2\masters\Level3\Test2.bak
Y:\Folder7\masters\Test7.bak

剩下的文件将是:

Y:\masters\Level2\Level3\Level4\Level5\Test1.bak
Y:\Folder3\Level2\masters\Level4\Test3.bak
Y:\Folder4\Level2\Level3\Level4\Level5\Test4.bak
Y:\Folder5\Level2\Test5.bak
Y:\Folder6\Level2\Level3\Level4\Level5\Level6\Test6.bak


Robert Chizmadia Jr. 在已删除的评论中问:

是否可以使用GOTO代替在FORFILES命令行上调用另一个批处理文件?

这个附加问题的答案:

FORFILES 不是cmd.exe的内部命令,例如 FOR .如果已使用的Windows版本已预先安装,它将是一个存储在目录%SystemRoot%\System32中的控制台应用程序.

FORFILES 选项/C之后指定的要执行的命令必须是可执行文件或脚本.这就是为什么 DEL 执行Windows命令解释器cmd.exe的内部命令(如 DEL )时始终使用cmd /C的原因,因此真正完整的命令应为%SystemRoot%\System32\cmd.exe /C.

因此无法在 FORFILES 命令中使用类似 GOTO 的命令,因为没有名称为 GOTO 的可执行文件或脚本./p>

FOR 循环中的 GOTO 也会退出循环,因此,批处理文件命令行的解释将继续在批处理文件中的另一个位置进行.

但是,可以使用与用于运行 FORFILES 命令的文件相同的批处理文件进行文件路径评估和备份文件删除.

示例1的批处理文件不要求任何默认操作参数:

@echo off
if not "%~1" == "" (
    for /F "tokens=3 delims=\" %%I in ("%~1") do if /I "%%I" == "masters" ECHO del "%~1"
    goto :EOF
)

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

如果使用参数执行此批处理文件,它将运行 FOR 循环,以检查文件路径中的第二个目录是否为masters,在这种情况下,删除后将其删除回声.否则,在启动没有任何参数的批处理文件时,该批处理文件将运行 FORFILES 可执行文件.

示例2的批处理文件需要1个或多个默认操作参数:

@echo off
if "%~1" == "#Delete:Backup#" (
    for /F "tokens=3 delims=\" %%I in ("%~2") do if /I "%%I" == "masters" ECHO 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.

这与示例1几乎相同,不同之处在于,如果在运行批处理文件时使用的第一个参数区分大小写,字符串#Delete:Backup#,则批处理文件将使用具有完整路径的备份文件的名称作为第二个参数如果文件路径中的第二个目录为masters,则将其删除.

在所有批处理代码示例中,同样在此代码示例中,在del命令之前也必须先删除命令 ECHO ,才能真正执行备份文件的删除.


要了解所使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读每个命令显示的所有帮助页面.

  • echo /?
  • for /?
  • forfiles /?
  • goto /?
  • if /?
  • set /?

I'm writing a batch file for Windows 7.

I currently have a code that deletes old backups from our masters folders within our site management folders. This is the code:

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

However I need to code it to only delete things that are older than 3 years, which would be this code:

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

However I need what is in the top code so that I can delete all *.bak files from the masters folders that exist within our 173 site management folders. I'm ripping my hair out figuring this out. I can't have it deleting *.bak files from our other folders.

I've tried combining the code, but below command line in batch file does not work as expected:

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

How to delete all *.bak files older than 3 years anywhere in directory tree if second directory in file path is masters and keep all other *.bak files being newer or in a directory where second directory in file path is not masters?

解决方案

Create first a batch file C:\Temp\DeleteBackup.bat with the following commands:

@echo off
set "BackupFileName=%~1"
if not "%BackupFileName:\masters\=%" == "%BackupFileName%" ECHO del "%BackupFileName%"

This batch code checks if the file name with full path and file extension contains anywhere \masters\ by removing this string case-insensitive from left argument of string comparison.

If the remaining string is not equal the unmodified file name string because of containing \masters\ in path, the IF condition is true and the backup file would be deleted if there would not be command ECHO which results in just displaying the DEL command line.

For example the complete list of backup files is:

Y:\masters\Level2\Level3\Level4\Level5\Test1.bak
Y:\Folder2\masters\Level3\Test2.bak
Y:\Folder3\Level2\masters\Level4\Test3.bak
Y:\Folder4\Level2\Level3\Level4\Level5\Test4.bak
Y:\Folder5\Level2\Test5.bak
Y:\Folder6\Level2\Level3\Level4\Level5\Level6\Test6.bak
Y:\Folder7\masters\Test7.bak

The files deleted would be:

Y:\masters\Level2\Level3\Level4\Level5\Test1.bak
Y:\Folder2\masters\Level3\Test2.bak
Y:\Folder3\Level2\masters\Level4\Test3.bak
Y:\Folder7\masters\Test7.bak

And the files remaining would be:

Y:\Folder4\Level2\Level3\Level4\Level5\Test4.bak
Y:\Folder5\Level2\Test5.bak
Y:\Folder6\Level2\Level3\Level4\Level5\Level6\Test6.bak

Then use in your batch file:

forfiles /P "Y:\" /S /D -1096 /M *.bak /C "C:\Temp\DeleteBackup.bat @PATH"

It is of course possible to modify DeleteBackup.bat to check if directory in second directory hierarchy level is masters.

@echo off
for /F "tokens=3 delims=\" %%I in ("%~1") do if /I "%%I" == "masters" ECHO del "%~1"

This code would delete from the complete list above the files:

Y:\Folder2\masters\Level3\Test2.bak
Y:\Folder7\masters\Test7.bak

And the files remaining would be:

Y:\masters\Level2\Level3\Level4\Level5\Test1.bak
Y:\Folder3\Level2\masters\Level4\Test3.bak
Y:\Folder4\Level2\Level3\Level4\Level5\Test4.bak
Y:\Folder5\Level2\Test5.bak
Y:\Folder6\Level2\Level3\Level4\Level5\Level6\Test6.bak


Robert Chizmadia Jr. asked in an already deleted comment:

Is it possible to use GOTO instead of calling another batch file on FORFILES command line?

The answer on this additional question:

FORFILES is not an internal command of cmd.exe like FOR. It is a console application stored in directory %SystemRoot%\System32 if used version of Windows has it pre-installed at all.

The command to execute as specified after FORFILES option /C must be an executable or script. That is the reason why cmd /C is always used when an internal command of Windows command interpreter cmd.exe like DEL should be executed by FORFILES whereby the really complete command would be %SystemRoot%\System32\cmd.exe /C.

So it is not possible to use a command like GOTO in FORFILES command as there is no executable or script with name GOTO.

Also GOTO in a FOR loop exits the loop and therefore interpreting of command lines of batch files continues on another position in batch file.

However, it is possible to use the same batch file for the file path evaluation and backup file deletion as used to run FORFILES command.

Example 1 with batch file not expecting any parameter for default operation:

@echo off
if not "%~1" == "" (
    for /F "tokens=3 delims=\" %%I in ("%~1") do if /I "%%I" == "masters" ECHO del "%~1"
    goto :EOF
)

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

If this batch file is executed with an argument, it runs the FOR loop written to check if second directory in file path is masters and delete this file in this case after removing ECHO. Otherwise on starting the batch file without any parameter the batch file runs the FORFILES executable.

Example 2 with batch file expecting 1 or more parameters for default operation:

@echo off
if "%~1" == "#Delete:Backup#" (
    for /F "tokens=3 delims=\" %%I in ("%~2") do if /I "%%I" == "masters" ECHO 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.

This is nearly the same as example 1 with the difference that if first parameter used on running the batch file is case-sensitive the string #Delete:Backup#, the batch file expects as second parameter the name of a backup file with full path being deleted if second directory in file path is masters.

Like in all batch code examples the command ECHO must be removed before del command also in this code example to really execute the deletion of the backup files.


For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • echo /?
  • for /?
  • forfiles /?
  • goto /?
  • if /?
  • set /?

这篇关于如何根据文件路径中的目录递归地删除* .bak文件,早于特定日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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