0

I am very curious about this as it took me a while but I couldn't figure it out

First, I ran the following script to get all Zip files in a dir

$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
        }
    }

it showed as below:

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

Then I tried to delete the first one(2016-08-30_21-15-17_server-1.1.20558_client-1.1.20518 - Copy - Copy.zip) with remove-item like this:

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

I got a path too long exception. However, if I put the file name in "Remove-Item" instead of passing it by $entryList[0], it worked

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
user1888955
  • 626
  • 1
  • 9
  • 27
  • As an aside: You could simply do `$entryList = Get-ChildItem -Path "\\tools-backup.nas\Tools-Backup\FakeS3\Rollback\$serverName" *.zip -ErrorAction Stop | sort -Property "LastWriteTime"`. – mklement0 Nov 06 '16 at 02:50

2 Answers2

2

Your issue is using '$entryList[0]' in your quoted string.

Run this code to see how this works (or doesn't work)...

$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

You can try something like:

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

Also, instead of using Name, you might refactor your code to use FullName...

$entryList.Add($_.FullName)

Enjoy.

Kory Gill
  • 6,993
  • 1
  • 25
  • 33
1

Kory Gill is correct. When you reference a string array inside a string enclosed by double quotes, you will need to add $( before the array name, and a ) after. So,

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

Will not give you the result you want. The below will correctly get the string in the array and insert in the string.

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

It's one of these little "gotchas" that got me when I first started out on PowerShell.