Vb.Net - 根据模式复制文件

时间:2012-12-10 16:03:17

标签: vb.net

我有一张名为FILELISTS的表

Table Name - Filelists
Field - FileNames

数据值

  1. FILE1.TXT
  2. File2.csv
  3. 文件3 *的.csv
  4. 我正在努力编写代码,如果它有文件名(如file1.txt和file2.txt),它需要从源代码复制到目标。如果文件名是模式(如File3 * .csv),则将所有与此模式匹配的文件从源复制到目标。

    我使用数据阅读器列举了Vb.net中的上一行。

2 个答案:

答案 0 :(得分:2)

您可以使用Directory.EnumerateFilesFile.Copy,例如:

var filePatterns = database.GetFileNamePattern(); // your method that returns the list of files
// assuming you've stored the source- and dest.-directories in the app-settings
string sourceFolder = Properties.Settings.Default.SourceFolder;      
string destFolder = Properties.Settings.Default.DestinationFolder;
foreach (string pattern in filePatterns)
{
    var files = Directory.EnumerateFiles(
        sourceFolder,
        pattern,
        SearchOption.TopDirectoryOnly);
    foreach (string file in files)
    {
        File.Copy(file, Path.Combine(destFolder, Path.GetFileName(file)), true);
    }
}

编辑:对不起,这里是VB.NET版本:

' your method that returns the list of files:
Dim filePatterns = database.GetFileNamePattern()  
' assuming you've stored the source- and dest.-directories in the app-settings
Dim sourceFolder As String = My.Settings.SourceFolder
Dim destFolder As String = My.Settings.DestinationFolder
For Each pattern As String In filePatterns
    Dim files = Directory.EnumerateFiles(sourceFolder, pattern, SearchOption.TopDirectoryOnly)
    For Each file As String In files
       IO.File.Copy(file, IO.Path.Combine(destFolder, IO.Path.GetFileName(file)), True)
    Next
Next

答案 1 :(得分:0)

DirectoryInfo,FileInfo - - 您在我点击发布之前删除了您的其他问题...但这适用于.net framework 2.0,就像您要求的那样

Option Strict On
Imports sO = System.IO.SearchOption
Imports dI = System.IO.DirectoryInfo
Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        For Each F In (New dI("C:\")).GetFiles("*.*", sO.TopDirectoryOnly)
            MsgBox(F.FullName)
            'Do your copy here
        Next
    End Sub
End Class