Powershell download jpg and timestamp test

Carl S 16 Reputation points

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:

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


Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,396 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Michael Taylor 49,071 Reputation points

    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. Rich Matheisen 45,111 Reputation points

    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 |  
                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.