copying multiple files

K

Kerr

Hi all,
I am sure that this is quite simple but can't find any good examples
anywhere.

I need a method of copying multiple files from a source directory to a
destination directory. The file format of the files will be mixed but I
will be only interested in copying files with a particular extention
(i.e. *.csv).

I'm using VB.NET and thought creating a filecopy loop would be all i
needed to do but i don't think I understand the architecture of it
properly.

Cheers in advance for your help!!

Kerr
 
H

Herfried K. Wagner [MVP]

Kerr said:
I need a method of copying multiple files from a source directory to a
destination directory. The file format of the files will be mixed but I
will be only interested in copying files with a particular extention
(i.e. *.csv).

Written from scratch (untested):

\\\
Imports System.IO
..
..
..
Const DestinationDirectory As String = "C:\Goo"
For Each FileName As String In Directory.GetFiles("C:\Temp\*.foo")
File.Copy( _
FileName, _
Path.Combine(DestinationDirectory, Path.GetFileName(FileName)) _
)
Next FileName
///
 
K

Kerr

Thanks Cor for your post.

I really think I must be dumb or something because the link you sent me
loops through directories and I want to do it the other way and loop
through files in a specified directory. I'll keep trying.

Kerr

Hendrick, sorry, I'm not good with C# and don't have a translater from
C# to vb.net.

I'll keep trying.

Cheers for your help. that article has sort of put me on the right track
 
H

Herfried K. Wagner [MVP]

Kerr said:
Hendrick, sorry, I'm not good with C# and don't have a translater from
C# to vb.net.

Who is Hendrick? The code /I/ (Herfried) posted actually /is/ VB.NET code!
 
G

Guest

Here's another way of doing it, which will handle your future usage of it
without the need to change:

Imports System.IO

Private Sub CopyFiles(ByVal sSource As String, ByVal sDestination As
String, Optional ByVal sExtension As String = "*.csv", Optional ByVal
blnOverWrite As Boolean = False)
If Directory.Exists(sSource) = False Then
MessageBox.Show("Source directory doesn't exist", "Source
Directory Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End If
If Directory.Exists(sDestination) = False Then
MessageBox.Show("Destination directory doesn't exist",
"Destination Directory Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End If
Dim fil As FileInfo
Dim diSourceDir As New DirectoryInfo(sSource)
Try
For Each fil In diSourceDir.GetFiles(sExtension)
fil.CopyTo(Path.Combine(sDestination, fil.Name), blnOverWrite)
Next
Catch ex As Exception
' Probably read only
End Try
End Sub

Usage:

CopyFiles("C:\", "C:\Temp")
CopyFiles("C:\", "C:\Temp",, True)
CopyFiles("C:\", "C:\Temp", "*.zyx")
CopyFiles("C:\", "C:\Temp", "*.zyx", True)

Remember that I have made 'CSV' the default extension & it doesn't have to
be specified unless you chage the searched file extension

Also, Overwrite has been set to false by default.

I hope this helps
 
C

Cor Ligthert

Herfired,
Who is Hendrick? The code /I/ (Herfried) posted actually /is/ VB.NET
code!

--
I have the idea that you start making some samples too complex, they are
nice, however only 40% will probably understand it.

Just to help

Cor
 
K

Kerr

Apologies Herfried,
I saw your code with the (( and // and automatically thought it was c#
code. Also, I typed your name whilst someone was talking to me, so not
paying attention to what I wrote.

Thanks for your assistance!!
 
G

Guest

I agree, Mr MVP's untested code is actually VB.NET code. Its very easy to
understand, even for the true novice.
 
K

Kerr

Hi fellas,
me again.
thanks for all the help so far. one other question. using the filecopy
method, i keep getting the error saying "the target directory already
exists".

Now I don't know why it's telling me this because I created the
directory.

Do you guys know why an exception is thrown each time i try to copy a
file.

Kerr
 
H

Herfried K. Wagner [MVP]

Kerr said:
one other question. using the filecopy
method, i keep getting the error saying "the target directory already
exists".

Can you post the code you are currently using and the complete exception
text?
 
K

Kerr

Here's the code I am currently using.

'grab path from class
Dim strImportPath As String = objclsData.strImportPath
Dim strCSVWildcard As String =
ConfigurationSettings.AppSettings("CSVWildcard")
Dim strAppWorkingDir As String =
ConfigurationSettings.AppSettings("AppWorkingFolder")
Dim objfile As FileInfo
Dim objDirSource As New DirectoryInfo(strImportPath)

Try
If Not Len(strImportPath) > 0 Then
objclsUtils.WriteStatusbarMessage("You have not selected
the path of the CSV files!")
Exit Try
End If

'if it doesn't exist create working folder
If Not Directory.Exists(strAppWorkingDir) Then
Directory.CreateDirectory(strAppWorkingDir)
End If

'copy files to working directory
For Each objfile In objDirSource.GetFiles(strCSVWildcard)
objfile.CopyTo(Path.Combine(strAppWorkingDir,
objfile.Name), True)
Next

'copy local version of batch file to working directory
Dim objBATFile As New
FileInfo(ConfigurationSettings.AppSettings("BatchFile"))

FileCopy(objBATFile.ToString, strAppWorkingDir)
objBATFile = Nothing
Catch ex As Exception
strEvt = ex.Source.ToString & ": " & vbCrLf & "Message: " &
ex.Message & vbCrLf & vbCrLf & ex.InnerException.ToString
objclsUtils.writeEventLogError(strEvt)

The filecopy command keeps returning "target directory exist".
 
C

Cor Ligthert

Kerr,

Why dont you use
\\\
FileCopy(strImportPath, strAppWorkingDir)
///
I did not analyze the rest of your code, however in that part I dont see why
you make it yourself that difficult.

I hope this helps?

Cor
 
H

Herfried K. Wagner [MVP]

Kerr said:
FileCopy(objBATFile.ToString, strAppWorkingDir)

Make sure that both values passed to 'FileCopy' are valid file paths, which
means, that they include a filename.
 
K

Kerr

Cor,
I cannot use the strImportPath in the filecopy command as it has nothing
to do with the file I am trying to copy. I don't see where I am making
things difficult for myself. The comments should make what I am trying
to do make sense.

Kerr
 
C

Cor Ligthert

Kerr,

The sentence had to be as Herfried did including the file names, I forgot to
write that.
However the rest of the message stays almost the same.

"Keep it simple" is mostly the most efficient way to get things right and
maybe should I have written "make it so difficult for us to translate your
problem".

I know there is no need that I answer you, however I am in that not so
different from others.

Cor
 

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