Reputation: 213
im doing an application that checks or compare if the files are existing in the backup folder from another folder. I can do it with declaring a specific file or an array. like this one.
" = file1.txt"
" = file2.txt"
"etc..."
but how about not having specific files or array?
this is my code:
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
Upvotes: 0
Views: 1027
Reputation: 1985
Try this:
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
Edited so you can get the most of what you need.
Upvotes: 0
Reputation: 2198
try this u r code is working fine jst change the logic of it i alredy given in below code
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
Upvotes: 1