9

I have a file called Eric Clapton - Nothing But The Blues - Full Concert (1994) [HQ].URL in a directory C:\Hans\Hans4\.

Code to create this is below (though the error occurs regardless of file contents)

$fileContents = @"
[{000214A0-0000-0000-C000-000000000046}]
Prop3=19,2
[InternetShortcut]
URL=http://example.com/
IDList=
"@

New-Item -Path "C:\Hans\Hans4" `
         -Name "Eric Clapton - Nothing But The Blues - Full Concert (1994) [HQ].URL" `
         -ItemType "file" `
         -Value $fileContents `
         -Force

When I run the following I get an error

Get-ChildItem 'C:\Hans' -Recurse | Resolve-ShortcutFile > Output.txt

The code references the function below

function Resolve-ShortcutFile {         
    param(
        [Parameter(
            ValueFromPipeline=$true,
            ValueFromPipelineByPropertyName=$true,
            Position = 0)]
        [Alias("FullName")]
        [string]$fileName
    )
    process {
        if ($fileName -like "*.url") {
            Get-Content $fileName | Where-Object {
                $_ -like "url=*"
            } |
            Select-Object @{
                Name='ShortcutFile'
                Expression = {Get-Item $fileName}
            }, @{
                Name='Url'
                Expression = {$_.Substring($_.IndexOf("=") + 1 )}
            } 
        }
    }
}

This is the error message

Get-Content : An object at the specified path C:\Hans\Hans4\Eric Clapton - Nothing But 
The Blues - Full Concert (1994) [HQ].URL does not exist, or has been filtered by 
the -Include or -Exclude parameter.
At line:33 char:28
+             Get-Content $fileName | Where-Object {
+             ~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (System.String[]:String[]) [Get-Content], Exception
    + FullyQualifiedErrorId : ItemNotFound,Microsoft.PowerShell.Commands.GetContentCommand

Why do I get this error?

2

1 Answer 1

15

The problem is the filename that is piped into the function and passed to Get-Content

C:\Hans\Hans4\Eric Clapton - Nothing But The Blues - Full Concert (1994) [HQ].URL

You are encountering the problem described here.

It contains square brackets that are interpreted as a range operator with the set H and Q

So the pattern would mean that it attempts to read the contents of files with either of the following names...

  • Eric Clapton - Nothing But The Blues - Full Concert (1994) H.URL
  • Eric Clapton - Nothing But The Blues - Full Concert (1994) Q.URL

... but will not match the literal [HQ] in

  • Eric Clapton - Nothing But The Blues - Full Concert (1994) [HQ].URL

You can use the -literalPath parameter to avoid this problem and have the file name treated literally.

function Resolve-ShortcutFile {         
    param(
        [Parameter(
            ValueFromPipeline=$true,
            ValueFromPipelineByPropertyName=$true,
            Position = 0)]
        [Alias("FullName")]
        [string]$fileName
    )
    process {
        if ($fileName -like "*.url") {
            Get-Content  -literalPath $fileName | Where-Object {
                $_ -like "url=*"
            } |
            Select-Object @{
                Name='ShortcutFile'
                Expression = {Get-Item  -literalPath $fileName}
            }, @{
                Name='Url'
                Expression = {$_.Substring($_.IndexOf("=") + 1 )}
            } 
        }
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

I had about 200 of these files and none did pass. It did work with the same script about 2 months ago. But I will sure try out your solution as soon as i'm back home.
@Roman - is the [HQ] a new naming convention that you (or a website that you frequent and create internet shortcuts from) has adopted? Because it is that that is causing the problem with your existing code. Most of the time your existing code will work fine unless it encounters a file name with square brackets.
Those are links from youtube. I just don't get it how it worked befor and now not anymore. Maybe they updated the powershell.
@Roman - I can't answer that. I've got no idea about the history of your files and when they were created but Get-Content "H:\Hans\Hans4\Eric Clapton - Nothing But The Blues - Full Concert (1994) [HQ].URL" would never have worked as you want.
thanks it worked perfect! And thanks for editing the question, i first thought it was Coeur.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.