简体   繁体   English

如何创建批处理脚本,当达到文件夹大小限制时,该脚本将删除最早的文件

[英]How do you create a batch script that will delete the oldest files when a folder size limit is reached

我已经找到了许多解决类似问题的解决方案,但是我不知道是否可以创建一个脚本,以在达到预设的文件夹大小限制后删除最旧的文件?

You need to solve two problems. 您需要解决两个问题。

First, you need to calculate the folder size. 首先,您需要计算文件夹的大小。 Use a code similar to this 使用与此类似的代码

:foldersize
set sz=0
for %%F in (%1\*.*) do (
  set /a kb = %%~zF / 1024
  set /a sz = !sz! + !kb!  
  echo %%F %%~zF !kb! !sz!
)
goto :eof

Second, you need to recognize older files and delete them until a size is reached 其次,您需要识别较旧的文件并删除它们,直到达到大小为止

for /F "tokens=*" %%F in ('dir /A-D /OD /B %1\*.*') do (
  if !sz! geq !targetsize! (
    call :filesize %1\%%F
    del %1\%%F
    set /a sz = !sz! - !kb!
  ) else (
    goto :eof
  )
) 
goto :eof  

:filesize
set /a kb = %~z1 / 1024
goto :eof

Putting all pieces together... 将所有零件放在一起...

@echo off
setlocal enabledelayedexpansion
set /a targetsize=%2
call :foldersize %1
for /F "tokens=*" %%F in ('dir /A-D /OD /B %1\*.*') do (
  if !sz! geq !targetsize! (
    call :filesize %1\%%F
    del %1\%%F
    set /a sz = !sz! - !kb!
  ) else (
    echo Done... %1 size is now !sz! KB
    goto :eof
  )
) 
echo Not completely done... %1 size is still !sz! KB 
goto :eof  

:filesize
set /a kb = %~z1 / 1024
goto :eof

:foldersize
set sz=0
for %%F in (%1\*.*) do (
  set /a kb = %%~zF / 1024
  set /a sz = !sz! + !kb!  
)
goto :eof

Test and test and test, as it does not move the files to the trash but it deletes the files permanently. 测试并测试,因为它不会将文件移到回收站,但会永久删除文件。

Also, you may want to specify /F option in case you have read-only files you want to delete. 另外,如果您有要删除的只读文件,则可能要指定/F选项。

In the case you have subfolders in the folder and you want to take those into the account of folder size and you want to delete the older files, things may get more complicated. 如果文件夹中有子文件夹,并且您想将这些文件夹考虑到文件夹大小中,并且想要删除较旧的文件,则情况可能会变得更加复杂。

The calculation of the size is this 尺寸的计算是这个

:foldersizerecurse
set sz=0
for /F %%F in ('dir /OD /B *.*') do (
  set /a kb = %%~zF / 1024
  set /a sz = !sz! + !kb!  
  echo %%F %%~zF !kb! !sz!
)
goto :eof

And deleting the older files.. you need to pipe the 'dir /S' command output to sort and sort by date. 并删除较旧的文件..您需要通过管道'dir / S'命令输出来对日期进行排序和排序。 I feel tired to do it. 我觉得很累。

暂无
暂无

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

相关问题 如何创建将按计划删除文件夹的批处理脚本? - How do I create a batch script that will delete a folder on a scheduled basis? 如何创建一个批处理脚本来删除早于 X 的文件而不是在根文件夹中删除并排除某些子文件夹? - How to create a batch script that deletes files older than X and not delete at the root folder and exclude certain subfolders? 如何遍历文件夹中的文件以创建参数列表以传递到Windows批处理文件中的另一个脚本中? - How do I loop through files in folder to create a parameter list to pass into another script in a Windows batch file? 如何使用 C# 从 windows Temp 文件夹中删除所有文件? (当进程运行时) - How do you delete all files from the windows Temp folder using C#? (When the processes are running) 如何创建一个包含项目中所有文件的文件夹? - How do you create a folder containing all the files in a project? 如何创建一个程序,在文件夹达到一定数量的文件后自动删除文件(先删除旧文件) - How do I create a program that auto-deletes files after the folder has reached a certain number of files (deleting old file first) 如何从文件名的一部分创建批处理文件夹,将文件移动到具有修改名称的文件夹中? - How do I create batch folder from part of file name, move files into folder with a modified name? 批处理文件删除文件和文件夹 - Batch file to delete files and folder 批处理-删除文件夹和所有文件 - Batch - Delete folder and all files 使用Windows Scheduler运行时无法使用.bat删除和创建文件和文件夹的PHP脚本 - Php Script to delete and create files and folder using .bat not working when run using Windows Scheduler
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM