如何使用.bat脚本从非静态链接下载文件



我正在尝试使用稍后将使用任务管理器运行的脚本下载所有King County excel文件的按日期计算的总数。我被卡住了,因为链接不是静态的,它的命名约定很可能在接下来的几个月里会改变。

这是我迄今为止写的代码:

@echo off
::Script to download COVID-19 Data
echo "Downloading Total counts by date for all King County"
powershell -Command "Invoke-WebRequest https://www.kingcounty.gov/depts/health/covid-19/data/~/media/depts/health/communicable-diseases/documents/C19/data/covid-data-daily-counts-sept-14.ashx -Outfile CovidData.xlsx
echo "Download has been successful!"
cls
pause

我想知道是否有办法添加一张外卡,比如"*"在invoke-webrequest中;sept-14";链接的一部分。

链接:https://www.kingcounty.gov/depts/health/covid-19/data/daily-summary.aspx

需要脚本才能使用任务管理器自动下载的链接(所有金县按日期计算的总数(:https://www.kingcounty.gov/depts/health/covid-19/data/~/media/depts/health/communicable数据库/文档/C19/data/covid-data-daily-counts-sept-14.ashx

我用Windows Powershell ISE创建并测试了Powershell脚本,它可以运行5/5,希望它也能为您工作!


$start_time = Get-Date
$url = "https://www.kingcounty.gov/depts/health/covid-19/data/daily-summary.aspx"
$xlFile = "E:TestCovidData.xlsx"
$http_request = New-Object -ComObject Microsoft.XMLHTTP
$http_request.open('GET', $url, $false)
#Sending the request
$http_request.send()
$Contents = $http_request.ResponseText
$pattern = "([w-.,@?^=%&/~+#]*[w-@?^=%&/~+#])(.ashx)"
$Links = $Contents | Select-String $pattern -AllMatches | ForEach-Object {$_.Matches.Value} | sort -unique
$Filter = $Links -match "data-daily"
$MyUrlFile = "https://www.kingcounty.gov/depts/health/covid-19/data/" + $Filter
$MyUrlFile
Invoke-WebRequest "$MyUrlFile" -Outfile $xlFile
if (Test-Path $xlFile) { Start $xlFile }
Write-Output "Running Script Time taken is : $((Get-Date).Subtract($start_time).Seconds) second(s)"

最新更新