windows - Windows Server 2008 R2 中的批处理脚本

标签 windows batch-file

我需要一个批处理批处理脚本来检查文件夹的大小。如果它达到某个 X GB,例如:10 GB,它应该删除其中的所有子目录或文件。

我知道这在 Linux 中很容易完成。但我对 Windows 服务器和编写批处理脚本很不熟悉。

目前我正在运行一个脚本,每次运行时都会删除文件/文件夹,就像这样

set folder="C:\myfolder"
cd /d %folder%
for /F "delims=" %%i in ('dir /b') do (rmdir "%%i" /s/q || del "%%i" /s/q)

但我只想删除达到 10 或 12 GB 等 GB 大小时的文件/文件夹。

最佳答案

在 Windows 服务器上使用 powershell。然后查找文件夹的大小:

$size = (Get-ChildItem $FolderName -Recurse | Measure-Object -Property Length -Sum).Sum
if ($size > $Limit) {
    Remove-Item $FolderName -Recurse -Whatif
}

作为 Is PowerShell ready to replace my Cygwin shell on Windows? 的最佳答案,对于为什么 powershell 比一组 unix 工具更有用,有一个很好的解释。

-Whatif 使 Remove-Item 安全,因为添加 -WhatIf 只是报告命令在执行时将执行的操作。

关于windows - Windows Server 2008 R2 中的批处理脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30658891/

相关文章:

c - eclipse CDT "Symbol NULL could not be resolved"

printing - 使用 Windows 图片和传真查看器 (shimgvw.dll) 从命令行打印 jpg 时,如何选择布局?

windows - 创建一个 python 脚本来编译和运行一个 c++ 文件

windows - NPM 安装 (Windows) 抛出 .SLN 错误...?

for-loop - 批处理文件,For 循环不回显行

windows - 如何从 PATH 环境变量中提取完整路径?

windows - 一次在多个文件夹中运行一个批处理文件

python替换为捕获组

windows - 在 Windows 批处理中检查空变量

.net - 禁用 "application has stopped working"窗口