K
K
Hi all, I have code below which i got from a friend on this group.
it works fine but only thing i am getting is that even i mentioned
that i want only those files to be copied which have extention ".xls"
in the below code line
" Dim files = From f In Directory.GetFiles(sourceDir, "*.xls",
subdirsOption) Where ((New FileInfo(f).Attributes) And
FileAttributes.Hidden) = 0 Select f "
but it still copies all the other extention excel files as well like
(".xlsm , xlsx etc..). Please can any friend can help that how can i
resolve it. I just want below code to copy only those files of which
extentions are given in the code.
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
Dim sourceDir = "C:\Documents\Target"
Dim destinationDir = "C:\Documents\Destination"
If Not (Directory.Exists(sourceDir)) Then
MsgBox("No source folder - " & sourceDir)
Exit Sub
End If
Dim subdirsOption As SearchOption = SearchOption.TopDirectoryOnly
If includeSubforms.Checked Then
subdirsOption = SearchOption.AllDirectories
End If
' get the filenames of the non-hidden xlsx files
Dim files = From f In Directory.GetFiles(sourceDir, "*.xls",
subdirsOption) Where ((New FileInfo(f).Attributes) And
FileAttributes.Hidden) = 0 Select f
If files Is Nothing OrElse files.Count = 0 Then
MsgBox("No files were found to copy.")
Exit Sub
End If
If Not Directory.Exists(destinationDir) Then
Directory.CreateDirectory(destinationDir)
End If
Dim n As Integer = 0
For Each f In files
File.Copy(f, Path.Combine(destinationDir, Path.GetFileName(f)),
overwrite:=True)
n += 1
ProgressBar1.Value = (n / files.Count) * 100
Application.DoEvents()
Next
' now notify user
End Sub
it works fine but only thing i am getting is that even i mentioned
that i want only those files to be copied which have extention ".xls"
in the below code line
" Dim files = From f In Directory.GetFiles(sourceDir, "*.xls",
subdirsOption) Where ((New FileInfo(f).Attributes) And
FileAttributes.Hidden) = 0 Select f "
but it still copies all the other extention excel files as well like
(".xlsm , xlsx etc..). Please can any friend can help that how can i
resolve it. I just want below code to copy only those files of which
extentions are given in the code.
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
Dim sourceDir = "C:\Documents\Target"
Dim destinationDir = "C:\Documents\Destination"
If Not (Directory.Exists(sourceDir)) Then
MsgBox("No source folder - " & sourceDir)
Exit Sub
End If
Dim subdirsOption As SearchOption = SearchOption.TopDirectoryOnly
If includeSubforms.Checked Then
subdirsOption = SearchOption.AllDirectories
End If
' get the filenames of the non-hidden xlsx files
Dim files = From f In Directory.GetFiles(sourceDir, "*.xls",
subdirsOption) Where ((New FileInfo(f).Attributes) And
FileAttributes.Hidden) = 0 Select f
If files Is Nothing OrElse files.Count = 0 Then
MsgBox("No files were found to copy.")
Exit Sub
End If
If Not Directory.Exists(destinationDir) Then
Directory.CreateDirectory(destinationDir)
End If
Dim n As Integer = 0
For Each f In files
File.Copy(f, Path.Combine(destinationDir, Path.GetFileName(f)),
overwrite:=True)
n += 1
ProgressBar1.Value = (n / files.Count) * 100
Application.DoEvents()
Next
' now notify user
End Sub