我正在做一个应用程序来检查或比较文件是否存在于另一个文件夹的备份文件夹中。我可以通过声明特定文件或数组来实现。喜欢这个。
" = file1.txt"
" = file2.txt"
"etc..."
但是没有特定的文件或数组呢?
这是我的代码:
Imports System.IO
Public Class Form3
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
check()
End Sub
Sub check()
Dim src As String = "D:\test"
Dim dest As String = "D:\test2"
Dim srcdir As New DirectoryInfo(src)
Dim destdir As New DirectoryInfo(dest)
Dim srcfile As FileInfo() = srcdir.GetFiles
Dim destfile As FileInfo() = destdir.GetFiles
Dim fi As FileInfo
For Each fi In srcfile
If fi.Name = fi.Name(destfile) Then
MsgBox("no new files")
Else
MsgBox("new files detected")
End If
Next
For Each fi In srcfile
File.Copy(fi.FullName, dest & fi.Name)
Next
End Sub
答案 0 :(得分:0)
试试这个:
Dim filesToCopy As New ArrayList()
For Each Dir As String In System.IO.Directory.GetFiles(src) ' This will check every file from src or (D:\Test)
Dim dirInfo As New System.IO.DirectoryInfo(Dir)
If Not System.IO.File.Exists(dest & "\" & dirInfo.Name) Then 'This will check if the file from src exists in dest (D:\Test2)
filesToCopy.Add(dirInfo.Name)
End If
Next
If filesToCopy.Count > 0 Then
If MsgBox("There are new files found. Do you want to sync it now?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Confirm") = MsgBoxResult.Yes Then
For i = 0 To filesToCopy.Count - 1
System.IO.File.Copy(src & "\" & filesToCopy(i), dest & "\" & filesToCopy(i))
Next
End If
End If
编辑,以便您可以充分利用所需的一切。
答案 1 :(得分:0)
试试这个 你的代码工作正常jst改变它的逻辑我alredy在下面的代码中给出
Imports System.IO
Public Class Form3
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
check()
End Sub
Sub check()
Dim src As String = "D:\test"
Dim dest As String = "D:\test2"
Dim srcdir As New DirectoryInfo(src)
Dim destdir As New DirectoryInfo(dest)
Dim srcfile As FileInfo() = srcdir.GetFiles
Dim destfile As FileInfo() = destdir.GetFiles
Dim fi As FileInfo
For Each fi In srcfile
If If Not System.IO.File.Exists(fi.Name) Then
MsgBox("no new files")
Exit sub
Else
For Each fi In srcfile
File.Copy(fi.FullName, dest & fi.Name)
Next
End If
Next
End Sub