[英]PowerShell Remove-Item silently doesn't remove a single file
I have mixed together a PowerShell script that browses folders recursively searching for too low resolution pictures. 我将PowerShell脚本混合在一起,该脚本以递归方式浏览文件夹以搜索分辨率过低的图片。
I have googled for a solution for couple of hours and so far I haven't managed to get those images deleted. 我已经搜索了几个小时的解决方案,到目前为止,我还没有设法删除这些图像。 So what's the deal with this?
那么这有什么用呢?
Turns out that square brackets in file names were causing problems. 原来文件名中的方括号引起了问题。 But now Remove-Item complains that the files are used by another process, which I assume is this script itself.
但是现在,Remove-Item抱怨文件被另一个进程使用,我认为这是脚本本身。
$kuveja = 0
$pikkusii = 0
[void][reflection.assembly]::loadwithpartialname("system.drawing")
function Get-Image{
process {
$file = $_
[Drawing.Image]::FromFile($_.FullName) |
ForEach-Object{
$_ | Add-Member -PassThru NoteProperty FullName ('{0}' -f $file.FullName)
}
}
}
$path = 'H:\Juttui\'
Get-ChildItem $path\* -Include *.jpg, *.jpeg* -Recurse | ? {
$kuva = $_ | Get-Image
if($kuva.Width -lt 960 -and $kuva.Height -lt 960){
Write-Host $_.FullName -fore red
Remove-Item $_.FullName -Force
$pikkusii++
}
elseif($kuva.Width -lt 540 -or $kuva.Height -lt 540){
Write-Host $_.FullName -fore yellow
Remove-Item $_.FullName -Force
$pikkusii++
}
$kuveja++
}
Write-Host "Pics browsed: $kuveja" -fore green
Write-Host "Small: $pikkusii" -fore green
Please understand that I am complete noob in PowerShell and I don't know a better place to ask this than here. 请了解,我在PowerShell中是完全菜鸟,而且我不知道有比这更好的地方了。
I think your $kuva
reference is keeping the file open. 我认为您的
$kuva
参考使文件保持打开状态。 Try performing the .Dispose()
method before trying to remove the object. 在尝试删除该对象之前,请尝试执行
.Dispose()
方法。 So something like this: 所以像这样:
Get-ChildItem $path\* -Include *.jpg, *.jpeg* -Recurse | ? {
$kuva = $_ | Get-Image
if($kuva.Width -lt 960 -and $kuva.Height -lt 960){
Write-Host $_.FullName -fore red
$kuva.dispose()
Remove-Item $_.FullName -Force
$pikkusii++
}
elseif($kuva.Width -lt 540 -or $kuva.Height -lt 540){
Write-Host $_.FullName -fore yellow
$kuva.dispose()
Remove-Item $_.FullName -Force
$pikkusii++
}
$kuveja++
}
Get-ChildItem $path\* -Include *.jpg, *.jpeg* -Recurse | ? {
# ...
}
?
means Where-Object
, but I think what you really want is ForEach-Object
. 表示
Where-Object
,但我认为您真正想要的是ForEach-Object
。 The alias for ForEach-Object
is %
, but you should not use aliases in scripts for this very reason (it's less clear). ForEach-Object
的别名为%
,但是出于这个原因,您不应该在脚本中使用别名(不清楚)。
Try this: 尝试这个:
Get-ChildItem $path\* -Include *.jpg, *.jpeg* -Recurse | ForEach-Object {
# ...
}
If that doesn't work, you have Write-Host
calls before the removes so are you seeing the red and yellow text that indicates the remove should be happening? 如果这不起作用,则在删除之前您有
Write-Host
调用,因此您是否看到红色和黄色的文本指示应该进行删除? You should try to run your script in ISE. 您应该尝试在ISE中运行脚本。 Set a breakpoint right before the
Remove-Item
call, then step through the code. 在
Remove-Item
调用之前设置一个断点,然后逐步执行代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.