Powershell script to download and name files
powershell, powershell-2.0
Solution
If you must have the exact date/time shown on the page, you need to turn the solution around. Start by examining the index page (where your second excerpt came from), then work with that that to extract the URLs and timestamps, and download as you're doing but with the new file name.
Except...you can't easily parse arbitrary HTML with regular expressions. Now, if you know that this HTML is pretty static in how it's formed, you can probably get away with it. But be prepared for things to break if the court changes their page even the tiniest bit.
If you don't need that level of precision, you can use the HTTP response headers that come along with the file when you download it. From this, you can get the `Last-Modified` date as reported by the server - the last date/time the file was modified on the server itself. This isn't necessarily the date you see in the web page, but rather when they put the file out there (so if there was a 2 hour lag from production to publishing, you might see that difference).
R is my RAMdisk I use for temp stuff. Fix your paths as needed.
$client = New-Object system.net.WebClient;
$client.DownloadFile("http://app1.co.madison.il.us/circuitclerk/dockets/63/489641.TXT","r:\tempfile.txt");
$updated = Get-Date $wc.ResponseHeaders["Last-Modified"] -Format "yyyyMMdd";
Rename-Item -Path "r:\tempfile.txt" -NewName "r:\July-Updated$updated.txt";
If you were using PowerShell 3.0, you could use `invoke-webrequest` to get the file into memory then write it directly out to disk with the appropriate name, as `invoke-webrequest` returns an object containing both the response data and headers which you can then handle as needed.
Still another option would be to contact the court and see if they have another, more machine-friendly, method of accessing the data. An RSS or XML feed, or some other kind of gateway that is meant for what you're trying to do.
Problem
So I've got a powershell script that goes out at certain times and downloads files from a site and saves it to our network. It's really super simple but I have one issue I need fixed. First, the code: ``` $client = new-object system.Net.Webclient $client.DownloadFile("http://app1.co.madison.il.us/circuitclerk/dockets/63/489641.TXT","\\risokcdatp001\automated_data\PeopleInfo\DataFile\Traffic\IL\Madison\July_ $(get-date -f yyyyMMdd.TXT)") $client.DownloadFile("http://app1.co.madison.il.us/circuitclerk/dockets/63/599256.TXT","\\risokcdatp001\automated_data\PeopleInfo\DataFile\Traffic\IL\Madison\August_ $(get-date -f yyyyMMdd.TXT)") $client.DownloadFile("http://app1.co.madison.il.us/circuitclerk/dockets/63/429855.TXT","\\risokcdatp001\automated_data\PeopleInfo\DataFile\Traffic\IL\Madison\September_ $(get-date -f yyyyMMdd.TXT)") ``` You can see the site it goes to and then it deposits the files on our network. The problem is that I need to name the files based on the date and time uploaded thats on the site. See the following HTML code for this: ``` <td width="65%" colspan="2"> <div align="center"><font size="3"><a href="http://app1.co.madison.il.us/circuitclerk/dockets/63/489641.TXT" target="_blank"><b>MONTH OF JULY 2013</b></a></font></div> </td> <td> <div align="center"><font size="3"><b>July 05, 2013 (11:19 AM)</b></font></div> </td> ``` How do I get powersheel to get that value (July 05, 2013 (11:19 AM)) and use it in my file name like the following: July-UpdatedYYYYMMDD.txt? Thanks!