是否有更简单的方法来协调文件列表和具有子文件夹/文件的目录以查找更改?

发布于 2024-09-03 14:49:31 字数 457 浏览 1 评论 0原文

我有一个包含文件列表(路径+文件名)的 SQL Server 表,以及一个包含多个层和每个层中的文件的文件夹。我正在寻找一种方法来协调两者,而不必两次处理列表。目前,我正在这样做:

For Each f as FileInfo In FileListFromDatabase
   If f.Exists is False, mark it as deleted in the database
Next

For Each f as FileInfo In RecursiveListOFFilesOnDisk
   If Not FileExistsInDatabase, then add it
Next

有更好的方法吗?我想避免将所有匹配文件(其中大多数)转换为 FileInfo 对象两次。由于我首先是一名 T-SQL 开发人员,因此我想象了类似两个不匹配列表的 OUTER JOIN 的情况。类似于 LINQ 的东西吗?

I have a SQL Server table with a list of files (path + filename), and a folder with multiple layers and files in each layer. I'm looking for a way to reconcile the two without having to process the list twice. Currently, I'm doing this:

For Each f as FileInfo In FileListFromDatabase
   If f.Exists is False, mark it as deleted in the database
Next

For Each f as FileInfo In RecursiveListOFFilesOnDisk
   If Not FileExistsInDatabase, then add it
Next

Is there a better way to do this? I'd like to avoid converting all the matching files (of which most will be) to FileInfo objects twice. Since I'm a T-SQL developer first, I'm picturing something like an OUTER JOIN of the two lists where they don't match. Something LINQ-ish?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

场罚期间 2024-09-10 14:49:31

我想不出任何像您想要的那样简洁的解决方案,但如果您担心 IO 性能,则不必查找每个现有文件两次,只需读取所有文件位置并在内存中完成所有操作即可。

Dim dbFiles As List(Of String)
Dim files As New List(Of String)
files.AddRange(IO.Directory.GetFiles("path", "pattern", IO.SearchOption.AllDirectories))
Dim deletedFiles As New List(Of String)
For Each dbfile As String In dbFiles
    If files.Contains(dbfile) Then
        files.Remove(dbfile)
    Else
        deletedFiles.Add(dbfile)
    End If
Next
' now deleteFiles should have all files that have been deleted from the file 
' system and files should have all new files in the file system

如果存储在数据库中的路径名可能与文件系统中的路径名不同,您可能需要对此进行调整。

I can't think of any solution as neat as you want, but if you're worrying about IO performance, instead of finding each existing file twice, just read in all the file locations and do it all in memory.

Dim dbFiles As List(Of String)
Dim files As New List(Of String)
files.AddRange(IO.Directory.GetFiles("path", "pattern", IO.SearchOption.AllDirectories))
Dim deletedFiles As New List(Of String)
For Each dbfile As String In dbFiles
    If files.Contains(dbfile) Then
        files.Remove(dbfile)
    Else
        deletedFiles.Add(dbfile)
    End If
Next
' now deleteFiles should have all files that have been deleted from the file 
' system and files should have all new files in the file system

If the path names stored in the DB could have a different case than the ones in the filesystem you might have to adjust for that though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文