0

I have a script that nearly works but I need to add in either parsing or a wildcard on the URL as the URL will change to characters I won’t know each month. I have to use New-Object System.Net.WebClient because invoke web request is blocked. So, I was thinking if anyone knows how to download the link using the characters that I will know and strip off the rest of the link.

Examples of the links below

Full-CSV-data-file-Jan19-ZIP-3633K-53821.zip
Full-CSV-data-file-Dec18-ZIP-3427K.zip
Full-CSV-data-file-Nov18-ZIP-3543K-21860.zip

So on the above links i know the latest file will have Jan19 in it and it's a zip file. The script i am using is

$currentMonthNo = get-date -format "MM"
$currentMonthName = (get-date((get-date).addmonths(-2)) -format MMM)
$currentYearNo = get-date –format yy
$url = "http://www.website.com/Full-CSV-data-file-$currentMonthName$currentYearNo-ZIP-3633K-53821.zip"
$output = "C:\Folder\Full-CSV-data-file-Jan19-ZIP-3633K-53821.zip"
$start_time = Get-Date
$wc = New-Object System.Net.WebClient
$wc.DownloadFile($url, $output)
#OR
(New-Object System.Net.WebClient).DownloadFile($url, $output)

Write-Output "Time taken: $((Get-Date).Subtract($start_time).Seconds) second(s)"
Write-Output $url
Start-Sleep -s 6
Jimbo
  • 1
  • 1
  • I am unsure what you are asking here. Are we trying to rename the zip file after you download it? – Matt Mar 15 '19 at 13:43
  • I'm trying to automate downloading the file Full-CSV-data-file-Jan19-ZIP-3633K-53821.zip but next month it could be Full-CSV-data-file-Feb19-ZIP-44466K-77766.zip i will only know the month has incremented 1 month and the year is current year and that it's a zip file the rest of the link i won't know how it looks – Jimbo Mar 15 '19 at 13:53
  • 1
    You can't wildcard URL's. You need to check something else first that gets you the names. Scrape the page before for example – Matt Mar 15 '19 at 13:55
  • Hence the question, how would i parse links or regex for zip file using System.Net.WebClient – Jimbo Mar 15 '19 at 14:00
  • So this is the part I am not clear on. Do you already have the url and you are just parsing that text to get the file name out? You are trying to determine what `$output` would look like instead of hard coding it? – Matt Mar 15 '19 at 14:07
  • I don't have the URL i will only know a certain part of next months URL – Jimbo Mar 15 '19 at 14:29
  • 1
    Start here then: https://stackoverflow.com/questions/49418802/getting-links-from-webpage-in-powershell-using-regular-expression – Matt Mar 15 '19 at 14:30

0 Answers0