简体   繁体   English

如果所有文件都超过3天,则批量文件删除目录中的子文件夹

[英]Batch file to delete subfolders in directory if all files is older than 3 days

I need to complete my batch script. 我需要完成我的批处理脚本。 I have a path C:\\Users\\Mahmo03S\\Shaban. 我有一个路径C:\\ Users \\ Mahmo03S \\ Shaban。 In that path there a several subfolders, eg. 在那条路径中有几个子文件夹,例如。 Ansys. ANSYS。 In the subfolder Ansys there are even more subfolders = its a foldertree. 在子文件夹Ansys中有更多子文件夹=它是一个foldertree。 My batch script needs to delete the Ansys folder if every files in the folder and the subfolders is older than 3 days. 如果文件夹和子文件夹中的每个文件都超过3天,我的批处理脚本需要删除Ansys文件夹。 If just one file in Ansyn is modified in the last 3 days then nothing should happen with Ansys. 如果在过去3天内只修改了Ansyn中的一个文件,那么Ansys就不会发生任何事情。 I tried to make a solid solution but: The batch file deletes a subsubfolder in Ansys directory because the files in the subsubfolder is not modified since 3 days. 我尝试制作一个可靠的解决方案但是:批处理文件删除Ansys目录中的子子文件夹,因为子文件夹中的文件自3天后未被修改。

I got 3 batch files doing the task. 我有3个批处理文件来完成任务。

Script.bat Script.bat

@echo off
setlocal enableextensions
pushd C:\Users\Mahmo03S\Desktop\Shaban

set /p check="Select a date:" 
Rem When prompted with above line type the date 3 days ago.

forfiles /c "cmd /c (IF @isdir==TRUE call C:\Users\Mahmo03S\Desktop\Search.bat "@path" "%check%")"

popd

Search.bat Search.bat

set del=TRUE
forfiles /p %1 /d -%2 /s /m * /c "cmd /c (IF @isdir ==TRUE call C:\Users\Mahmo03S\Desktop\DeleteFolders.bat "@path" "%check%" "%del%")"

DeleteFolders.bat DeleteFolders.bat

set del=FALSE
if %del%==FALSE (rmdir /S /Q %1)

The problem is: I got a subsubfolder (eg hello) in the folder Ansys. 问题是:我在Ansys文件夹中有一个子文件夹(例如hello)。 In the hello folder every file is older than 3 days. 在hello文件夹中,每个文件都超过3天。 The script deletes the hello folder which it should not do. 该脚本删除它不应该执行的hello文件夹。 It should only delete Ansys if EVERY file in the folder is older than 3 days. 如果文件夹中的每个文件都超过3天,它应该只删除Ansys。 If just one file is modified in Ansys folder for the past 3 days nothing in the Ansys folder should be deleted. 如果在过去3天内只在Ansys文件夹中修改了一个文件,则应删除Ansys文件夹中的任何内容。

I made the batch file so it deletes the Ansys folder if every file is older than 3 days. 我制作了批处理文件,因此如果每个文件都超过3天,它会删除Ansys文件夹。 But it should not delete a subsubfolder just because the files in it is older than 3 days. 但它不应该仅仅因为其中的文件超过3天而删除子文件夹。

How can I do that? 我怎样才能做到这一点?

A working solution can be made much simpler ;) 可以使工作解决方案更简单;)

You can detect if FORFILES printed any result, and take action if nothing was printed. 您可以检测FORFILES是否打印了任何结果,并在未打印任何内容时执行操作。

Use FOR /D to iterate the direct child folders. 使用FOR / D迭代直接子文件夹。 For each folder, run FORFILES looking for files that are at or later than the cutoff date. 对于每个文件夹,运行FORFILES以查找截止日期或晚于截止日期的文件。 If you didn't have to worry about ignoring folders, then you could simply take action based on the FORFILES return code. 如果您不必担心忽略文件夹,那么您可以根据FORFILES返回代码执行操作。 But you don't want to get a false positive based on a new but empty folder. 但是你不希望基于一个新的空文件夹得到误报。 You can detect if FORFILES printed any filenames using FINDSTR, and delete the folder tree if none were printed. 您可以检测FORFILES是否使用FINDSTR打印任何文件名,如果没有打印则删除文件夹树。 FORFILES still prints an empty line if it finds a new folder, so you must search for a character using . FORFILES如果找到新文件夹仍会打印空行,因此您必须使用搜索字符. instead of searching for a line using "^" . 而不是使用"^"搜索一行。

