question

CarlS-9581 avatar image
0 Votes"
CarlS-9581 asked cooldadtx commented

Powershell download jpg and timestamp test

Hi, i am looking to download a jpg every 2-4 mins from a public traffic camera as a project.
i want the file to download to say c:\temp\ from the source:
https://roads-waterways.transport.nsw.gov.au/trafficreports/cameras/camera_images/hunterexpressway_m1.jpg

i also need it to prepend the timestamp into the filename
for example:
hunterexpressway_m1- 10-08-2021-22:34.jpg

or something of the sort
any suggestions please? i cannot find one that works or does this task.

i managed this so far:

$WebClient = New-Object System.Net.WebClient
$WebClient.DownloadFile("https://roads-waterways.transport.nsw.gov.au/trafficreports/cameras/camera_images/hunterexpressway_m1.jpg","C:temphunterexpressway_m1hunterexpressway_m1.jpg")



sincerely

windows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

cooldadtx avatar image
0 Votes"
cooldadtx answered cooldadtx commented

I'm having a feeling of deja vu here as it seems like this question was asked a while back here or in the older forums.

Nevertheless what problem are you having? You give the code but you don't indicate what you need help with. Testing your code it appears you're getting an exception. The issue is your temp file path is invalid. You are attempting to create a really long filename on the C: drive and that syntax isn't going to work. Try something like "C:\temp\hunterexpressway_m1hunterexpressway_m1.jpg".

Note that in PS you generally don't need to use WebClient as PS has built in support for calling HTTP endpoints directly via Invoke-WebRequest.

$response = Invoke-WebRequest "<url>"
[System.IO.File]::WriteAllBytes("<savepath>", $response.Content)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

thanks for replying, my problem is i dont know how to append the time and date to the filename. when trying it stuffs up and changes the extension,.

that code was as far as i got that successfully downloaded the file but without any file modifications

0 Votes 0 ·

Yes appending to the filename is annoying. Given a file if you simply append to the end then it adds to the extension so you need to extract the filename (without extension), modify it and add the extension back. Something like this:

$filename = "C:\temp\abc.txt"

$newName = [System.IO.Path]::GetFilenameWithoutExtension($filename) + [DateTime]::Now.ToString("_yyyyMMddTHHmmss") + [System.IO.Path]::GetExtension($filename)


If you want a custom datetime format then you can do so as I did here. If you are OK with a standard format then refer to the supported formats here.

0 Votes 0 ·
RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered RichMatheisen-8856 edited

This will get the "Date taken" property from the files details and use that (or the current date/time if it's not a valid date/time) to rename the file. Note, however, that in your example you're using a colon (":") as part of the new file name. That immediately dooms you to failure! A colon is used to separate a device name (e.g. a drive letter) from the directory\file. The script just replaces that with a hyphen.

     $FilePath = 'C:\junk\hunterexpressway_m1.jpg'
        
     $WebClient = New-Object System.Net.WebClient
     $WebClient.DownloadFile("https://roads-waterways.transport.nsw.gov.au/trafficreports/cameras/camera_images/hunterexpressway_m1.jpg", $FilePath)
        
     # get the date/time the picture was taken
     $Folder = Split-Path -Parent -Path $FilePath
     $File = Split-Path -Leaf -Path $FilePath
     $Shell = New-Object -COMObject Shell.Application
     $ShellFolder = $Shell.NameSpace($Folder)
     $ShellFile = $ShellFolder.ParseName($File)
        
     $numberofdetails = 266  # adjust this as needed. There many be more than 266 details, or there may be fewer. 
                             # it depends on the file, the O/S, and the version of the O/S
        
     0..$numberofdetails |
         ForEach-Object{
             if ($ShellFolder.GetDetailsOf($null, $_) -eq 'Date taken'){
                 $d = $ShellFolder.GetDetailsOf($ShellFile, $_)
                 [datetime]$datetaken = $d -replace "[^0-9APM/ :]",""  # get rid of BOM characters
                                                                       # the date/time conversion should work
                                                                       # but if it fails it'll throw an error
             }
         }
     $ShellFile = $null
     $ShellFolder = $null
     $Shell = $null
     if ($datetaken -isnot [DateTime]){
         $datetaken = Get-Date   # just use something sensible
     }
        
     # NOTE you cannot use ":" as part of the file name!
     # so use a hyphen instead
     $datepart = $datetaken.ToString("MM-dd-yyyy hh-mm") # get just date and time (without the colon)
        
     [array]$f = $File -split "\."
     $newfile = "{0}-{1}.{2}" -f $f[0], $datepart, $f[1]
     Rename-Item -Path $FilePath -NewName "$newfile"

If you like, you can wrap the DownloadFile part of the script in a Try/Catch so it reacts in a sensible way to any failure.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

thanks for the details and script suggestions. also for some reason when i submitted it seemed to have omitted my c:\ entries in the filepath not sure why.

i will gladly test this when i am off work today

0 Votes 0 ·