Results 1 to 30 of 30

Thread: Help Please simple copy files

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 1999
    Posts
    28

    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

  2. #2

    Thread Starter
    Junior Member
    Join Date
    Jun 1999
    Posts
    28

    Re: Help Please simple copy files

    btw using vb 6.0

  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Help Please simple copy files

    you could use vb filecopy
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

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

    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.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jun 1999
    Posts
    28

    Re: Help Please simple copy files

    is there another way to copy files if so can you supply code

  6. #6
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Help Please simple copy files

    As Westconn1 said you can use filecopy for a simple copy method:

    vb Code:
    1. Private Sub Command1_Click()
    2. FileCopy "c:\.rnd", "g:\.rnd"
    3. End Sub

    or a slightly more advanced method which copies the entire contents of a folder:

    vb Code:
    1. Option Explicit
    2. Private source As String, destination As String
    3. Private Sub Command1_Click()
    4. source ="c:\New Folder\"
    5. destination = "d:\New Folder\"
    6. VBCopyFolder(source, destination)
    7. End Sub

    Place this in a module:
    vb Code:
    1. Private Type SHFILEOPSTRUCT
    2.     hWnd As Long
    3.     wFunc As Long
    4.     pFrom As String
    5.     pTo As String
    6.     fFlags As Integer
    7.     fAnyOperationsAborted As Long
    8.     hNameMappings As Long
    9.     lpszProgressTitle As Long ' only used if FOF_SIMPLEPROGRESS, sets dialog title
    10. End Type
    11. Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long
    12. ' Available Operations
    13. Private Const FO_COPY = &H2 ' Copy File/Folder
    14. Private Const FO_DELETE = &H3  ' Delete File/Folder
    15. Private Const FO_MOVE = &H1  ' Move File/Folder
    16. Private Const FO_RENAME = &H4  ' Rename File/Folder
    17. ' Flags
    18. Private Const FOF_ALLOWUNDO = &H40  ' Allow to undo rename, delete ie sends to recycle bin
    19. Private Const FOF_FILESONLY = &H80  ' Only allow files
    20. Private Const FOF_NOCONFIRMATION = &H10  ' No File Delete or Overwrite Confirmation Dialog
    21. Private Const FOF_SILENT = &H4  ' No copy/move dialog
    22. Private Const FOF_SIMPLEPROGRESS = &H100  ' Does not display file names
    23.  
    24. Public Sub VBCopyFolder(ByRef strSource As String, ByRef strTarget As String)
    25.  
    26.     Dim op As SHFILEOPSTRUCT
    27.     With op
    28.         .wFunc = FO_COPY ' Set function
    29.         .pTo = strTarget  ' Set new path
    30.         .pFrom = strSource ' Set current path
    31.         .fFlags = FOF_SIMPLEPROGRESS Or FOF_NOCONFIRMATION And FOF_FILESONLY
    32.     End With
    33.     ' Perform operation
    34.     SHFileOperation op
    35. End Sub
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Jun 1999
    Posts
    28

    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

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Jun 1999
    Posts
    28

    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

  9. #9
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Help Please simple copy files

    Quote Originally Posted by Jeremy View Post
    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?
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Jun 1999
    Posts
    28

    Re: Help Please simple copy files

    Compile Error:

    Varible Required - Can't Assdign to This Expression

    its also highlighting op


    hopefully this helps

  11. #11
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    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.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  12. #12
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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)
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  13. #13
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    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
    Last edited by Doogle; Nov 10th, 2011 at 08:49 AM.

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Jun 1999
    Posts
    28

    Re: Help Please simple copy files

    is there a way that i can force a overwrite when im coping the folder :/

  15. #15
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Help Please simple copy files

    Add this to the flag:

    vb Code:
    1. FOF_SILENT
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  16. #16

    Thread Starter
    Junior Member
    Join Date
    Jun 1999
    Posts
    28

    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

  17. #17
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Help Please simple copy files

    vb Code:
    1. Dim op As SHFILEOPSTRUCT
    2.     With op
    3.         .wFunc = FO_COPY ' Set function
    4.         .pTo = strTarget  ' Set new path
    5.         .pFrom = strSource ' Set current path
    6.         .fFlags = FOF_SIMPLEPROGRESS Or FOF_NOCONFIRMATION Or FOF_FILESONLY AND FOF_SILENT
    7.     End With

    See the ".fFlags" line.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  18. #18
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Help Please simple copy files

    I think that Nightwalker meant
    Code:
            .fFlags = FOF_SIMPLEPROGRESS Or FOF_NOCONFIRMATION Or FOF_FILESONLY Or FOF_SILENT

  19. #19
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Help Please simple copy files

    Quote Originally Posted by Doogle View Post
    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.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  20. #20
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Help Please simple copy files

    I was referring to the use of 'Or' rather than 'And' to include a flag.

  21. #21
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Help Please simple copy files

    Quote Originally Posted by Doogle View Post
    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.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  22. #22
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Help Please simple copy files

    Quote Originally Posted by Nightwalker83 View Post
    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'.
    Last edited by Doogle; Nov 11th, 2011 at 02:24 AM. Reason: Correected additions

  23. #23

    Thread Starter
    Junior Member
    Join Date
    Jun 1999
    Posts
    28

    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

  24. #24
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Help Please simple copy files

    Quote Originally Posted by Jeremy View Post
    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:
    1. FOF_SIMPLEPROGRESS Or FOF_NOCONFIRMATION Or FOF_FILESONLY Or FOF_SILENT
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  25. #25

    Thread Starter
    Junior Member
    Join Date
    Jun 1999
    Posts
    28

    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

  26. #26
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Help Please simple copy files

    You can use this code to retrieve the location of the appdata folder as well as others.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  27. #27
    Junior Member
    Join Date
    Nov 2011
    Posts
    19

    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?

  28. #28
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Help Please simple copy files

    Quote Originally Posted by jstinnett View Post
    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!
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  29. #29
    Junior Member
    Join Date
    Nov 2011
    Posts
    19

    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

  30. #30
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    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

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