Announcement

Collapse
No announcement yet.

Help me with this script? (Simple Copy File)

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

  • Help me with this script? (Simple Copy File)

    I have a script that works to copy a file, but only if I know its name ahead of time.

    Code:
    Sub Main(ByVal Parm As String)
    
            Dim IFilePath As String = ""
            Dim IDestPath As String = ""
            IFilePath = hs.GetAppPath & "\logs\HomeSeerLog_Export_20190101-154846_20190101-155846.csv"
            IDestPath = "C:\Program Files (x86)\HomeSeer HS3\Data\ToSend\log.csv"
            If System.IO.File.Exists(IFilePath) Then
                System.IO.File.Copy(IFilePath, IDestPath, True)
            Else
                hs.writelog("Error", "File does not exist!")
            End If
    
        End Sub
    The problem is that I will not always know the exact name of a file, only that it should exist. See above example.
    What I want to do is specify / pattern match one part of the file name that will always be the same.
    For example, the file name I'd like to specify is "HomeSeerLog_Export_20190101*.csv" and have it copy the file listed in the example.

    Easy way to do this?

  • #2
    You can use the file system's GetFiles method. Documentation is here:

    https://docs.microsoft.com/en-us/dot...ecific-pattern

    Comment


    • #3
      Thanks for the link. I wish I knew how to program but I'm a total hack. I manage to piece bits of others code together and make things work.

      Can anyone provide a semi-complete bit of code which would allow me to pattern-match a file to copy?

      Comment


      • #4
        Here is some info that might be helpful

        https://docs.microsoft.com/en-us/dot...ramework-4.7.2

        Comment


        • #5
          You can get a list of files matching a pattern.

          https://docs.microsoft.com/en-us/dot...ramework-4.7.2

          Comment


          • #6
            Try this:

            Code:
            Imports System.IO
            
            Sub Main(ByVal Parm As String)
            
                Dim Search As String = "HomeSeerLog_Export_20190101"
                Dim SourcePath As String = hs.GetAppPath & "\logs\"
                Dim DestPath = hs.GetAppPath & "\Data\ToSend\log.csv"
                Dim Dir As New DirectoryInfo(SourcePath)
                Dim Files As Array = (From x In Dir.GetFiles("*" & Search & "*.csv", SearchOption.TopDirectoryOnly) Select x.FullName).ToArray
                For each sFile As String in Files
                    System.IO.File.Copy(sFile, DestPath, True)
                Next
            
            End Sub
            Jon

            Comment


            • #7
              Originally posted by jon00 View Post
              Try this:

              Code:
              Imports System.IO
              
              Sub Main(ByVal Parm As String)
              
              Dim Search As String = "HomeSeerLog_Export_20190101"
              Dim SourcePath As String = hs.GetAppPath & "\logs\"
              Dim DestPath = hs.GetAppPath & "\Data\ToSend\log.csv"
              Dim Dir As New DirectoryInfo(SourcePath)
              Dim Files As Array = (From x In Dir.GetFiles("*" & Search & "*.csv", SearchOption.TopDirectoryOnly) Select x.FullName).ToArray
              For each sFile As String in Files
              System.IO.File.Copy(sFile, DestPath, True)
              Next
              
              End Sub
              Happy New Year Jon! As always, you are a genius!

              Please do check your $$ balance when you get a chance. Only wish I could donate more for all the work you save me!

              Comment

              Working...
              X