On Jun 17, 12:30 pm, K <kamranr1...@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/4...bcde-and-so-on
(You can convert to VB.NET easily, though)
HTH,
Onur Guzel