0

Please can someone assist me with a project we are working on. We use HMIs to log data to a USB drives connected directly to the HMIs. Our client requires access to these files via FTP. The HMI has an FTP area that can be accessed however there is no direct way of copying these folders between locations. I can run some VB script in the background and trigger the sub routine from the PLC.

Not knowing anything about VB scripts I'm struggling to get this to work.

This code only works if the date is included: IO.DirectoryInfo("\UsbDisk\Data Logging\Log Files\DataSet0\yyyymmdd").

A new folder is created every night at midnight with a new date, so having a fixed date will only work for that day. Ideally I want the copy to copy everything from "UsbDisk\Data Logging\Log Files", ignoring the "DataSet0\yyyymmdd".

Thanks in advance.

Sub Test
    Dim di As New IO.DirectoryInfo("\UsbDisk\Data Logging\Log Files\DataSet0\20191126")
    Dim Diar1 As IO.FileInfo() = di.GetFiles()
    Dim dra As IO.FileInfo

    For Each dra In Diar1
        System.IO.File.Copy(dra.FullName, "\User\SysmacHMI\FTP\" + dra.Name,True)

    Next
End Sub
Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
Matt Page
  • 11
  • 1
  • Just to check I understand, do you want to copy every file found under the "DataSet0" directory into "\User\SysmacHMI\FTP\"? Or is the "0" in "DataSet0" also changeable? – Andrew Morton Nov 27 '19 at 11:10
  • I think you want `Dim di As New IO.DirectoryInfo("\UsbDisk\Data Logging\Log Files")` and `Dim Diar1 As IO.FileInfo() = di.GetFiles("*.*", SearchOption.AllDirectories)`. – Andrew Morton Nov 27 '19 at 11:14
  • Thanks for your input. Im not sure if this is due to the script running on a HMI but the SearchOption.AllDirectories doesn't compile. Dim Diar1 As IO.FileInfo() = di.GetFiles("*.*" SearchOption.AllDirectories) – Matt Page Nov 28 '19 at 16:26
  • You could use the code in the question [Recursive File Search in VB.NET](https://stackoverflow.com/q/44103640/1115360) instead. Incidentally, what does HMI stand for, and which version of the .NET Framework is it using? – Andrew Morton Nov 28 '19 at 16:32
  • HMI (Human Machine Interface) I'm a controls engineer and only understand logic programming (Ladder logic / Structured text) VB is not something I ever have to use. Until now :/ I appreciate your link but doesn't make any sense to me. Also not sure how I could find out what version of .NET Framework is used. – Matt Page Nov 29 '19 at 08:25
  • Assuming you are using Visual Studio, to find out which version of the .NET Framework is being used open the "Project" menu, click on "your-project-name Properties..", select the "Application" tab on the left, and look in the "Target framework" dropdown box. – Andrew Morton Nov 29 '19 at 10:52

1 Answers1

0

You write "VB scripts" but show code that would be in a VB.NET program, so I am going with the latter. (What is the difference between vbscript and vb.net?)

It is possible to iterate over all the subdirectories and get the files from them, a slight adaptation of the code shown in Recursive File Search in VB.NET (which isn't actually using a recursive method) will suffice.

As an example, this is a complete VB.NET console application using that:

Option Infer On
Option Strict On

Imports System.IO

Module Module1

    ''' <summary>
    ''' Get the full filenames of the files in the given directory and its subdirectories.
    ''' Errors are ignored.
    ''' </summary>
    ''' <param name="path">Initial directory.</param>
    ''' <returns>A List(Of String) of the full filenames.</returns>
    Public Function GetAllFiles(ByVal path As String) As List(Of String)
        ' Adapted from https://stackoverflow.com/questions/44103640/recursive-file-search-in-vb-net

        Dim files As New List(Of String)
        Dim dirs As New Stack(Of String)
        dirs.Push(path)

        Do While (dirs.Count > 0)
            Dim currentDir = dirs.Pop()
            Try
                files.AddRange(Directory.EnumerateFiles(currentDir))

                For Each d In Directory.EnumerateDirectories(currentDir)
                    dirs.Push(d)
                Next

            Catch ex As Exception
                ' Could have been a permissions problem. Ignore it.
            End Try

        Loop

        Return files

    End Function

    Sub Test()
        'TODO: Make sure these are the correct absolute paths:
        Dim rootDir = "\UsbDisk\Data Logging\Log Files"
        Dim destDir = "\User\SysmacHMI\FTP"

        Dim allTheFiles = GetAllFiles(rootDir)

        For Each f In allTheFiles
            Dim newName = Path.Combine(destDir, Path.GetFileName(f))
            Console.WriteLine("Copy " & f & " to " & newName)
            ' Uncomment the File.Copy line to make it actually do the copy.
            ' File.Copy(f, newName, True)
        Next

    End Sub

    Sub Main()
        Test()

        Console.ReadLine()

    End Sub

End Module

If it complains about EnumerateFiles and EnumerateDirectories then use GetFiles and GetDirectories.

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84