VB.net从Dir读取文件名以运行SQL查询

时间:2012-11-21 11:30:04

标签: sql vb.net

我被要求创建一个轮询活动目录的控制台应用程序。 (C. \ TEMP \输入)

当文件带有(filename).SUCCESS时,将检索文件名以运行SQL查询。所以

IF fileextension = SUCCESS

使用filename运行SQL查询以更改SQL表中的值。 将原始文件移至c:\temp\Input\Processed

非常感谢任何帮助或提示。

更新:

嗨,通过对各个网站的一些看法,我想出了以下内容。暂时忘记SQL,我只在文件名和文件的移动之后,但我得到一个IO异常,该文件已被使用:

导入System.IO 导入System.String 模块模块1

Dim fileName As String = "C:\temp\Input\NR12345.success"
Dim pathname As String = "C:\temp\Input\"
Dim result As String
Dim sourceDir As String = "C:\temp\Input\"
Dim processedDir As String = "C:\temp\Input\Processed\"
Dim fList As String() = Directory.GetFiles(sourceDir, "*.success")



Sub Main()
    result = Path.GetFileName(fileName)
    Console.WriteLine("GetFileName('{0}') returns '{1}'", fileName, result)
    result = Path.GetFileName(pathname)
    Console.WriteLine("GetFileName('{0}') returns '{1}'", pathname, result)

    Call MySub()

End Sub

Sub MySub()
    'Move Files

    For Each f As String In fList
        'Remove path from the file name. 
        Dim fName As String = f.Substring(sourceDir.Length = 0)
        Dim sourceFile = Path.Combine(sourceDir, fName)
        Dim processedFileDir = Path.Combine(processedDir, fName)

        ' Use the Path.Combine method to safely append the file name to the path. 
        ' Will overwrite if the destination file already exists.
        File.Copy(Path.Combine(sourceDir, fName), Path.Combine(processedDir, fName), True)
        'File.Copy(sourceFile, processedFileDir)

    Next f
End Sub

结束模块

1 个答案:

答案 0 :(得分:2)

我以前用过这个:

The FileWather Class

在轮询目录以获取结构和文件详细信息等方面非常有用。

然后,您可以使用this获取文件的扩展名,如果符合您的条件,则执行一些操作。

这些链接附带示例,所以尽情享受!!

Sub MySub()
    'Move Files

    For Each f As String In fList

        Dim fInfo As FileInfo = New FileInfo(f)

        Dim fName As String = fInfo.Name

        Dim processedFileDir = Path.Combine(processedDir, fName)

        ' Use the Path.Combine method to safely append the file name to the path. 
        ' Will overwrite if the destination file already exists.
        File.Copy(fInfo.FullName, processedFileDir, True)

    Next f
End Sub