# 定义源目录和目标目录路径 $sourceDir = "D:\kep\Client\bin\Debug\vtk" # 源目录(要匹配文件名的目录) $targetDir = "D:\kep\Client\bin\Debug" # 目标目录(要删除文件的目录) # 检查目录是否存在 if (-not (Test-Path -Path $sourceDir -PathType Container)) { Write-Host "错误: 源目录不存在 [$sourceDir]" -ForegroundColor Red exit } if (-not (Test-Path -Path $targetDir -PathType Container)) { Write-Host "错误: 目标目录不存在 [$targetDir]" -ForegroundColor Red exit } # 获取源目录所有文件的文件名(不含路径) $sourceFiles = Get-ChildItem -Path $sourceDir -File | Select-Object -ExpandProperty Name if ($sourceFiles.Count -eq 0) { Write-Host "警告: 源目录中没有文件可匹配。" -ForegroundColor Yellow exit } # 获取目标目录文件并匹配删除 $targetFiles = Get-ChildItem -Path $targetDir -File $deletedCount = 0 foreach ($file in $targetFiles) { if ($file.Name -in $sourceFiles) { try { $filePath = $file.FullName Remove-Item -Path $filePath -Force -ErrorAction Stop Write-Host "已删除: $filePath" -ForegroundColor Green $deletedCount++ } catch { Write-Host "删除失败: $filePath - $_" -ForegroundColor Red } } } Write-Host "操作完成。共删除 $deletedCount 个文件。"