Results 1 to 8 of 8

Thread: Try to run Shell command to excute Xcopy in Visual Basic

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2005
    Posts
    26

    Exclamation Try to run Shell command to excute Xcopy in Visual Basic

    I am trying to run xcopy from Visual Basic using shell and it is not working. Any help is appreciated!

    Shell "xcopy C:\Documents and Settings\sName\*.* /E /F /G /H /K /O /Y c:\temp\"

    Am I doing it right?

    The sNAME is a variable name that was inputed by the user when I prompt them to put their user name beforehand. (sName = UserID.Text)

    I took the variable sNAME out and put my name which is a valid folder on the system and ran the shell command from VB and it did not do anything. (it is supposed to copy everything from C:\Documents and Settings\jatalife folder to c:\temp.)

    Do I need to use batch file to do the copy? If so how can send the USERNAME in the batch file to copy the correct directory for the user.

  2. #2
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: Try to run Shell command to excute Xcopy in Visual Basic

    You need shellexecute api in order to be able to use parameters.

  3. #3
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: Try to run Shell command to excute Xcopy in Visual Basic

    Also, if you want to copy an entire folder, I have seen code here on the forums, just search for FolderCopy or something similar.

  4. #4
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Try to run Shell command to excute Xcopy in Visual Basic

    Not sure if XCopy supports long file names but if you're looking for some way to inject variable then the following is what you might need:

    Shell "xcopy C:\Documents and Settings\" & sName & "\*.* /E /F /G /H /K /O /Y c:\temp\"

    Also, here is a sample that copy entire folder (the Windows way and not DOS):
    VB Code:
    1. Option Explicit
    2.  
    3. Private Type SHFILEOPSTRUCT
    4.         hwnd As Long
    5.         wFunc As Long
    6.         pFrom As String
    7.         pTo As String
    8.         fFlags As Integer
    9.         fAnyOperationsAborted As Long
    10.         hNameMappings As Long
    11.         lpszProgressTitle As String '  only used if FOF_SIMPLEPROGRESS
    12. End Type
    13.  
    14. Private Const FOF_MULTIDESTFILES = &H1
    15. Private Const FOF_CONFIRMMOUSE = &H2
    16. Private Const FOF_SILENT = &H4
    17. Private Const FOF_RENAMEONCOLLISION = &H8
    18. Private Const FOF_NOCONFIRMATION = &H10
    19. Private Const FOF_WANTMAPPINGHANDLE = &H20
    20. Private Const FOF_CREATEPROGRESSDLG = &H0
    21. Private Const FOF_ALLOWUNDO = &H40
    22. Private Const FOF_FILESONLY = &H80
    23. Private Const FOF_SIMPLEPROGRESS = &H100
    24. Private Const FOF_NOCONFIRMMKDIR = &H200
    25.  
    26. Private Const FO_MOVE = 1
    27. Private Const FO_COPY = 2
    28. Private Const FO_DELETE = 3
    29. Private Const FO_RENAME = 4
    30.  
    31. Private Declare Function SHFileOperation Lib "shell32.dll" (lpFileOp As SHFILEOPSTRUCT) As Long
    32.    
    33. Public Function CopyFolder(ByVal strSource As String, ByVal strDest As String) As Boolean
    34. '==========================================================================================
    35. Dim varFOS As SHFILEOPSTRUCT
    36.     With varFOS
    37.         .fFlags = FOF_NOCONFIRMATION Or FOF_SILENT Or FOF_NOCONFIRMMKDIR
    38.         .wFunc = FO_COPY
    39.         .pFrom = strSource
    40.         .pTo = strDest
    41.     End With
    42.     Call SHFileOperation(varFOS)
    43.     CopyFolder = (varFOS.fAnyOperationsAborted = 0)
    44. End Function
    45.  
    46. Private Sub Command1_Click()
    47. '============================
    48.  
    49.     CopyFolder "c:\temp\temp1", "c:\temp\temp2"
    50.  
    51. End Sub

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Try to run Shell command to excute Xcopy in Visual Basic

    FYI to anyone reading this thread.

    To my knowledge, XCopy does not support long file names.

  6. #6

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Aug 2005
    Posts
    26

    Re: Try to run Shell command to excute Xcopy in Visual Basic

    Thanks a lot..

    but when I copid the code in my vb it did not work.

    Compile Error:
    Only Comments may appear after End Sub, End Function, or End property

    On this line
    Private Declare Function SHFileOperation Lib "shell32.dll" (lpFileOp As SHFILEOPSTRUCT) As Long

    >> This is what I copied ..
    Option Explicit

    Private Type SHFILEOPSTRUCT
    hwnd As Long
    wFunc As Long
    pFrom As String
    pTo As String
    fFlags As Integer
    fAnyOperationsAborted As Long
    hNameMappings As Long
    lpszProgressTitle As String ' only used if FOF_SIMPLEPROGRESS
    End Type

    Private Const FOF_MULTIDESTFILES = &H1
    Private Const FOF_CONFIRMMOUSE = &H2
    Private Const FOF_SILENT = &H4
    Private Const FOF_RENAMEONCOLLISION = &H8
    Private Const FOF_NOCONFIRMATION = &H10
    Private Const FOF_WANTMAPPINGHANDLE = &H20
    Private Const FOF_CREATEPROGRESSDLG = &H0
    Private Const FOF_ALLOWUNDO = &H40
    Private Const FOF_FILESONLY = &H80
    Private Const FOF_SIMPLEPROGRESS = &H100
    Private Const FOF_NOCONFIRMMKDIR = &H200

    Private Const FO_MOVE = 1
    Private Const FO_COPY = 2
    Private Const FO_DELETE = 3
    Private Const FO_RENAME = 4

    Private Declare Function SHFileOperation Lib "shell32.dll" (lpFileOp As SHFILEOPSTRUCT) As Long

    Public Function CopyFolder(ByVal strSource As String, ByVal strDest As String) As Boolean
    '================================================================
    Dim varFOS As SHFILEOPSTRUCT
    With varFOS
    .fFlags = FOF_NOCONFIRMATION Or FOF_SILENT Or FOF_NOCONFIRMMKDIR
    .wFunc = FO_COPY
    .pFrom = strSource
    .pTo = strDest
    End With
    Call SHFileOperation(varFOS)
    CopyFolder = (varFOS.fAnyOperationsAborted = 0)
    End Function

    Private Sub Command2_Click()
    '============================

    CopyFolder "C:\Documents and Settings\" & sName & "\Favorites\", "c:\temp\test"

    End Sub

    Can u e-mail me the vb project [email protected]

  8. #8
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Try to run Shell command to excute Xcopy in Visual Basic

    Quote Originally Posted by jatalife
    ...
    Compile Error:
    Only Comments may appear after End Sub, End Function, or End property
    First: edit your post and GET RID OF YOUR PERSONAL EMAIL ADDRESS !!!

    That error is generated if you copy some declarations at the end of your code.
    What you have to do is "move" it to the top into a general section.
    And sorry, I will not send you any projects through email.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width