删除 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 $($args\[0\]) } 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

相关推荐
wtsolutions5 天前
Batch Conversion Online JSON Files (from URL) to Excel by WTSolutions
json·excel·batch
开开心心就好14 天前
PDF转图片工具,一键转换高清无损
服务器·前端·智能手机·r语言·pdf·excel·batch
Freshman小白19 天前
Fluent自动化仿真(TUI命令脚本教程)
自动化·脚本·仿真
巴伦是只猫23 天前
【深度学习笔记 Ⅱ】8 mini-batch 梯度下降法
笔记·深度学习·batch
点云SLAM24 天前
Windows CMD(命令提示符)中最常用的命令汇总和实战示例
windows·microsoft·visual studio·powershell·cmd命令提示符·bat文件·win环境批处理
等不到来世25 天前
npm : 无法加载文件 C:\Program Files\nodejs\npm.ps1
npm·powershell
Ronin-Lotus1 个月前
上位机知识篇---Prompt&PowerShell Prompt
prompt·powershell
开开心心_Every1 个月前
免费PDF文件格式转换工具
java·智能手机·pdf·word·batch·java-zookeeper
Мартин.1 个月前
Operation Blackout 2025: Smoke & Mirrors
powershell
山海上的风1 个月前
Spring Batch终极指南:原理、实战与性能优化
spring·性能优化·batch·springbatch