删除 C 盘空文件夹--递归删除

TypeScript 复制代码
# Language: Powershell
# Style: TypeScript

# 文件名:
# "文件(夹):删除空文件夹.ps1"

function Remove-EmptyDirectories {
	param (
		[string]$Path
	)
	# 获取所有目录,把子目录排列在父目录前面。
	# 删除所有子文件(夹)后,可判断父目录为空,进而再删除父目录。
	$directories = Get-ChildItem $Path -Recurse -Directory | Sort-Object -Property FullName -Descending
	foreach ($dir in $directories) {
		# 检查目录是否为空(排除 desktop.ini)
		if (-not (Get-ChildItem $dir.FullName -Recurse) -and 
		    -not (Test-Path (Join-Path $dir.FullName "desktop.ini"))) {
			# 检查目录属性,排除具有系统、只读和重分析点属性的目录
			$attributes = (Get-Item $dir.FullName).Attributes
			if (-not ($attributes -band [System.IO.FileAttributes]::System) -and
			    -not ($attributes -band [System.IO.FileAttributes]::ReadOnly) -and
				-not ($attributes -band [System.IO.FileAttributes]::ReparsePoint)) {
					Remove-Item $dir.FullName -Force -Verbose
			}
		}
	}
}

function main {
	if ($args.Count -gt 0) {
			Remove-EmptyDirectories $($args[0])
	} else {
			Write-Host "No arguments provided."
	}
}

main $args

# Usage:
# PowerShell.exe -File "文件(夹):删除空文件夹.ps1" "C:\"

<#
 # 无回溯,只删除了叶子空目录
Get-ChildItem "C:\" -Recurse | Where-Object { $_.PSIsContainer -and @(Get-ChildItem $_.FullName -Recurse).Count -eq 0 } | Remove-Item
#>
Cobol 复制代码
:: Language: cmd
:: Style: Cobol

:: 使用方法: "你命名的脚本.bat" "C:\"

:DelEmptyDir
@	echo off&pushd "%TEMP%"&setlocal EnableDelayedExpansion
	@if not exist "%~1\" exit /b 0
	REM 设置要清理的路径
	set "targetPath=%~1"
	REM 遍历所有目录
	for /f "delims=" %%D in ('dir "%targetPath%" /s/b/ad ^| sort /r') do (
		set "dirPath=%%D"
		REM 检查目录是否为空
		set isEmpty=1
		dir /a/b "!dirPath!" | findstr .* >nul && set isEmpty=0
		REM 检查是否包含 desktop.ini 文件
		if exist "!dirPath!\desktop.ini" set isEmpty=0
		REM 检查目录属性
		for %%A in ("!dirPath!") do (
			set "attr=%%~aA"
			echo !attr! | find "R" >nul && set isReadOnly=1 || set isReadOnly=0
			echo !attr! | find "S" >nul && set isSystem=1 || set isSystem=0
			echo !attr! | find "L" >nul && set isReparsePoint=1 || set isReparsePoint=0
		)
		REM 如果目录为空且不包含特定属性或文件,则删除
		if "!isEmpty!"=="1" if "!isReadOnly!"=="0" if "!isSystem!"=="0" if "!isReparsePoint!"=="0" (
			echo Deleting empty directory: "!dirPath!"
			rd "!dirPath!" >nul 2>&1
		)
	)
@	endlocal & popd
@exit /b 0

不想麻烦,本想网上找一个简单脚了事,没想太多照搬的,而且还错的离谱。只好自己搞个。

使用 Powershell 语言

