删除 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

相关推荐
淋了一场太阳雨1 天前
SAP LSMW (二) - Batch Input Recording
sap·batch·lsmw
gis分享者1 天前
如何在 Shell 脚本中如何使用条件判断语句?(中等)
面试·shell·脚本·语法·使用·判断·条件
性感博主在线瞎搞2 天前
【神经网络】超参调优策略(二):Batch Normalization批量归一化
人工智能·神经网络·机器学习·batch·批次正规化
小孟的CDN3 天前
使用pytorch进行batch_size分批训练,并使用adam+lbfgs算法——波士顿房价预测
pytorch·算法·batch·代码·adam+lbfgs
Hello.Reader3 天前
Flink SQL 的 TRUNCATE 用法详解(Batch 模式)
sql·flink·batch
課代表5 天前
bat 批处理中的路径:%CD%与%~dp0
脚本·bat·环境变量·目录·批处理·路径·相对路径
世转神风-5 天前
windows-ps1脚本-获取网线直连下文件路径中的文件名
windows·脚本
世转神风-5 天前
ps1脚本-运行报错-并带有乱码
windows·脚本
課代表7 天前
bat 批处理从文本文件自动创建文件夹
自动化·脚本·bat·批处理·txt·文件编码·文件夹创建
添加shujuqudong1如果未回复8 天前
MATLAB 中提取冲击信号的解卷积方法探索
batch