简体   繁体   English

如果最近没有使用 Powershell 修改过,请从 URL 下载文件

[英]Download file from URL if not modified recently using Powershell

I have a Powershell script that is downloading files continuously from a URL public directory.我有一个 Powershell 脚本,它正在从 URL 公共目录连续下载文件。 The script is supposed to download files from the directory that does not currently exist in our local directory.该脚本应该从我们本地目录中当前不存在的目录下载文件。 The script looks like this:该脚本如下所示:

$outputdir = 'C:\mydir\public\demos'
$url       = 'https://xxx.xxx.net/fastdl/xxx/xxx/public/'

# enable TLS 1.2 and TLS 1.1 protocols
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12, [Net.SecurityProtocolType]::Tls11

$WebResponse = Invoke-WebRequest -Uri $url
# get the list of links, skip the first one ("../") and download the files
$WebResponse.Links | Select-Object -ExpandProperty href -Skip 0 | ForEach-Object {
    Write-Host "Downloading file '$_'"
    $filePath = Join-Path -Path $outputdir -ChildPath $_
    $fileUrl  = '{0}/{1}' -f $url.TrimEnd('/'), $_
    
    if (Test-Path($filePath)) 
    {
        Write-Host 'Skipping file, already downloaded' -ForegroundColor Yellow
        return
    }
    Invoke-WebRequest -Uri $fileUrl -OutFile $filePath
}

Now, since I want to download all files from the $url directory I need to check one more condition.现在,因为我想从$url目录下载所有文件,所以我需要再检查一个条件。 The file in the $url can't be modified in the last 10 minutes, if it has then the script should skip the file and download it once the last modification time is greater than 10 minutes. $url中的文件在最后 10 分钟内不能修改,如果有,则脚本应跳过该文件并在最后修改时间大于 10 分钟后下载它。

I have not found any syntax that seem to be able to fit an if statement like that.我还没有找到任何似乎能够适合这样的 if 语句的语法。 Any ideas?有任何想法吗?

I would change Test-Path to Get-Item so if that returns something, the file exists and you can examine its LastWriteTime property:我会将Test-Path更改为Get-Item ,这样如果它返回某些内容,则该文件存在并且您可以检查其 LastWriteTime 属性:

$WebResponse.Links | Select-Object -ExpandProperty href -Skip 0 | ForEach-Object {
    $filePath = Join-Path -Path $outputdir -ChildPath $_
    $fileUrl  = '{0}/{1}' -f $url.TrimEnd('/'), $_

    # set up a boolean fag to download or not
    $downloadThis = $true  
    if ($file = Get-Item -Path $filePath -ErrorAction SilentlyContinue) {
        # $file exists, check the LastWriteTime property
        if ($file.LastWriteTime -ge (Get-Date).AddMinutes(-10)) {
            Write-Host 'Skipping file, already downloaded' -ForegroundColor Yellow
            $downloadThis = $false
        }
    }
    if ($downloadThis) {
        Write-Host "Downloading file '$($file.Name)'"
        Invoke-WebRequest -Uri $fileUrl -OutFile $filePath
    }
}

I think all you need is to calculate time from now to the last modified time of the file.我想你所需要的只是计算从现在到文件的最后修改时间的时间。 I once write a function to check a file created less than 10 mins or more than 10 mins and action accordingly, Hope fully it will help我曾经写过一个 function 来检查创建时间少于 10 分钟或超过 10 分钟的文件并采取相应措施,希望它能有所帮助

#function to check whether a file is more than 10 mins age, if yes, return TRUE. If not, return FALSE
function file_age ($file){
#$file_info = Get-Item $file
$createtime = $file.CreationTime
$now = get-date

if (($now - $createtime).totalminutes -ge 10) {  
    Write-host "file [$file] created equal or more than 10 mins ago"
    return $true
    }
else
    {#Write-host  "file [$file] created within the 10 mins"
    return $false
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM