powershell - 如果文件存在,如何使用powershell跳过文件下载

标签 powershell

我有以下脚本从 channel 9 下载一些文件:

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 = $fileName -replace '(-|#|\||"|,|/|:|â|€|™|\?)', ''

    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/Series/Deep-Dive-into-the-Office-365-App-Model/feed/mp4high'                                            -path $physicalPath -folder 'Deep-Dive-into-the-Office-365-App-Model'

如果文件已经存在,我需要改进它以跳过下载。

最佳答案

您必须将整个路径传递给 Test-Path cmdlet 检查文件是否存在。那么您所要做的就是return功能:

# ....
$fileName = $fileName -replace '(-|#|\||"|,|/|:|â|€|™|\?)', ''
$filePath = Join-Path $path $fileName

if (Test-Path($filePath)) 
{
    Write-Host 'Skipping file, already downloaded' -ForegroundColor Yellow
    return
}

Invoke-WebRequest $url -OutFile $filePath

关于powershell - 如果文件存在,如何使用powershell跳过文件下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38172533/

相关文章:

powershell - 在ant build.xml上设置PATH环境变量在cygwin上有效,但在cmd或PowerShell上无效

powershell - Azure DevOps审核日志下载剩余api在给定月份内未下载经过过滤的数据

events - powershell 多运行空间事件传递

powershell - 启用 IIS 可选功能 IIS-ASPNET 和 IIS-ManagementConsole 时出错

windows - 如何检查 Docker 是否在 Windows 上运行?

powershell - 如何在 TFS Build 任务中获得代理用户能力?

powershell - 从变量而不是Powershell中的文件进行管道

powershell - 在 nuget init.ps1 中,您如何检测是作为安装还是作为控制台初始化运行

arrays - 如何测试一个对象是否是任何类型的数组或列表?

powershell - Windows Powershell快捷方式,用于删除先前命令的输出