Copy Directory

  • Thread starter Thread starter samoore33
  • Start date Start date
S

samoore33

I found a real nice article on how to copy a directory on MSDN. The
only problem is that I can not figure out how to get the namespace
Microsoft.VisualBasic.MyServices. I wanted to know if anyone else has
had experience with this, or knows of a different way to copy a
directory using VB.

I found ways to copy files, but not having a lot of luck with finding a
way to copy a directory other then this information I am finding on
MSDN.

Thanks

Scott Moore
 
Scott,

You may or may not like this method, but I thought I'd post it anyway.

You could use xcopy in a .bat file to copy the directory from one
location to another. Using different switches for xcopy you can include
hidden files, subdirectories, ignore errors, automatically create
subdirectories, turn message prompts off and a bunch of other options.
Here is a complete list of switch options for xcopy:

http://www.computerhope.com/xcopyhlp.htm

You could call this .bat file using
System.Diagnostics.Process.Start([Path To .bat File])
I think you should be able to hide the command window from showing up.

By starting a new process you would be able to tell when the process
finishes. Additionally you could modify the .bat file in the future so
code changes would not be necessary.

Just a thought,
Izzy
 
Or use just System.IO.Directory... Some of the 2.0 My features are just
wrappers around the .NET framework classes available elsewhere...
 
In the System.IO.Directory I can only find Move. I do not see where I
can copy a directory. The Move works, but it cuts the directory, it
does not copy it.

Scott
 
I had used a bat file before, but had not thought of calling it through
code. Thanks for the idea.

Scott
Scott,

You may or may not like this method, but I thought I'd post it anyway.

You could use xcopy in a .bat file to copy the directory from one
location to another. Using different switches for xcopy you can include
hidden files, subdirectories, ignore errors, automatically create
subdirectories, turn message prompts off and a bunch of other options.
Here is a complete list of switch options for xcopy:

http://www.computerhope.com/xcopyhlp.htm

You could call this .bat file using
System.Diagnostics.Process.Start([Path To .bat File])
I think you should be able to hide the command window from showing up.

By starting a new process you would be able to tell when the process
finishes. Additionally you could modify the .bat file in the future so
code changes would not be necessary.

Just a thought,
Izzy
I found a real nice article on how to copy a directory on MSDN. The
only problem is that I can not figure out how to get the namespace
Microsoft.VisualBasic.MyServices. I wanted to know if anyone else has
had experience with this, or knows of a different way to copy a
directory using VB.

I found ways to copy files, but not having a lot of luck with finding a
way to copy a directory other then this information I am finding on
MSDN.

Thanks

Scott Moore
 
I am trying to use the XCOPY command in a batch file. This is what I am
trying to do.

@echo off

XCOPY C:\Pictures C:\Documents and Settings\smoore\My
Documents\Pictures

Pause

When I run this I get Invalid number of parameters?

Not sure what I am doing wrong here, please advise.

Scott
Scott,

You may or may not like this method, but I thought I'd post it anyway.

You could use xcopy in a .bat file to copy the directory from one
location to another. Using different switches for xcopy you can include
hidden files, subdirectories, ignore errors, automatically create
subdirectories, turn message prompts off and a bunch of other options.
Here is a complete list of switch options for xcopy:

http://www.computerhope.com/xcopyhlp.htm

You could call this .bat file using
System.Diagnostics.Process.Start([Path To .bat File])
I think you should be able to hide the command window from showing up.

By starting a new process you would be able to tell when the process
finishes. Additionally you could modify the .bat file in the future so
code changes would not be necessary.

Just a thought,
Izzy
I found a real nice article on how to copy a directory on MSDN. The
only problem is that I can not figure out how to get the namespace
Microsoft.VisualBasic.MyServices. I wanted to know if anyone else has
had experience with this, or knows of a different way to copy a
directory using VB.

I found ways to copy files, but not having a lot of luck with finding a
way to copy a directory other then this information I am finding on
MSDN.

Thanks

Scott Moore
 
Try This:

XCOPY C:\Pictures "C:\Documents and Settings\smoore\My Documents
\Pictures"


I am trying to use the XCOPY command in a batch file. This is what I
am trying to do.

@echo off

XCOPY C:\Pictures C:\Documents and Settings\smoore\My
Documents\Pictures