@echo off
setlocal disableDelayedExpansion
set "root=C:\Users\Mahmo03S\Shaban"
set /p "cutoff=Enter a cutoff date (mm/dd/yyyy): "
for /d %%F in ("%root%\*") do (
  2>nul forfiles /p "%%F" /d %cutoff% /s /c "cmd /c if @isdir==FALSE echo @file"
) | >nul 2>nul findstr . || rd /s /q "%%F"

There are multiple methods posted on StackOverflow and elsewhere showing how to do date arithmetic in batch. StackOverflow和其他地方发布了多种方法,展示了如何批量进行日期算术。 I'm partial to a hybrid JScript/batch utility called getTimestamp.bat . 我偏向于一个名为getTimestamp.bat混合JScript /批处理实用程序 It is pure script that runs on any modern Windows machine from XP onward - no 3rd party executable required. 它是从XP开始在任何现代Windows机器上运行的纯脚本 - 不需要第三方可执行文件。 It is chock full of options, allowing you to do nearly any date computation you could ever imagine. 它充满了各种选项,允许您进行几乎任何您能想象到的日期计算。

Assuming getTimestamp.bat is in your current directory, or better yet, somewhere within your PATH, then the following line can be used to compute your cutoff date instead of prompting for the date: 假设getTimestamp.bat位于当前目录中,或者更好,位于PATH中的某个位置,则可以使用以下行来计算截止日期,而不是提示输入日期:

call gettimestamp -OD -3 -F {mm}/{dd}/{yyyy} -R cutoff

This uses a VBS script to calculate the date 3 days ago, and then checks if any files are younger than that, using xcopy, and if no files are younger than 3 days then it should remove the "C:\\Users\\Mahmo03S\\Shaban\\Ansys" folder and subdirectories. 这使用VBS脚本计算3天前的日期,然后检查是否有任何文件比使用xcopy更年轻,如果没有文件小于3天,那么它应该删除"C:\\Users\\Mahmo03S\\Shaban\\Ansys"文件夹和子目录。

3 days is a date calculation and not exactly 72 hours. 3天是日期计算,不完全是72小时。

It's not tested. 它没有经过测试。

@echo off
call :date today -3
echo mm-dd-yyyy 3 days ago was: %day%

xcopy "C:\Users\Mahmo03S\Shaban\Ansys\*.*" "%temp%\" /l /s /d:%day% |find ":" >nul

if %errorlevel% EQU 1 rd /s /q "C:\Users\Mahmo03S\Shaban\Ansys"

goto :EOF
:date
set date1=%1
set qty=%2
set separator=%~3
if /i "%date1%" EQU "TODAY" (set date1=now) else (set date1="%date1%")
echo >"%temp%\%~n0.vbs" s=DateAdd("d",%qty%,%date1%)
echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^&_
echo>>"%temp%\%~n0.vbs"         right(100+month(s),2)^&_
echo>>"%temp%\%~n0.vbs"         right(100+day(s),2)
for /f %%a in ('cscript //nologo "%temp%\%~n0.vbs"') do set result=%%a
del "%temp%\%~n0.vbs"
endlocal& set "YY=%result:~0,4%"&set "MM=%result:~4,2%"&set "DD=%result:~6,2%"
set "day=%MM%-%DD%-%YY%"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 批处理文件删除超过N天的文件 - Batch file to delete files older than N days Windows 批处理文件删除超过 X 天的文本文件 - Windows Batch file to delete text files older than X days Windows 批处理文件删除早于 X 天的文件,但保留一分钟的 X 文件 - Windows batch file delete files older than X days but leave a min of X files 批处理文件可删除N天之前的文件并将结果另存为文本文件 - Batch file to delete files older than N days and save the result as a text file 批量删除早于x天的FTP上的文件 - Batch delete files on FTP older than x days 批处理文件以递归方式删除超过N天的文件夹中的文件 - Batch file to recursively delete files in a folder older than N number of days 如何创建一个批处理脚本来删除早于 X 的文件而不是在根文件夹中删除并排除某些子文件夹? - How to create a batch script that deletes files older than X and not delete at the root folder and exclude certain subfolders? 用于删除超过 X 天的文件的批处理脚本(基于创建日期,而不是修改日期) - Batch script to delete files older than X days (based on creation date, not modified date) 在忽略子文件夹的同时删除早于x的文件 - Delete files older than x while ignoring subfolders 在Windows 7上批处理作业以删除7天以上的文件夹 - Batch job to delete folder older than 7 days on windows 7
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM