473,411 Members | 2,085 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,411 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 16071
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.