Announcement

Collapse
No announcement yet.

Script to rename file to Current

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Script to rename file to Current

    Recently took the plunge and converted to HS4, all well and good.
    I have noticed there now is a file in html "captures" which are all the files of what the cameras have recorded.
    I have tried looking into scripts which will rename the files so I can get the latest "current" image so I can email it to myself if I'm away from the house, however I have googled various sites and I still keep coming up with errors

    Does anyone have a script which I can run after the camera has recorded motion, which I can run to give me the latest/current image, which I can then send on as an attachment in my email?

    Many thanks..

    #2
    Give this a try. Disclaimer: I code like 1000 monkeys who have access to google. This is not deployed on my system and I've never used it other than to test it.

    Just supply the required parameters. Note that this script by default will delete a file if it has the same name as the newfilename. You can change how this is handled at the bottom of the script by commenting/uncommenting the appropriate option.

    Code:
    Public Sub Main(ByVal Parms As Object)
    '' This script will rename the last modified file with the target extension to the designated filename
    '' Parameters '<path to folder>|<extension>|<newname>
    '' example: C:\Temp|txt|newestxt.txt
    '' I've provided 2 options to deal with the condition that newfilename already exists
    '' The default is to delete the old file. The other option is to rename it to oldfile with a suffix YearMonthDayHoursSeconds
    '' You can comment/uncomment the appropriate option for your use case
    '' the renamed file(s) will be found in the same folder
    
    Dim ParmArray(10) As String
    '' Extract script parameters into ParmArray()
    ParmArray(0) = ""
    If Parms.ToString.Contains("|") Then
    ParmArray = Parms.ToString.Split(Convert.ToChar("|"))
    Else
    ParmArray(0) = Parms.ToString
    End If
    
    '' define our parameters for use by the script
    Dim folder As String = ParmArray(0)
    Dim extension As String = "*." + ParmArray(1)
    Dim newname As String = ParmArray(2)
    
    '' find the last modified file in the supplied folder with the supplied extension
    Dim LastModifiedFile As System.IO.FileInfo = (From fi In (New System.IO.DirectoryInfo(folder)).GetFiles(extension) Select fi Order By fi.LastWriteTime Descending Take 1)(0)
    
    '' if newname already exists we'll need to deal with it
    '' comment or uncomment to change how the old file is handled
    '' only one option can be uncommented at a time
    If IO.File.Exists(folder + "\" + newname) Then
    '' this option deletes the old file
    My.Computer.FileSystem.DeleteFile(folder + "\" + newname)
    
    '' this option renames the old file to a new filename
    'My.Computer.FileSystem.RenameFile(folder + "\" + newname, "oldfile" + Format(Date.Now, "yyyyMMddHHmmss") + "." + ParmArray(1))
    End If
    
    '' finally rename LastModifiedFile to NewName the file
    My.Computer.FileSystem.RenameFile(LastModifiedFile.FullName, newname)
    
    End Sub
    HS4 Pro on Shuttle NC10U, Win10; Z-NET
    Number of Devices: 1005
    Number of Events: 293

    Plug-Ins: BLLock, DirecTv, EasyTrigger, Honeywell WiFi Thermostat, Marquis monoprice Amp, MeiHarmonyHub, PHLocation2, Pushover 3P, UltraM1G3, rnbWeather, Worx Landroid, Z-Wave

    External applications: Homebridge-homeseer, Geofency, EgiGeoZone.

    Comment


      #3
      Hi jmaddox, and thank you for sending me the script. I only wish I had the brains to understand what goes where!!

      The file I wish to run the script on to give me the latest mp4 is in C:\Program Files (x86)\HomeSeer HS4\html\captures, when the camera records something it will leave a JPG file, a JSON file and an MP4 file, all with the same file name. As you can probably gather I am after running the script on this file and hopefully ending up with something which leaves me a file named "latest" (or something on those lines) in either another file destination or in the C:\Program Files (x86)\HomeSeer HS4\html\captures file. Something I can then attach to the email I want to send.

      Attachment C:\Program Files (x86)\HomeSeer HS4\html\captures\xxxxxxxxxx\latest.mp4

      I'm sure your script does that in some form, but to be honest I am way out of my depth with scripting.

      Many thanks once again

      Comment


        #4
        See if this works:

        Code:
        Imports System.IO
        
        
        Sub Main(ByVal Parm As Object)
        Dim FN As String = ""
        Dim FD As String = ""
        Dim NFN As String = ""
        Dim NFD As Date
        Dim Directory As String = hs.GetAppPath & "\html\captures"
        
        FN = Dir(Directory & "\*.jpg")
        Do While FN <> ""
            FD = FileDateTime(Directory & "\" & FN)
            If FD > NFD Then
                NFN = FN
                NFD = FD
            End If
            FN = Dir
        Loop
        My.Computer.FileSystem.CopyFile(Directory & "\" & NFN, Directory & "\latest.jpg", overwrite:=True)
        
        End Sub
        If using Linux, you will need to changes the \ to /
        Jon

        Comment


          #5
          What can I say....................jmaddox, thank you so much for your help, and once again Jon, amazing!!

          Comment


            #6
            It worked then

            I'm not sure if you wanted the jpg or the mp4 but I'm sure you worked out changing the *.jpg to *.mp4 will do that for you.
            Jon

            Comment


              #7
              Hi All,

              Back in 2020 Jon helped me out with this perfect script which would take a filename from a folder and change it to "Latest" in another file destination

              I have just downloaded dcorsus "Ring Doorbell" Plugin and my plan was to try to email the latest mp4 file across by email.
              When I run the script, the destination folder gives me a file named "Latest", however the mp4 created is of the oldest mp4 file.

              Whilst I know nothing of scripting, I think the information layout of the mp4 file could be the issue.
              The format from the Ring Plugin gives my oldest mp4 file name

              02-16-2023_13-12-07_human_***********.mp4,

              and the newest

              02-24-2023_15-53-06_human_***********.mp4.

              The script seems to point towards the 02-16-2023 file as being newer than the 02-24-2023 file.

              Is this a relatively easy modification to a existing script and could anyone point me in the right direction?

              Many thanks

              Comment


                #8
                Ignore this last post, I think it was because it was the first time I run the script, once a new file came in it updated it correctly with the 'Latest' file name

                Comment

                Working...
                X