Pause

When I run this I get Invalid number of parameters?

Not sure what I am doing wrong here, please advise.

Scott
Scott,

You may or may not like this method, but I thought I'd post it
anyway.

You could use xcopy in a .bat file to copy the directory from one
location to another. Using different switches for xcopy you can
include hidden files, subdirectories, ignore errors, automatically
create subdirectories, turn message prompts off and a bunch of other
options. Here is a complete list of switch options for xcopy:

http://www.computerhope.com/xcopyhlp.htm

You could call this .bat file using
System.Diagnostics.Process.Start([Path To .bat File])
I think you should be able to hide the command window from showing
up.

By starting a new process you would be able to tell when the process
finishes. Additionally you could modify the .bat file in the future
so code changes would not be necessary.

Just a thought,
Izzy
I found a real nice article on how to copy a directory on MSDN. The
only problem is that I can not figure out how to get the namespace
Microsoft.VisualBasic.MyServices. I wanted to know if anyone else
has had experience with this, or knows of a different way to copy a
directory using VB.

I found ways to copy files, but not having a lot of luck with
finding a way to copy a directory other then this information I am
finding on MSDN.

Thanks

Scott Moore
 
Try this:

XCOPY C:\Pictures "C:\Documents and Settings\smoore\My
Documents\Pictures" /e /c /i /h /k

This issus is you didn't enclose the path with spaces in quatation
marks. Look at the link I sent too see what the switches mean.

Izzy

I am trying to use the XCOPY command in a batch file. This is what I am
trying to do.

@echo off

XCOPY C:\Pictures C:\Documents and Settings\smoore\My
Documents\Pictures

Pause

When I run this I get Invalid number of parameters?

Not sure what I am doing wrong here, please advise.

Scott
Scott,

You may or may not like this method, but I thought I'd post it anyway.

You could use xcopy in a .bat file to copy the directory from one
location to another. Using different switches for xcopy you can include
hidden files, subdirectories, ignore errors, automatically create
subdirectories, turn message prompts off and a bunch of other options.
Here is a complete list of switch options for xcopy:

http://www.computerhope.com/xcopyhlp.htm

You could call this .bat file using
System.Diagnostics.Process.Start([Path To .bat File])
I think you should be able to hide the command window from showing up.

By starting a new process you would be able to tell when the process
finishes. Additionally you could modify the .bat file in the future so
code changes would not be necessary.

Just a thought,
Izzy
I found a real nice article on how to copy a directory on MSDN. The
only problem is that I can not figure out how to get the namespace
Microsoft.VisualBasic.MyServices. I wanted to know if anyone else has
had experience with this, or knows of a different way to copy a
directory using VB.

I found ways to copy files, but not having a lot of luck with finding a
way to copy a directory other then this information I am finding on
MSDN.

Thanks

Scott Moore
 
Thanks a lot Izzy, it is working now.
Try this:

XCOPY C:\Pictures "C:\Documents and Settings\smoore\My
Documents\Pictures" /e /c /i /h /k

This issus is you didn't enclose the path with spaces in quatation
marks. Look at the link I sent too see what the switches mean.

Izzy

I am trying to use the XCOPY command in a batch file. This is what I am
trying to do.

@echo off

XCOPY C:\Pictures C:\Documents and Settings\smoore\My
Documents\Pictures

Pause

When I run this I get Invalid number of parameters?

Not sure what I am doing wrong here, please advise.

Scott
Scott,

You may or may not like this method, but I thought I'd post it anyway.

You could use xcopy in a .bat file to copy the directory from one
location to another. Using different switches for xcopy you can include
hidden files, subdirectories, ignore errors, automatically create
subdirectories, turn message prompts off and a bunch of other options.
Here is a complete list of switch options for xcopy:

http://www.computerhope.com/xcopyhlp.htm

You could call this .bat file using
System.Diagnostics.Process.Start([Path To .bat File])
I think you should be able to hide the command window from showing up.

By starting a new process you would be able to tell when the process
finishes. Additionally you could modify the .bat file in the future so
code changes would not be necessary.

Just a thought,
Izzy

samoore33 wrote:
I found a real nice article on how to copy a directory on MSDN. The
only problem is that I can not figure out how to get the namespace
Microsoft.VisualBasic.MyServices. I wanted to know if anyone else has
had experience with this, or knows of a different way to copy a
directory using VB.

I found ways to copy files, but not having a lot of luck with finding a
way to copy a directory other then this information I am finding on
MSDN.

Thanks

Scott Moore
 
Why can't you write a simple routine to copy all the files in your source
directory to your new directory using the copy method of the file class.
--
Dennis in Houston


samoore33 said:
Thanks a lot Izzy, it is working now.
Try this:

XCOPY C:\Pictures "C:\Documents and Settings\smoore\My
Documents\Pictures" /e /c /i /h /k

This issus is you didn't enclose the path with spaces in quatation
marks. Look at the link I sent too see what the switches mean.

Izzy

I am trying to use the XCOPY command in a batch file. This is what I am
trying to do.

@echo off

XCOPY C:\Pictures C:\Documents and Settings\smoore\My
Documents\Pictures

Pause

When I run this I get Invalid number of parameters?

Not sure what I am doing wrong here, please advise.

Scott

Izzy wrote:
Scott,

You may or may not like this method, but I thought I'd post it anyway.

You could use xcopy in a .bat file to copy the directory from one
location to another. Using different switches for xcopy you can include
hidden files, subdirectories, ignore errors, automatically create
subdirectories, turn message prompts off and a bunch of other options.
Here is a complete list of switch options for xcopy:

http://www.computerhope.com/xcopyhlp.htm

You could call this .bat file using
System.Diagnostics.Process.Start([Path To .bat File])
I think you should be able to hide the command window from showing up.

By starting a new process you would be able to tell when the process
finishes. Additionally you could modify the .bat file in the future so
code changes would not be necessary.

Just a thought,
Izzy

samoore33 wrote:
I found a real nice article on how to copy a directory on MSDN. The
only problem is that I can not figure out how to get the namespace
Microsoft.VisualBasic.MyServices. I wanted to know if anyone else has
had experience with this, or knows of a different way to copy a
directory using VB.

I found ways to copy files, but not having a lot of luck with finding a
way to copy a directory other then this information I am finding on
MSDN.

Thanks

Scott Moore
 
I was thinking the same thing, why not just write a recursive function to do
it for you. It isn't difficult.

Hope this helps
Greetz, Peter

See sample code below.

Private Sub DirectoryCopy(ByVal src As String, ByVal dest As String)

If Not Directory.Exists(dest) Then
Directory.CreateDirectory(dest)
End If

If Directory.Exists(src) And Directory.Exists(dest) Then

Dim di As New DirectoryInfo(src)
Dim fsi As FileSystemInfo

For Each fsi In di.GetFileSystemInfos()
Try
Dim destName As String = Path.Combine(dest, fsi.Name)
If TypeOf fsi Is FileInfo Then
If fsi.Attributes = FileAttributes.ReadOnly Then
fsi.Attributes = FileAttributes.Normal
End If
File.Copy(fsi.FullName, destName, True)
Else
Directory.CreateDirectory(destName)
DirectoryCopy(fsi.FullName, destName)
End If
Catch ex As Exception
MessageBox.Show(ex.ToString, "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
Next

Else
MessageBox.Show("Source or target doesn't exist.", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
End Sub

--
Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the Universe trying to produce
bigger and better idiots. So far, the Universe is winning. (Rich Cook)

Dennis said:
Why can't you write a simple routine to copy all the files in your source
directory to your new directory using the copy method of the file class.
--
Dennis in Houston


samoore33 said:
Thanks a lot Izzy, it is working now.
Try this:

XCOPY C:\Pictures "C:\Documents and Settings\smoore\My
Documents\Pictures" /e /c /i /h /k

This issus is you didn't enclose the path with spaces in quatation
marks. Look at the link I sent too see what the switches mean.

Izzy


samoore33 wrote:
I am trying to use the XCOPY command in a batch file. This is what I am
trying to do.

@echo off

XCOPY C:\Pictures C:\Documents and Settings\smoore\My
Documents\Pictures

Pause

When I run this I get Invalid number of parameters?

Not sure what I am doing wrong here, please advise.

Scott

Izzy wrote:
Scott,

You may or may not like this method, but I thought I'd post it anyway.

You could use xcopy in a .bat file to copy the directory from one
location to another. Using different switches for xcopy you can include
hidden files, subdirectories, ignore errors, automatically create
subdirectories, turn message prompts off and a bunch of other options.
Here is a complete list of switch options for xcopy:

http://www.computerhope.com/xcopyhlp.htm

You could call this .bat file using
System.Diagnostics.Process.Start([Path To .bat File])
I think you should be able to hide the command window from showing up.

By starting a new process you would be able to tell when the process
finishes. Additionally you could modify the .bat file in the future so
code changes would not be necessary.

Just a thought,
Izzy

samoore33 wrote:
I found a real nice article on how to copy a directory on MSDN. The
only problem is that I can not figure out how to get the namespace
Microsoft.VisualBasic.MyServices. I wanted to know if anyone else has
had experience with this, or knows of a different way to copy a
directory using VB.

I found ways to copy files, but not having a lot of luck with finding a
way to copy a directory other then this information I am finding on
MSDN.

Thanks

Scott Moore
 
Thanks for the code Peter, I'll archive it for later.

I think I still favor the xcopy method only because it's simple and
achives the same result.

Izzy


Peter said:
I was thinking the same thing, why not just write a recursive function to do
it for you. It isn't difficult.

Hope this helps
Greetz, Peter

See sample code below.

Private Sub DirectoryCopy(ByVal src As String, ByVal dest As String)

If Not Directory.Exists(dest) Then
Directory.CreateDirectory(dest)
End If

If Directory.Exists(src) And Directory.Exists(dest) Then

Dim di As New DirectoryInfo(src)
Dim fsi As FileSystemInfo

For Each fsi In di.GetFileSystemInfos()
Try
Dim destName As String = Path.Combine(dest, fsi.Name)
If TypeOf fsi Is FileInfo Then
If fsi.Attributes = FileAttributes.ReadOnly Then
fsi.Attributes = FileAttributes.Normal
End If
File.Copy(fsi.FullName, destName, True)
Else
Directory.CreateDirectory(destName)
DirectoryCopy(fsi.FullName, destName)
End If
Catch ex As Exception
MessageBox.Show(ex.ToString, "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
Next

Else
MessageBox.Show("Source or target doesn't exist.", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
End Sub

--
Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the Universe trying to produce
bigger and better idiots. So far, the Universe is winning. (Rich Cook)

Dennis said:
Why can't you write a simple routine to copy all the files in your source
directory to your new directory using the copy method of the file class.
--
Dennis in Houston


samoore33 said:
Thanks a lot Izzy, it is working now.

Izzy wrote:
Try this:

XCOPY C:\Pictures "C:\Documents and Settings\smoore\My
Documents\Pictures" /e /c /i /h /k

This issus is you didn't enclose the path with spaces in quatation
marks. Look at the link I sent too see what the switches mean.

Izzy


samoore33 wrote:
I am trying to use the XCOPY command in a batch file. This is what I am
trying to do.

@echo off

XCOPY C:\Pictures C:\Documents and Settings\smoore\My
Documents\Pictures

Pause

When I run this I get Invalid number of parameters?

Not sure what I am doing wrong here, please advise.

Scott

Izzy wrote:
Scott,

You may or may not like this method, but I thought I'd post it anyway.

You could use xcopy in a .bat file to copy the directory from one
location to another. Using different switches for xcopy you can include
hidden files, subdirectories, ignore errors, automatically create
subdirectories, turn message prompts off and a bunch of other options.
Here is a complete list of switch options for xcopy:

http://www.computerhope.com/xcopyhlp.htm

You could call this .bat file using
System.Diagnostics.Process.Start([Path To .bat File])
I think you should be able to hide the command window from showing up.

By starting a new process you would be able to tell when the process
finishes. Additionally you could modify the .bat file in the future so
code changes would not be necessary.

Just a thought,
Izzy

samoore33 wrote:
I found a real nice article on how to copy a directory on MSDN. The
only problem is that I can not figure out how to get the namespace
Microsoft.VisualBasic.MyServices. I wanted to know if anyone else has
had experience with this, or knows of a different way to copy a
directory using VB.

I found ways to copy files, but not having a lot of luck with finding a
way to copy a directory other then this information I am finding on
MSDN.

Thanks

Scott Moore
 
Back
Top