[请有人可以协助我进行我们正在进行的项目。我们使用HMI将数据记录到直接连接到HMI的USB驱动器上。我们的客户要求通过FTP访问这些文件。 HMI有一个可以访问的FTP区域,但是没有直接方法在位置之间复制这些文件夹。我可以在后台运行一些VB脚本并从PLC触发子例程。
对VB脚本一无所知,我正在努力使其正常工作。
此代码仅在包含日期的情况下有效:IO.DirectoryInfo("\UsbDisk\Data Logging\Log Files\DataSet0\yyyymmdd")
。
将在每天晚上的午夜创建一个具有新日期的新文件夹,因此具有固定日期仅在该天有效。理想情况下,我希望副本从“ UsbDisk \ Data Logging \ Log文件”复制所有内容,而忽略“ DataSet0 \ yyyymmdd”。
谢谢。
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
您编写了“ VB脚本”,但显示了将在VB.NET程序中使用的代码,因此我将继续使用后者。 (What is the difference between vbscript and vb.net?)
可以遍历所有子目录并从它们中获取文件,对Recursive File Search in VB.NET中显示的代码进行一点改编(实际上并没有使用递归方法就足够了:
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
如果投诉EnumerateFiles
和EnumerateDirectories
,请使用GetFiles
和GetDirectories
。