powershell - 如何删除 Powershell 脚本本身正在使用的文件?
问题描述
我正在尝试遍历给定目录中的所有图片,检查它们的大小和尺寸。当某些属性与我的约束不匹配时,我想立即删除该文件。否则我想执行一些其他操作。
Add-Type -AssemblyName System.Drawing
$maxFileSizeKB = 100
$minPicWidth = 500
$minPicHeight = 500
foreach ($file in Get-ChildItem -Path ..\pics) {
$fname = $file.fullname
$fsizeKB = $file.length/1KB
$image = [System.Drawing.Image]::FromFile($file.FullName)
$iWidth = $image.width
$iHeight = $image.height
$fLastWrite = $file.LastWriteTime
if( $fsizeKB -gt $maxFileSizeKB -or
$iWidth -lt $minPicWidth -or
$iHeight -lt $minPicHeight) {
Write-Host "`tDoes'nt match criteria - deleting and continueing with next Image ..."
Remove-Item -Force $fname
continue
}
Write-Host "other action"
}
我希望通过相应的输出删除尺寸或尺寸太小的图片。如果图片符合所有要求,我想查看输出“其他操作”
它的工作原理是删除,这给了我这个错误:
Remove-Item : Das Element pics\tooSmall2.PNG kann nicht entfernt werden: Der Prozess kann nicht auf die Datei "pics\tooSmall2.PNG" zugreifen, da sie von einem anderen Prozess verwendet wird。 在 PowerShell\ADPhotoHandler.ps1:27 Zeichen:9 + 删除项目 -Force $fname + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : WriteError: (\tooSmall2.PNG:FileInfo) [Remove-Item], IOException + FullyQualifiedErrorId : RemoveFileSystemItemIOError,Microsoft.PowerShell.Commands.RemoveItemCommand
解决方案
推荐阅读
- python - python 3中将输入值分配给变量的函数
- java - 在以下测试用例中获取 NotAMockException
- c# - 选择选项卡时Xamarin表单选项卡式页面加载数据
- kubernetes - 在 kubernetes pod 中运行 bash 脚本
- arrays - 将项目添加到数组(状态)并且它正在更新状态中的先前记录
- java - 强制在android的searchview中将第一个单词写为大写
- javascript - 如何在继续调用函数之前完成被调用函数以便可以使用返回的数据?
- java - 在调用 void ServiceImplementationLayer 方法的 JUnit 测试用例上抛出 Mockito UnfinishedStubbingException
- javascript - 在本地运行html文件时需要使用axios的想法(例如浏览器中的file://路径)
- c# - 我怎样才能通过他们的属性来上课