Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Is it possible in VB .Net to use XCOPY with VB variables

Posted on 2009-05-14
1
Medium Priority
?
1,260 Views
Last Modified: 2013-11-10
I am just wondering how I would add Visual Basic variables (folder path) to a xcopy command in visual basic .net.  I have the code that I have attempted to use.  I have also commented out a few ideas that I tried, but didnt work.  I am trying to get around the issue of copying the documents folder in Vista.  Those legacy folders for "My Videos, My Pictures, etc" are casuing problems copying that folder.  Any suggestions would be helpful.

Thanks,
Joe
Dim local_users As String
                ' Here is where we loop through the selected items in the listbox.
 
                For Each local_users In lstUsers.SelectedItems
 
                    ' We need to formulate the full path again to test if it exists and to copy the file.
                    'If File.Exists("c:\users\" & theSelectedFile) Then
 
                    ' Here we do a simple file.copy call where we specify the file we want and then
                    ' specify the destination where we want to go. We let the user know each file that was
                    ' copied as well.
                    Dim objShell As Object
                    objShell = CreateObject("WScript.Shell")
                    Dim path1 As String
                    Dim path2 As String
                    path1 = ("C:\Users\" & local_users & "\Documents")
                    path2 = (FolderChoiceTextBox.Text & "\" & local_users & "\Documents")
 
                    My.Computer.FileSystem.CopyDirectory("c:\users\" & local_users & "\Desktop", FolderChoiceTextBox.Text & "\" & local_users & "\Desktop")
                    My.Computer.FileSystem.CopyDirectory("c:\users\" & local_users & "\Favorites", FolderChoiceTextBox.Text & "\" & local_users & "\Favorites")
                    My.Computer.FileSystem.CopyDirectory("c:\users\" & local_users & "\Music", FolderChoiceTextBox.Text & "\" & local_users & "\Music")
                    My.Computer.FileSystem.CopyDirectory("c:\users\" & local_users & "\Pictures", FolderChoiceTextBox.Text & "\" & local_users & "\Pictures")
                    'objShell.run("xcopy c:\users\" & theSelectedFile & "\Documents", FolderChoiceTextBox.Text & "\" & theSelectedFile & "\Documents")
                    Shell("xcopy" & path1, path2)
                    MessageBox.Show("Copied: " & local_users)

Open in new window

0
Comment
Question by:lehmaj1
1 Comment
 
LVL 16

Accepted Solution

by:
HooKooDooKu earned 2000 total points
ID: 24389438
At a minimum, are you including a space between the word "xcopy" and path1, or is the resulting string looking like this:

 "xcopyc:\users...

Additionally, when working with a command line, if a file name has an embedded space, the file name needs to be encloded in quotation marks:

xcopy "File with embeded space" "To File with embeded space

My suggestion is to start with a command prompt (Start/Programs/Accessories/Command Promt) and attempt to get xcopy to work from there.  Once you have the exact text for the command working at a DOS prompt, replicate that command line EXACTLY in VB.  A big help might be to assign the command to a string and then call Shell with the string variable.  That way you can use the debugger and see EXACTLY what is getting sent to the Shell command.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
When you discover the power of the R programming language, you are going to wonder how you ever lived without it! Learn why the language merits a place in your programming arsenal.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

926 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question