473,327 Members | 2,071 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

copying multiple files

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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 21 '05 #1
17 16054
Kerr,

By using the directory info and in that the method getfiles you can do what
you want.

http://msdn.microsoft.com/library/de...classtopic.asp

In this is almost the sample you need.
http://msdn.microsoft.com/library/de...filestopic.asp

I hope this helps?

Cor
Nov 21 '05 #2
"Kerr" <ke*****@lycos.co.uk> schrieb:
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
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #3
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 21 '05 #4
"Kerr" <ke*****@lycos.co.uk> schrieb:
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!

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #5
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

Nov 21 '05 #6
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
Nov 21 '05 #7
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!!

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 21 '05 #8
Kerr,

"Kerr" <ke*****@lycos.co.uk> schrieb:
Apologies Herfried,
No need to apologize :-).
I saw your code with the (( and // and automatically thought it was c#
code.


Oh, sorry... I always write listings between "\\\...///"...

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #9
I agree, Mr MVP's untested code is actually VB.NET code. Its very easy to
understand, even for the true novice.

"Herfried K. Wagner [MVP]" wrote:
"Kerr" <ke*****@lycos.co.uk> schrieb:
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!

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #10
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 21 '05 #11
"Kerr" <ke*****@lycos.co.uk> schrieb:
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?

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #12
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("AppWorkingFolde r")
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("BatchF ile"))

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".

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 21 '05 #13
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
Nov 21 '05 #14
"Kerr" <ke*****@lycos.co.uk> schrieb:
FileCopy(objBATFile.ToString, strAppWorkingDir)


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

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #15
doh.

:-)

Cor
Nov 21 '05 #16
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 21 '05 #17
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
Nov 21 '05 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Robert Tarantino | last post by:
Hello, I am trying to find a way to create a scheduled task or service that will copy my local profile folders under "Documents and settings" to a network drive. This would allow me to restore...
1
by: yaduraj | last post by:
hello all, How can i copy files with a particular extension. For eg: I am using copy ("$HOME_DIR/file1.txt" ,"$LOCAL_DIR/") or warn "Cannot copy file: $!";
4
by: Alex Vinokur | last post by:
Copying files : input to output =============================== C/C++ Performance Tests ======================= Using C/C++ Program Perfometer http://sourceforge.net/projects/cpp-perfometer...
0
by: Dan | last post by:
We debug a VB.Net App that uses compact framework to a DAP Windows CE.Net device. When I debug I would like to only have the EXE copied down. Right now it copies any referrences as well and...
8
by: John Smith | last post by:
Hi folks, I know how to place text into the user's clipboard: Clipboard.SetDataObject("My Copied Text"); but how do I place a file in there? So, if I have a file C:\test.txt, how can I place...
10
by: Martin Ho | last post by:
I am running into one really big problem. I wrote a script in vb.net to make a copy of folders and subfolder to another destination: - in 'from.txt' I specify which folders to copy - in...
2
by: Edhy Rijo [Progytech] | last post by:
Hi All, I am learning VB.NET and building small application that will do the following: 1.. Copy all files from a DVD to a specific folder in the hard drive and overwrite all files always. The...
6
by: kimiraikkonen | last post by:
Hi, I use system.io.file class to copy files but i have a difficulty about implementing a basic / XP-like progress bar indicator during copying process. My code is this with no progress bar,...
1
by: skygremlin | last post by:
I am trying to write a simple VB app to copy all directories and files off a thumb drive to a directory on a pc. I have a couple things I am trying to automate. Final Build will: - Have...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.