-
Help Please simple copy files
hey im making a program to copy files from point a to point b this will contain a list of jar files i would like to use cmd but control it with my program i have an idea of wat to do but basicly i wanna move directorys and jar files with different names im using this as a back up program its not that many files in cmd it takes like 1 min but i wanna release this to public with out people recoding it and sending viruses to every one if you get my drift thats y i wanna use vb instead of a bat file
-
Re: Help Please simple copy files
-
Re: Help Please simple copy files
you could use vb filecopy
-
Re: Help Please simple copy files
Keep in mind that if the file is in use when you attempt to copy it the copy will error out so make sure you build in the necessary error trapping.
-
Re: Help Please simple copy files
is there another way to copy files if so can you supply code
-
Re: Help Please simple copy files
As Westconn1 said you can use filecopy for a simple copy method:
vb Code:
Private Sub Command1_Click()
FileCopy "c:\.rnd", "g:\.rnd"
End Sub
or a slightly more advanced method which copies the entire contents of a folder:
vb Code:
Option Explicit
Private source As String, destination As String
Private Sub Command1_Click()
source ="c:\New Folder\"
destination = "d:\New Folder\"
VBCopyFolder(source, destination)
End Sub
Place this in a module:
vb Code:
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 Long ' only used if FOF_SIMPLEPROGRESS, sets dialog title
End Type
Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long
' Available Operations
Private Const FO_COPY = &H2 ' Copy File/Folder
Private Const FO_DELETE = &H3 ' Delete File/Folder
Private Const FO_MOVE = &H1 ' Move File/Folder
Private Const FO_RENAME = &H4 ' Rename File/Folder
' Flags
Private Const FOF_ALLOWUNDO = &H40 ' Allow to undo rename, delete ie sends to recycle bin
Private Const FOF_FILESONLY = &H80 ' Only allow files
Private Const FOF_NOCONFIRMATION = &H10 ' No File Delete or Overwrite Confirmation Dialog
Private Const FOF_SILENT = &H4 ' No copy/move dialog
Private Const FOF_SIMPLEPROGRESS = &H100 ' Does not display file names
Public Sub VBCopyFolder(ByRef strSource As String, ByRef strTarget As String)
Dim op As SHFILEOPSTRUCT
With op
.wFunc = FO_COPY ' Set function
.pTo = strTarget ' Set new path
.pFrom = strSource ' Set current path
.fFlags = FOF_SIMPLEPROGRESS Or FOF_NOCONFIRMATION And FOF_FILESONLY
End With
' Perform operation
SHFileOperation op
End Sub
-
Re: Help Please simple copy files
night walker you helped me alot ill see wat i can do and see if this does wat i would like it to do until then i will let you know
-
Re: Help Please simple copy files
the code is erroring on line 34 in the module can you check over the code and make sure its correct please and thank you im using the vbcopyfolder btw just to let you know
-
Re: Help Please simple copy files
Quote:
Originally Posted by
Jeremy
the code is erroring on line 34 in the module can you check over the code and make sure its correct please and thank you im using the vbcopyfolder btw just to let you know
What is the error you receive on that line?
-
Re: Help Please simple copy files
Compile Error:
Varible Required - Can't Assdign to This Expression
its also highlighting op
hopefully this helps
-
Re: Help Please simple copy files
Can you post the exact code you are using? I created a program using the exact code in post #6 and it works for me.
-
Re: Help Please simple copy files
have you change the source and destination folders to folders that exist on your computer?
(lines 4 and 5)
-
Re: Help Please simple copy files
You need to remove the brackets on this line as well
Code:
VBCopyFolder(source, destination)
You may also need to remove the trailing "\" on the source folder, or add *.* after it.
EDIT: and the fflags settings should be
Code:
.fFlags = FOF_SIMPLEPROGRESS Or FOF_NOCONFIRMATION Or FOF_FILESONLY
Also you should check the return from the function SHFileoperation ( 0 = success) and also the fAnyOperationsAborted member of the SHFILEOPSTRUCT structure (0 = no operations aborted)
Code:
' Perform operation
If SHFileOperation(op) = 0 Then
If op.fAnyOperationsAborted = 0 Then
MsgBox "Copy Complete"
Else
MsgBox "Copy Incomplete"
End If
Else
MsgBox "Copy Failed"
End If
End Sub
-
Re: Help Please simple copy files
is there a way that i can force a overwrite when im coping the folder :/
-
Re: Help Please simple copy files
-
Re: Help Please simple copy files
ok i get that and everything but how would i enable that in my code i dont really understand
-
Re: Help Please simple copy files
vb Code:
Dim op As SHFILEOPSTRUCT
With op
.wFunc = FO_COPY ' Set function
.pTo = strTarget ' Set new path
.pFrom = strSource ' Set current path
.fFlags = FOF_SIMPLEPROGRESS Or FOF_NOCONFIRMATION Or FOF_FILESONLY AND FOF_SILENT
End With
See the ".fFlags" line.
-
Re: Help Please simple copy files
I think that Nightwalker meant
Code:
.fFlags = FOF_SIMPLEPROGRESS Or FOF_NOCONFIRMATION Or FOF_FILESONLY Or FOF_SILENT
-
Re: Help Please simple copy files
Quote:
Originally Posted by
Doogle
I think that Nightwalker meant
Code:
.fFlags = FOF_SIMPLEPROGRESS Or FOF_NOCONFIRMATION Or FOF_FILESONLY Or FOF_SILENT
Well, if it can't handle two or more things at once then yes, that is correct.
-
Re: Help Please simple copy files
I was referring to the use of 'Or' rather than 'And' to include a flag.
-
Re: Help Please simple copy files
Quote:
Originally Posted by
Doogle
I was referring to the use of 'Or' rather than 'And' to include a flag.
Yeah, I know but it would depend on what the user wants 'and' would be inclusive while 'or' one or the other.
-
Re: Help Please simple copy files
Quote:
Originally Posted by
Nightwalker83
Yeah, I know but it would depend on what the user wants 'and' would be inclusive while 'or' one or the other.
Not quite.
Code:
FOF_SIMPLEPROGRESS Or FOF_NOCONFIRMATION Or FOF_FILESONLY And FOF_SILENT
equates to
&H100 Or &H80 Or &H10 And &H4 = &H190 ie the &H4 bit is off
whereas
Code:
FOF_SIMPLEPROGRESS Or FOF_NOCONFIRMATION Or FOF_FILESONLY Or FOF_SILENT
equates to
&h100 Or &H80 Or &H10 Or &H4 = &H194 ie the &H4 bit is on.
Remember we're dealing with logical operators, to 'add' to the flags value you have to use 'Or' not 'And'.
-
Re: Help Please simple copy files
k now im confused what part of the module would i edit i am using the code that doogle gave me i just dont understand where to put the silent thing
-
Re: Help Please simple copy files
Quote:
Originally Posted by
Jeremy
k now im confused what part of the module would i edit i am using the code that doogle gave me i just dont understand where to put the silent thing
Exactly where it is in post #6 except where it says 'And' in post #6 put 'Or' like:
vb Code:
FOF_SIMPLEPROGRESS Or FOF_NOCONFIRMATION Or FOF_FILESONLY Or FOF_SILENT
-
Re: Help Please simple copy files
alright heres another question how would i access the appdata
blah = appdata\test
thats wat i wanna do but i really dont know how can any one help me please
-
Re: Help Please simple copy files
You can use this code to retrieve the location of the appdata folder as well as others.
-
Re: Help Please simple copy files
First not trying to highjack the thread but I have a problem with this code and I cant figure out why its doing it. For some reason when I use the code it copies the files/Folders "C:\test\whatever" to "F:\test\whatever" fine but then tries to copy and overwrite them again. I have tried everything I can think of to make this not happen anyone know why its happening?
-
Re: Help Please simple copy files
Quote:
Originally Posted by
jstinnett
First not trying to highjack the thread but I have a problem with this code and I cant figure out why its doing it. For some reason when I use the code it copies the files/Folders "C:\test\whatever" to "F:\test\whatever" fine but then tries to copy and overwrite them again. I have tried everything I can think of to make this not happen anyone know why its happening?
Could please post the exact could you are using so we can see if there anything that needs changing in you code. Thanks!
-
Re: Help Please simple copy files
form
Code:
Option Explicit
Private source As String, destination As String
Private Sub Command1_Click()
source = "C:\squad"
destination = Text1.Text
VBCopyFolder source, destination
End Sub
Private Sub Command2_Click()
source = Text1.Text
destination = "C:\"
VBCopyFolder source, destination
End Sub
Private Sub Command3_Click()
Timer2.Enabled = True
Timer2.Enabled = False
End Sub
Private Sub Command4_Click()
'Text3.Text = FileDateTime("C:\test.txt")
End Sub
Private Sub Form_Load()
End Sub
Private Sub Timer1_Timer()
Text1.Text = Drive1.Drive + Text2.Text
End Sub
Private Sub Timer2_Timer()
Drive1.Refresh
End Sub
Modules
Code:
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 Long ' only used if FOF_SIMPLEPROGRESS, sets dialog title
End Type
Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long
' Available Operations
Private Const FO_COPY = &H2 ' Copy File/Folder
Private Const FO_DELETE = &H3 ' Delete File/Folder
Private Const FO_MOVE = &H1 ' Move File/Folder
Private Const FO_RENAME = &H4 ' Rename File/Folder
' Flags
Private Const FOF_ALLOWUNDO = &H40 ' Allow to undo rename, delete ie sends to recycle bin
Private Const FOF_FILESONLY = &H80 ' Only allow files
Private Const FOF_NOCONFIRMATION = &H10 ' No File Delete or Overwrite Confirmation Dialog
Private Const FOF_SILENT = &H4 ' No copy/move dialog
Private Const FOF_SIMPLEPROGRESS = &H100 ' Does not display file names
Public Sub VBCopyFolder(ByRef strSource As String, ByRef strTarget As String)
Dim op As SHFILEOPSTRUCT
With op
.wFunc = FO_COPY ' Set function
.pTo = strTarget ' Set new path
.pFrom = strSource ' Set current path
.fFlags = FOF_SIMPLEPROGRESS Or FOF_NOCONFIRMATION Or FOF_FILESONLY
End With
' Perform operation
SHFileOperation op
If SHFileOperation(op) = 0 Then
If op.fAnyOperationsAborted = 0 Then
MsgBox "Copy Complete"
Else
MsgBox "Copy Incomplete"
End If
Else
MsgBox "Copy Failed"
End If
End Sub
-
Re: Help Please simple copy files
You're performing the copy twice
Code:
SHFileOperation op
If SHFileOperation(op) = 0 Then
You just need
Code:
If SHFileOperation(op) = 0 Then