|
-
Aug 18th, 2005, 11:34 AM
#1
Thread Starter
Junior Member
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.
-
Aug 18th, 2005, 11:48 AM
#2
Re: Try to run Shell command to excute Xcopy in Visual Basic
You need shellexecute api in order to be able to use parameters.
-
Aug 18th, 2005, 11:49 AM
#3
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.
-
Aug 18th, 2005, 11:52 AM
#4
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:
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 Command1_Click()
'============================
CopyFolder "c:\temp\temp1", "c:\temp\temp2"
End Sub
-
Aug 18th, 2005, 11:56 AM
#5
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.
-
Aug 18th, 2005, 01:39 PM
#6
Re: Try to run Shell command to excute Xcopy in Visual Basic
I presumed it didn't but wasn't sure so I said "... not sure ..."
-
Aug 18th, 2005, 02:11 PM
#7
Thread Starter
Junior Member
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]
-
Aug 18th, 2005, 02:20 PM
#8
Re: Try to run Shell command to excute Xcopy in Visual Basic
 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|