gpt4 book ai didi

powershell - 仅当使用Powershell更改了文件时,才可以下载文件

转载 作者:行者123 更新时间:2023-12-04 23:00:30 25 4
gpt4 key购买 nike

我正在使用这个简单的功能来下载文件:

function DownloadFile([string]$url, [string]$file)
{
$clnt = new-object System.Net.WebClient
Write-Host "Downloading from $url to $file "
$clnt.DownloadFile($url, $file)
}

它工作正常,但是我正在使用的脚本调用了很多次,目前可能意味着多次下载文件。

如果文件本地不存在或服务器版本较新(例如服务器上的LastModifiedDate大于本地LastModifiedDate),我如何才能修改功能以仅下载?

编辑:
到目前为止,这是我所能得到的,似乎可以运行,但是希望不对服务器进行2次调用。
function DownloadFile([string]$url, [string]$file)
{
$downloadRequired = $true
if ((test-path $file))
{
$localModified = (Get-Item $file).LastWriteTime
$webRequest = [System.Net.HttpWebRequest]::Create($url);
$webRequest.Method = "HEAD";
$webResponse = $webRequest.GetResponse()
$remoteLastModified = ($webResponse.LastModified) -as [DateTime]
$webResponse.Close()

if ($remoteLastModified -gt $localModified)
{
Write-Host "$file is out of date"
}
else
{
$downloadRequired = $false
}

}

if ($downloadRequired)
{
$clnt = new-object System.Net.WebClient
Write-Host "Downloading from $url to $file"
$clnt.DownloadFile($url, $file)
}
else
{
Write-Host "$file is up to date."
}
}

最佳答案

这个星期我一直在跳动,想出了这个

# ----------------------------------------------------------------------------------------------
# download a file
# ----------------------------------------------------------------------------------------------
Function Download-File {
Param (
[Parameter(Mandatory=$True)] [System.Uri]$uri,
[Parameter(Mandatory=$True )] [string]$FilePath
)

#Make sure the destination directory exists
#System.IO.FileInfo works even if the file/dir doesn't exist, which is better then get-item which requires the file to exist
If (! ( Test-Path ([System.IO.FileInfo]$FilePath).DirectoryName ) ) { [void](New-Item ([System.IO.FileInfo]$FilePath).DirectoryName -force -type directory)}

#see if this file exists
if ( -not (Test-Path $FilePath) ) {
#use simple download
[void] (New-Object System.Net.WebClient).DownloadFile($uri.ToString(), $FilePath)
} else {
try {
#use HttpWebRequest to download file
$webRequest = [System.Net.HttpWebRequest]::Create($uri);
$webRequest.IfModifiedSince = ([System.IO.FileInfo]$FilePath).LastWriteTime
$webRequest.Method = "GET";
[System.Net.HttpWebResponse]$webResponse = $webRequest.GetResponse()

#Read HTTP result from the $webResponse
$stream = New-Object System.IO.StreamReader($webResponse.GetResponseStream())
#Save to file
$stream.ReadToEnd() | Set-Content -Path $FilePath -Force

} catch [System.Net.WebException] {
#Check for a 304
if ($_.Exception.Response.StatusCode -eq [System.Net.HttpStatusCode]::NotModified) {
Write-Host " $FilePath not modified, not downloading..."
} else {
#Unexpected error
$Status = $_.Exception.Response.StatusCode
$msg = $_.Exception
Write-Host " Error dowloading $FilePath, Status code: $Status - $msg"
}
}
}
}

关于powershell - 仅当使用Powershell更改了文件时,才可以下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26533819/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com