function Remove-EmptyDirectories {

param (

string$Path

)

获取所有目录,把子目录排列在父目录前面。

删除所有子文件(夹)后,可判断父目录为空,进而再删除父目录。

directories = Get-ChildItem Path -Recurse -Directory | Sort-Object -Property FullName -Descending

foreach (dir in directories) {

检查目录是否为空(排除 desktop.ini)

if (-not (Get-ChildItem $dir.FullName -Recurse) -and

-not (Test-Path (Join-Path $dir.FullName "desktop.ini"))) {

检查目录属性,排除具有系统、只读和重分析点属性的目录

attributes = (Get-Item dir.FullName).Attributes

if (-not ($attributes -band System.IO.FileAttributes::System) -and

-not ($attributes -band System.IO.FileAttributes::ReadOnly) -and

-not ($attributes -band System.IO.FileAttributes::ReparsePoint)) {

Remove-Item $dir.FullName -Force -Verbose

}

}

}

}

function main {

if ($args.Count -gt 0) {

Remove-EmptyDirectories (args0)

} else {

Write-Host "No arguments provided."

}

}

main $args

PowerShell.exe -File "文件(夹):删除空文件夹.ps1" "C:\"

<#

无回溯,只删除了叶子空目录

Get-ChildItem "C:\" -Recurse | Where-Object { _.PSIsContainer -and @(Get-ChildItem _.FullName -Recurse).Count -eq 0 } | Remove-Item

#>

:: ---------------------------------------------------------------------------------------------------------------------------

使用 CMD 命令:

:DelEmptyDir

@ echo off&pushd "%TEMP%"&setlocal EnableDelayedExpansion

@if not exist "%~1\" exit /b 0

REM 设置要清理的路径

set "targetPath=%~1"

REM 遍历所有目录

for /f "delims=" %%D in ('dir "%targetPath%" /s/b/ad ^| sort /r') do (

set "dirPath=%%D"

REM 检查目录是否为空

set isEmpty=1

dir /a/b "!dirPath!" | findstr .* >nul && set isEmpty=0

REM 检查是否包含 desktop.ini 文件

if exist "!dirPath!\desktop.ini" set isEmpty=0

REM 检查目录属性

for %%A in ("!dirPath!") do (

set "attr=%%~aA"

echo !attr! | find "R" >nul && set isReadOnly=1 || set isReadOnly=0

echo !attr! | find "S" >nul && set isSystem=1 || set isSystem=0

echo !attr! | find "L" >nul && set isReparsePoint=1 || set isReparsePoint=0

)

REM 如果目录为空且不包含特定属性或文件,则删除

if "!isEmpty!"=="1" if "!isReadOnly!"=="0" if "!isSystem!"=="0" if "!isReparsePoint!"=="0" (

echo Deleting empty directory: "!dirPath!"

rd "!dirPath!" >nul 2>&1

)

)

@ endlocal & popd

@exit /b 0

相关推荐
满怀冰雪3 天前
22_Runnable接口源码拆解_LCEL管道语法背后_invoke_stream_batch究竟做了什么
python·batch
xcLeigh3 天前
KES运维自动化与脚本体系实战
运维·数据库·自动化·脚本·数据迁移·kes
CAU界编程小白3 天前
CAU抢课脚本
c++·脚本
无为之士22 天前
Windows 批量打印 PDF 工具分享:支持文件夹、指定文件、当天文件、预览列表
windows·powershell
无聊的老谢23 天前
基于 Spring Batch 的电信 MR 数据亿级记录清洗实战
spring·batch·mr
疯狂学习GIS24 天前
基于Python earthaccess库批量下载全球MODIS GPP(MOD17A2HGF)数据
python·脚本·批量下载·遥感影像·nasa·earthdata·自动处理
m0_图灵灵25 天前
吴恩达《深度学习》之深度剖析Batch Norm 作用机制的本质
人工智能·深度学习·batch
qq_527887871 个月前
机器学习训练中Epoch、Batch、Bath_size、Data_size的区别
人工智能·机器学习·batch
阆遤1 个月前
Windows环境安装Hermes Desktop安装
powershell·ai agent·hermes desktop
AI浩1 个月前
梯度累积与 Micro-Batch 设计分层式精讲:有效批次、显存边界与分布式同步
开发语言·分布式·batch