【问题标题】:Remove-Item not working when a file name is provided in a variable [duplicate]在变量中提供文件名时,Remove-Item 不起作用[重复]
【发布时间】:2017-03-19 08:51:27
【问题描述】:

我对此很好奇,因为我花了一段时间,但我想不通

首先,我运行以下脚本来获取目录中的所有 Zip 文件

$entryList = New-Object System.Collections.ArrayList

Get-ChildItem -Path "\\tools-backup.nas\Tools-Backup\FakeS3\Rollback\$serverName" -ErrorAction Stop | sort -Property "LastWriteTime" | ForEach-Object {
    if($_.Name.Contains(".zip")) {
            $entryList.Add($_.Name) | Out-Null
        }
    }

显示如下:

2016-08-30_21-15-17_server-1.1.20558_client-1.1.20518 - Copy - Copy.zip
2016-08-30_21-15-17_server-1.1.20558_client-1.1.20518 - Copy (2).zip
2016-08-30_21-15-17_server-1.1.20558_client-1.1.20518 - Copy (3).zip
2016-08-30_21-15-17_server-1.1.20558_client-1.1.20518 - Copy.zip
2016-08-30_21-15-17_server-1.1.20558_client-1.1.20518 - Copy (6).zip
2016-08-30_21-15-17_server-1.1.20558_client-1.1.20518 - Copy - Copy (2).zip

然后我尝试使用这样的删除项目删除第一个(2016-08-30_21-15-17_server-1.1.20558_client-1.1.20518 - Copy - Copy.zip):

Remove-Item -Path "\\tools-backup.nas\Tools-Backup\FakeS3\Rollback\$serverName\$entryList[0]" -ErrorAction Stop

Remove-Item : The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.
At line:1 char:1
+ Remove-Item -Path "\\tools-backup.nas\Tools-Backup\FakeS3\Rollback\$s ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ReadError: (\\toolsbackup....lback\autopatch:String) [Remove-Item], PathTooLongException
+ FullyQualifiedErrorId : DirIOError,Microsoft.PowerShell.Commands.RemoveItemCommand

我得到了一个路径太长的异常。但是,如果我将文件名放在“Remove-Item”中而不是通过 $entryList[0] 传递它,它就起作用了

Remove-Item -Path "\\tools-backup.nas\Tools-Backup\FakeS3\Rollback\$serverName\2016-08-30_21-15-17_server-1.1.20558_client-1.1.20518 - Copy (2).zip" -ErrorAction Stop

【问题讨论】:

  • 顺便说一句:你可以简单地做$entryList = Get-ChildItem -Path "\\tools-backup.nas\Tools-Backup\FakeS3\Rollback\$serverName" *.zip -ErrorAction Stop | sort -Property "LastWriteTime"。

标签: powershell powershell-2.0 powershell-3.0 powershell-4.0 powershell-remoting


【解决方案1】:

您的问题是在引用的字符串中使用了 '$entryList[0]'。

运行此代码以查看其工作原理(或不工作)...

$entryList = New-Object System.Collections.ArrayList
$entryList.Add("This is an entry.")

"Broken"
# This is a string with: This is an entry.[0]
Write-Output "This is a string with: $entryList[0]"

"Fixed1"
# This is a string with: This is an entry.
Write-Output "This is a string with: $($entryList[0])"

# or...
"Fixed2"
# This is a string with: This is an entry.
$item = "This is a string with: {0}" -f $entryList[0]
Write-Output $item

你可以试试这样的:

Remove-Item -Path "\\tools-backup.nas\Tools-Backup\FakeS3\Rollback\$serverName\$($entryList[0])" -ErrorAction Stop

此外,您可以重构代码以使用 FullName...

,而不是使用 Name
$entryList.Add($_.FullName)

享受吧。

【讨论】:

  • 感谢您的解释:)
【解决方案2】:

Kory Gill 是正确的。当您在用双引号括起来的字符串中引用字符串数组时,您需要在数组名称之前添加$(,并在之后添加)。所以,

Write-Host "This is a $test[0]"

不会给你想要的结果。下面将正确获取数组中的字符串并插入到字符串中。

WRite-Host "This is a $($test[0])"

当我第一次开始使用 PowerShell 时,这些小“陷阱”之一让我着迷。

【讨论】:

  • 感谢您提供详细信息 :)
  • 请记住,如果您希望 PowerShell 先计算某些内容,请使用 "Something $($RunThisFirst)" 标注的子表达式,然后 PowerShell 先计算括号内的内容。
  • @user4317867 谢谢,我已经写在我的笔记上:)
猜你喜欢
  • 2014-10-25
  • 1970-01-01
  • 2015-12-16
  • 2016-06-30
  • 1970-01-01
  • 2019-02-11
  • 2020-01-07
  • 2014-06-06
  • 2013-10-27
相关资源
最近更新 更多