Luis Valencia
Luis Valencia

Reputation: 33978

Test-Path : Illegal characters in path

I am trying to download a video series from channel9 using powershell, however I get this error on the first file, I think its the pipe character, but even with the replace statement its not working

 function Get-Media
{
    [CmdletBinding()]
    param
    (
        [Object]
        $url,
        [Object]
        $title,
        [Object]
        $path
    )

    $u = New-Object System.Uri($url)
    $name = $title
    $extension = [System.IO.Path]::GetExtension($u.Segments[-1])
    $fileName = $name + $extension

    $fileName = $fileName -replace "’", ''
    $fileName = $fileName -replace "\?", ''
    $fileName = $fileName -replace ":", ''
    $fileName = $fileName -replace '/', ''
    $fileName = $fileName -replace ",", ''
    $fileName = $fileName -replace '"', ''
    $fileName = $fileName -replace '|', ''
    $fileName = $fileName -replace '\#', ''
    $fileName = $fileName -replace '-', ''

    $fileName

    if (Test-Path($fileName)) {
        Write-Host 'Skipping file, already downloaded' -ForegroundColor Yellow
    }
    else
    {
        Invoke-WebRequest $url -OutFile (Join-Path -Path $path -ChildPath $fileName)
    }
}

function Get-VideosFromFeed
{
    [CmdletBinding()]
    param
    (
        [Object]
        $feedUrl,
        [Object]
        $folder,
        [Object]
        $path
    )

    $feed=[xml](New-Object System.Net.WebClient).DownloadString($feedUrl)

    $downloadPath = (Join-Path -Path $path -ChildPath $folder)

    if (Test-Path($downloadPath)) {
        Write-Host 'Skipping folder, already exists' -ForegroundColor Yellow
    }
    else
    {
        New-Item -Path $downloadPath -ItemType directory -WarningAction SilentlyContinue
    }

    foreach($i in $feed.rss.channel.item) {
        foreach($m in $i.group){
            foreach($u in $m.content `
                    | Where-Object { `
                            $_.url -like '*mid.mp4' `
                         } | Select-Object -Property @{Name='url'; Expression = {$_.url}}, `
                                                     @{Name='title'; Expression = {$i.title}})
            {
                Get-Media -url $u.url -title $u.title -path $downloadPath
            }
        }
    }
}

$physicalPath = "D:\Videos\Series"

Get-VideosFromFeed -feedUrl 'https://channel9.msdn.com/Blogs/azurestack/feed/mp4high'                                                                          -path $physicalPath -folder 'azurestack'

Upvotes: 2

Views: 22611

Answers (4)

user5062856
user5062856

Reputation:

You could also try this:

$FileName.Split([IO.Path]::GetInvalidFileNameChars()) -join ''

It seems quite effective to me :)

Upvotes: 5

Ryan Bemrose
Ryan Bemrose

Reputation: 9266

This line doesn't do anything:

$fileName = $fileName -replace '|', ''

The first argument to -replace is a regex. It's finding every empty string in $fileName and replacing it with the empty string. Aside from wasting a bunch of CPU cycles, this has no effect.

You need to escape the pipe character inside the regex.

$fileName = $fileName -replace '\|', ''

Upvotes: 1

n01d
n01d

Reputation: 1077

I believe, you should replace this strange piece of code:

$fileName = $fileName -replace "’", ''
$fileName = $fileName -replace "\?", ''
$fileName = $fileName -replace ":", ''
$fileName = $fileName -replace '/', ''
$fileName = $fileName -replace ",", ''
$fileName = $fileName -replace '"', ''
$fileName = $fileName -replace '|', ''
$fileName = $fileName -replace '\#', ''
$fileName = $fileName -replace '-', ''

With this:

$fileName = $fileName -replace '(-|#|\||"|,|/|:|â|€|™|\?)', ''

I work behind a strict proxy and can't test the entire process, but at least I don't get the 'Illegal characters in path' error.

Btw you don't need to replace all of the stuff. Only theese characters: <>:"/\|?* according to naming rules. So the better regex pattern should be like this: -replace '\<|\>|:|"|/|\\|\||\?|\*', ''

Upvotes: 4

Johnny Grimes
Johnny Grimes

Reputation: 411

It looks like are calling Get-VideosFromFeed without the $Folder or $Path parameters.

You might need to set some default values or hand them in when calling the function.

Try calling it like this:

Get-VideosFromFeed -feedUrl 'https://channel9.msdn.com/Blogs/azurestack/feed/mp4high' -folder "Folder" -Path "C:\Path"

Upvotes: 1

Related Questions