0

我正在使用 powershell 脚本精简我的备份文件,并且我知道我有正确的文件名,但是由于某种原因,当我使用 remove-item 时,该项目不会被删除并且不会引发异常。这是它的样子:

try{
   $Drive = "E:\temp\"
   $deleteTime = -42
   $limit = (Get-Date).AddDays($deleteTime) #files older than 6 weeks

   #get files in folder older than deleteTime and with signature of *junk.vhd* (to be changed later)
   $temp1 = Get-ChildItem -Path $Drive -filter "*junk.vhd*" | Where-Object {$_.LastWriteTime -lt $limit} | Select -Expand Name  #this has 5 files in list

   #will delete every other file
   for($i=$temp1.GetLowerBound(0);$i -le $temp1.GetUpperBound(0);$i+=2) {
      $name = $temp1[$i]
      Write-Host "removing $name" #prints correct file names to screen
      Get-ChildItem -Path $Drive -include $name | Remove-Item -recurse -force #this is handling correct files but they aren't deleted for some reason
   }
}#try
Catch [Exception] {
   #nothing is caught
   Write-Host "here"
}

有谁知道为什么它会找到并写入主机要删除的正确文件名,但 Remove-Item 没有删除它们?

我正在研究删除一个有点不同的例子,但一切看起来都很好。

4

1 回答 1

0

尝试替换这一行:

Get-ChildItem -Path $Drive -include $name | Remove-Item -recurse -force #this is handling correct files but they aren't deleted for some reason

有了这个:

Remove-Item $temp1[i].FullName -force -recurse

由于您已经获得了文件的完整路径,因此我认为没有必要Get-ChilItem再次调用并通过管道传递它,而只需使用 FUllName 属性提供 Remove-Item ,这是文件的完整路径。

于 2014-08-17T13:08:29.570 回答