繁体   English   中英

VB.net 从目录中读取文件名以运行 SQL 查询

[英]VB.net Read File Name from Dir to Run SQL Query

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

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

IF fileextension = SUCCESS

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

任何帮助或提示将不胜感激。

更新:

嗨, 通过对各个站点的一些观察,iv 得出以下内容。 现在忘记 SQL,我只是在文件名和文件移动之后,但我收到 IO 异常,该文件已在使用中:

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

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

终端模块

我以前用过这个:

FileWather 类

对于轮询目录以了解结构和文件详细信息等变化非常有用。

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM