954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Using XCOPY dos command in VB.NET

I am trying to copy the contents of a folder into another folder in another drive using XCOPY.

Here is code I have now but it isn't working and I figure xcopy would be the easier way to go because MOVETO and CopyDirectory haven't been working because its copying files to a different directory.

Sub CopyFiles()

        Dim DestinationDirectory As String = "I:\New\Export\ID"
        Dim SourceDirectory As String = "C:\Temp2\"


        Try
            Dim f() As String = Directory.GetFiles(SourceDirectory)
            For i As Integer = 0 To UBound(f)
                File.Copy(f(i), DestinationDirectory & "\" & SourceDirectory(f(i)))
            Next
        Catch ex As Exception

        End Try

    End Sub
sastokes
Junior Poster in Training
50 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

why so complicated? Try this:

Dim DestinationDirectory As String = "I:\New\Export\ID"
Dim SourceDirectory As String = "C:\Temp2\"
Dim overwrite As boolean = true
my.Computer.FileSystem.CopyDirectory(SourceDirectory,DestinationDirectory,overwrite)
GeekByChoiCe
Master Poster
721 posts since Jun 2009
Reputation Points: 208
Solved Threads: 168
 

why so complicated? Try this:

Dim DestinationDirectory As String = "I:\New\Export\ID"
Dim SourceDirectory As String = "C:\Temp2\"
Dim overwrite As boolean = true
my.Computer.FileSystem.CopyDirectory(SourceDirectory,DestinationDirectory,overwrite)

Thank you! This does work, but it won't let me copy from my C drive to my I drive. It's saying it can't find the location of the I drive.

sastokes
Junior Poster in Training
50 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

The question is:
does this directory "I:\New\Export\ID" already exist?

If Not IO.Directory.Exists(DestinationDirectory) Then
			IO.Directory.CreateDirectory(DestinationDirectory)
		End If
GeekByChoiCe
Master Poster
721 posts since Jun 2009
Reputation Points: 208
Solved Threads: 168
 

The question is: does this directory "I:\New\Export\ID" already exist?

If Not IO.Directory.Exists(DestinationDirectory) Then
			IO.Directory.CreateDirectory(DestinationDirectory)
		End If

Yes it exists already.

sastokes
Junior Poster in Training
50 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

Please paste the exact error message.

GeekByChoiCe
Master Poster
721 posts since Jun 2009
Reputation Points: 208
Solved Threads: 168
 
Please paste the exact error message.

System.IO.DirectoryNotFoundException: Could not find a part of the path 'I:\New\Export\ID'.

sastokes
Junior Poster in Training
50 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

Is this Drive mapped to a different computer?
If so then change the path to: \\other_computer\New\Export\ID

GeekByChoiCe
Master Poster
721 posts since Jun 2009
Reputation Points: 208
Solved Threads: 168
 
Is this Drive mapped to a different computer? If so then change the path to: \\other_computer\New\Export\ID

ok well that error is gone, but now it gives me this one:

System.IO.IOException: Could not complete operation on some files and directories. See the Data property of the exception for more details

sastokes
Junior Poster in Training
50 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

and what are the details?

GeekByChoiCe
Master Poster
721 posts since Jun 2009
Reputation Points: 208
Solved Threads: 168
 
and what are the details?

IOException: Could not complete operation on some files and directories. See the Data property of the exception for more details.]
Microsoft.VisualBasic.FileIO.FileSystem.FxCopyOrMoveDirectory(CopyOrMove operation, String sourceDirectoryPath, String targetDirectoryPath, Boolean overwrite) +296170
Microsoft.VisualBasic.FileIO.FileSystem.CopyOrMoveDirectory(CopyOrMove operation, String sourceDirectoryName, String destinationDirectoryName, Boolean overwrite, UIOptionInternal showUI, UICancelOption onUserCancel) +304
Microsoft.VisualBasic.MyServices.FileSystemProxy.CopyDirectory(String sourceDirectoryName, String destinationDirectoryName, Boolean overwrite) +20
_Export.CopyFiles() in D:\importer\Export_Tool.aspx.vb:176
_Export.Export_Click(Object sender, EventArgs e) in D:\importer\Export_Tool.aspx.vb:134
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565

sastokes
Junior Poster in Training
50 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

did you try to set the credentials for the remote computer? like so:
\\username:password@machinename\directory\to\copy\to

GeekByChoiCe
Master Poster
721 posts since Jun 2009
Reputation Points: 208
Solved Threads: 168
 
did you try to set the credentials for the remote computer? like so: \\username:password@machinename\directory\to\copy\to

I would write the string first then set another string as credentials to what you just said?

**sorry I'm kinda new to vb**

sastokes
Junior Poster in Training
50 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 
did you try to set the credentials for the remote computer? like so: \\username:password@machinename\directory\to\copy\to

ok how about doing this to login?

Dim p As New System.Diagnostics.Process
p.StartInfo.RedirectStandardOutput = True
p.StartInfo.RedirectStandardInput = True
p.StartInfo.FileName = "xcopy.exe"
p.StartInfo.UseShellExecute = False
p.StartInfo.RedirectStandardError = True
p.StartInfo.WorkingDirectory = "C:\"
p.StartInfo.LoadUserProfile = True
p.StartInfo.UserName = "username"
p.StartInfo.Domain = "pass"
Dim pw As New System.Security.SecureString
For Each ch As Char In (Chr(36) & Chr(111) & Chr(55) & Chr(100) & Chr(105) & Chr(101) & Chr(114) & Chr(115))
pw.AppendChar(ch)
Next
p.StartInfo.Password = pw

p.Start()

sastokes
Junior Poster in Training
50 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

i actually meant:
Dim DestinationDirectory As String = "\\username:password@machinename\New\Export\ID"

GeekByChoiCe
Master Poster
721 posts since Jun 2009
Reputation Points: 208
Solved Threads: 168
 
i actually meant: Dim DestinationDirectory As String = "\\username:password@machinename\New\Export\ID"

I keep getting the error System.NotSupportedException: The given path's format is not supported.

sastokes
Junior Poster in Training
50 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

I'm running out of ideas...I think the best thing to do is, zipping your project and attach it to this thread. so we can take a look on it.

GeekByChoiCe
Master Poster
721 posts since Jun 2009
Reputation Points: 208
Solved Threads: 168
 

put your code in try catch statement and post your exception message.

Try
            ''your code in here
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
bluehangook629
Posting Whiz in Training
204 posts since Dec 2009
Reputation Points: 11
Solved Threads: 14
 
I'm running out of ideas...I think the best thing to do is, zipping your project and attach it to this thread. so we can take a look on it.

I tried your last idea and it worked! Thanks so much!

sastokes
Junior Poster in Training
50 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

I'm glad that it helped

bluehangook629
Posting Whiz in Training
204 posts since Dec 2009
Reputation Points: 11
Solved Threads: 14
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: