From: K on
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
From: Onur Güzel on
On Jun 17, 12:30 pm, K <kamranr1...(a)yahoo.co.uk> wrote:
> 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/437914/filtering-file-names-getting-abc-without-abcd-or-abcde-and-so-on

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

HTH,

Onur Guzel
From: Andrew Morton on
K wrote:
> 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

--
Andrew


From: Andrew Morton on
Onur G�zel wrote:
> 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
>
Interesting... I have disabled 8dot3 name creation on my computer, and in my
testing it doesn't pick up the "extra" extensions.

--
Andrew


From: K on
Thanks lot andrew it works like a charm