Correction needed in Copy File code

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
 
O

Onur Güzel

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

Check out your search pattern, it is ".xls" which will also retrieve
".xlsx" , ".xlsm" and so on.

An explanation is located on MSDN. Please read "remarks" section
carefully. That's because of 8.3 file name format.

http://msdn.microsoft.com/en-us/library/ms143316.aspx

Possible solutions here:

http://stackoverflow.com/questions/...s-getting-abc-without-abcd-or-abcde-and-so-on

(You can convert to VB.NET easily, though)

HTH,

Onur Guzel
 
A

Andrew Morton

K said:
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.

Hmm, my testing does not agree with GetFiles being so lax with the extension
matching... however, you can just add another clause to the where condition
to make it an exact match:

Dim files = From f In Directory.GetFiles(sourceDir, "*.xls", subdirsOption)
Where (Path.GetExtension(f) = ".xls" AndAlso ((New FileInfo(f).Attributes)
And FileAttributes.Hidden) = 0) Select f
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top