copy files

M

Mike

I want to be able to copy a directory of files (all .HTM files) from a
network drive to a local drive on the machine c:\HTMFiles , How can i do
that?

I tried File.Copy(source, dest) but i need to put the file name in the dest
section. I just want to copy the entire directory since I don't know all the
file names just the extensions. Would
File.Copy("\servername\folder", "c:\\HTMFiles\*.HTM") work or no?

thx
 
J

Jon Skeet [C# MVP]

Mike said:
I want to be able to copy a directory of files (all .HTM files) from a
network drive to a local drive on the machine c:\HTMFiles , How can i do
that?

I tried File.Copy(source, dest) but i need to put the file name in the dest
section. I just want to copy the entire directory since I don't know all the
file names just the extensions. Would
File.Copy("\servername\folder", "c:\\HTMFiles\*.HTM") work or no?

No, I wouldn't expect it to. File.Copy expects the name of *a* file.
You need something like Directory.GetFiles to find out which files to
copy, and then copy each of them in turn.
 
M

Mike

So i can't just copy all the files from one spot to another?
I need each file name? If so that won't work for me since the file names
will be different everytime
 
J

Jon Skeet [C# MVP]

Mike said:
So i can't just copy all the files from one spot to another?

Not in one call to File.Copy, no.
I need each file name? If so that won't work for me since the file names
will be different everytime

That's why you use Directory.GetFiles to find out what the files are
first...
 
M

Mike

So since I just need only the files with .HTM extensions, i just can't use a
wildcard?
File.Copy("location\*.htm)
 
J

Jon Skeet [C# MVP]

Mike said:
So since I just need only the files with .HTM extensions, i just can't use a
wildcard?

No, you can't. File.Copy copies *a file*, not a whole load of files.
 
M

Mike

OK thanks for your information and help, so it appears that i can't automate
this process of my app then. thanks again
 
J

Jon Skeet [C# MVP]

Mike said:
OK thanks for your information and help, so it appears that i can't automate
this process of my app then. thanks again

Yes, you can. Very easily. Just not in one line of code.

I repeat:

Use Directory.GetFiles to find the list of HTML files.
Go through each of the files it returns, and call File.Copy with it.

What's the matter with that solution?
 
M

Mike

Actually i just tried that it i get, "can't a files that already exsist"
when i try the file.copy piece
 
J

Jon Skeet [C# MVP]

Mike said:
Actually i just tried that it i get, "can't a files that already exsist"
when i try the file.copy piece

Then you presumably either want the version that will overwrite
existing files, or check whether or not the destination file exists
first, and ignore that file if so.
 
M

Mike

were can i find a snippet of code or an example that does this? I want to
see verify that my syntax is correct. I'm getting a new error everytime I
run the app
 
J

Jon Skeet [C# MVP]

Mike said:
were can i find a snippet of code or an example that does this? I want to
see verify that my syntax is correct. I'm getting a new error everytime I
run the app

Why don't you write a test console app then? That way you won't need to
go through much to find the problems.
 

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