PC Review


Reply
Thread Tools Rating: Thread Rating: 1 votes, 1.00 average.

Correction needed in Copy File code

 
 
K
Guest
Posts: n/a
 
      17th Jun 2010
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
 
Reply With Quote
 
 
 
 
Onur Güzel
Guest
Posts: n/a
 
      17th Jun 2010
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
 
Reply With Quote
 
Andrew Morton
Guest
Posts: n/a
 
      17th Jun 2010
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


 
Reply With Quote
 
Andrew Morton
Guest
Posts: n/a
 
      17th Jun 2010
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


 
Reply With Quote
 
K
Guest
Posts: n/a
 
      17th Jun 2010
Thanks lot andrew it works like a charm
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Code needed to copy rows. QuickLearner Microsoft Excel Programming 0 21st Jul 2006 12:24 PM
Copy Code needed MWH Microsoft Excel Programming 7 24th Jun 2005 08:41 AM
Copy Code Needed MWH Microsoft Excel Worksheet Functions 1 22nd Jun 2005 06:49 AM
Quick code correction needed =?Utf-8?B?U2FtIEt1bw==?= Microsoft Access Forms 2 9th Dec 2004 11:32 PM
Spell Check Correction Case Correction Code Question Dave Elliott Microsoft Access Forms 1 17th Sep 2004 04:40 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:32 PM.