Results 1 to 34 of 34

Thread: completely silent file copy

  1. #1
    nzer
    Guest

    completely silent file copy

    Hi,

    I'm making a backup program and it will be copying over the same file each day so i want to get rid of the "Folder already exsists..overwrite?" prompts

    I'm using this as my code

    Code:
    Private Declare Function SHFileOperation _
        Lib "shell32.dll" Alias "SHFileOperationA" _
        (lpFileOp As SHFILEOPSTRUCT) As Long
    
    Private Type SHFILEOPSTRUCT
       hwnd        As Long
       wFunc       As Long
       pFrom       As String
       pTo         As String
       fFlags      As Integer
       fAborted    As Boolean
       hNameMaps   As Long
       sProgress   As String
     End Type
    
    Private Const FO_MOVE = &H1
    Private Const FO_COPY = &H2
    Private Const FO_DELETE = &H3
    Private Const FO_RENAME = &H4
    Private Const FOF_SILENT = &H4
    and the actual copy bit

    Code:
    Dim SHF As SHFILEOPSTRUCT
            Dim lret As Long
            ArrayCount = 1
            Do While ArrayCount <= ArrayLimit
            
            SHF.wFunc = FO_COPY
            SHF.hwnd = Me.hwnd
            SHF.pFrom = Backup(ArrayCount)
            SHF.pTo = SaveLocation
            SHF.fFlags = FOF_SILENT
    
            lret = SHFileOperation(SHF)
            ArrayCount = ArrayCount + 1
            Loop
    now i thought with the flags set to FOF_SILENT it would not prompt or anything?

    Thanks in advance for help!

  2. #2
    PowerPoster MidgetsBro's Avatar
    Join Date
    Oct 2000
    Location
    Apparently, Internet.com
    Posts
    3,125
    Why can't you just use the FileCopy function built into VB?
    VB Code:
    1. FileCopy "C:\test", "D:\test"
    <removed by admin>

  3. #3
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    Can u trap the Error by using:
    On Error Resume Next

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    You can use the filecopy command instead.

    do you HAVE to use the method you described?

  5. #5
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Originally posted by MidgetsBro
    Why can't you just use the FileCopy function built into VB?
    VB Code:
    1. FileCopy "C:\test", "D:\test"
    AAAAARGH!!!!

  6. #6
    PowerPoster MidgetsBro's Avatar
    Join Date
    Oct 2000
    Location
    Apparently, Internet.com
    Posts
    3,125
    Originally posted by mendhak


    AAAAARGH!!!!
    LOL! Quick Draw McJew over here
    <removed by admin>

  7. #7
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    I think the problem is eliminating the win Dialogs....

  8. #8
    PowerPoster MidgetsBro's Avatar
    Join Date
    Oct 2000
    Location
    Apparently, Internet.com
    Posts
    3,125
    Do the Windows dialogs popup when you use FileCopy?
    -------------------
    I just tried it and I didn't get a copy dialg when I used:
    VB Code:
    1. FileCopy "C:\snt2.jpg", "C:\cheap.jpg"
    <removed by admin>

  9. #9
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    No.... (just tested it)

  10. #10
    nzer
    Guest
    yes and using the API

  11. #11
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    However, u get a 'permission' denied when coping from loc to same loc.

    ie.
    VB Code:
    1. Private Sub Form_Load()
    2. FileCopy "C:\test.txt", "C:\test.txt"
    3. End Sub

  12. #12
    PowerPoster MidgetsBro's Avatar
    Join Date
    Oct 2000
    Location
    Apparently, Internet.com
    Posts
    3,125
    Why in the world would you want to copy the same file to the same place? That makes no logical sense.
    <removed by admin>

  13. #13
    nzer
    Guest
    and yes I want to use that method..

    using On Error Resume Next will kill all my error handling won't it?

  14. #14
    nzer
    Guest

    Why in the world would you want to copy the same file to the same place? That makes no logical sense.
    so i have the latest versions of the files all the time, its the easiest way of doing it

  15. #15
    PowerPoster MidgetsBro's Avatar
    Join Date
    Oct 2000
    Location
    Apparently, Internet.com
    Posts
    3,125
    Yes, it will kill all error handling in that sub. You can have it check for specific error numbers though, instead On Error Resume Next, you could have
    VB Code:
    1. Private Sub Stuff()
    2. On Error Go To EL
    3. 'your stuff here
    4. Exit Sub
    5. EL:
    6. If Err.Number = 9485 Then 'the number of error you want to trap
    7.     Resume Next
    8. End If
    9. End Sub
    <removed by admin>

  16. #16
    PowerPoster MidgetsBro's Avatar
    Join Date
    Oct 2000
    Location
    Apparently, Internet.com
    Posts
    3,125
    Originally posted by nzer


    so i have the latest versions of the files all the time, its the easiest way of doing it
    But if you copy the same file to the same place, you aren't updating anything. He was saying copy the same file over itself. Think of it this way. Try to take a cup, and put it over itself... see what I'm getting at? Maybe if you copied from like C:\CompileFolder to C:\Program Folder then it would work.
    <removed by admin>

  17. #17
    nzer
    Guest

    Thanks, but..

    On Error Resume Next

    doesn't get rid of the window that pops up complaining the folder exsists.

    heh can it be done ?

  18. #18
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    Alternatively, can u open the file, make any changes, then save it back out?

  19. #19
    PowerPoster MidgetsBro's Avatar
    Join Date
    Oct 2000
    Location
    Apparently, Internet.com
    Posts
    3,125
    But what I am saying, if you copy a file called C:\file.dat to C:\file.dat, you are copying the same thing to the same place. You did not do anything to update the file, so it is like cloning the file, yet you aren't because you can't put something in the same place that it already is without moving the old file first, but then you would have a copy error because the source would be gone. It's like a paradox sort of thing, it's late, and I only had an hour and 45 min of sleep, so either I'm not thinking straight, or I'm not explaining it right.
    <removed by admin>

  20. #20
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    Valid point MidgetsBro

  21. #21
    nzer
    Guest
    heh no you are thinking correctly!

    but SAY my program is scheduled to backup every night at 1.00 am.. say the user hasn't changed any files but my backup program still runs, i still want it to be able to copy files (even if they are the same) without it stopping with a question asking if i can overwrite them .. understand?

    and it isn't copying like C:\file.file to C:\ file.file it would be like C:\Accounts to //Server/Backup/Accounts

  22. #22
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    Now I'm completely lost

    Anyway... This didn't give me ANY errors (although the file existed)
    VB Code:
    1. Private Sub Form_Load()
    2.     FileCopy "C:\test.txt", "C:\New\test.txt"
    3. End Sub

  23. #23
    Fanatic Member Vanguard-MnC's Avatar
    Join Date
    Apr 2002
    Location
    Inactive for like ever.
    Posts
    628
    hey midgetsbro! did you make the haztek site?

  24. #24
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Originally posted by nzer
    heh no you are thinking correctly!

    but SAY my program is scheduled to backup every night at 1.00 am.. say the user hasn't changed any files but my backup program still runs, i still want it to be able to copy files (even if they are the same) without it stopping with a question asking if i can overwrite them .. understand?

    and it isn't copying like C:\file.file to C:\ file.file it would be like C:\Accounts to //Server/Backup/Accounts
    I'm lost too.

    But in THIS case you won't get an error.

  25. #25
    nzer
    Guest
    okay maybe i'm crazy lol
    gimme 10 mins to try FileCopy

  26. #26
    nzer
    Guest
    FileCopy isnt' letting me copy the whole directory

    i'm using

    Code:
    Do While ArrayCount <= ArrayLimit
        FileCopy Backup(ArrayCount), SaveLocation
        ArrayCount = ArrayCount + 1
    Loop
    where Backup(1) for example may = E:\Comp
    and SaveLocation could = C:\Temp

  27. #27
    nzer
    Guest

    okay..

    I think whats causing the trouble is that i'm copying whole DIRS and not just individual files.. SO now you can see why I might want to copy the same directory over the same directory in a different place (in case the files inside are different)... so really my problem is when i try and copy it trys to create the directory and since its there it asks if its okay to overwrite... so how do i stop that popping up

    make sense now? (lol this is getting funny)

  28. #28
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497
    You know that you have RENAME and SILENT as both &H4 right?

    I think this may be the source of your errors.

    Should it be &H5 or even &H6?

  29. #29
    nzer
    Guest

    You know that you have RENAME and SILENT as both &H4 right?


    hmm just noticed that.. but i looked in the API viewer thing and it says

    Public Const FOF_SILENT = &H4 ' don't create progress/report


    and

    Public Const FO_RENAME = &H4

    .. argggg....

  30. #30
    nzer
    Guest

    had a hunt around...

    FOF_NOCONFIRMATION

    does the trick :-)

    thanks for all the input

  31. #31
    PowerPoster MidgetsBro's Avatar
    Join Date
    Oct 2000
    Location
    Apparently, Internet.com
    Posts
    3,125
    Originally posted by Vanguard-MnC
    hey midgetsbro! did you make the haztek site?
    Yes
    <removed by admin>

  32. #32
    Frenzied Member nishantp's Avatar
    Join Date
    Jan 2001
    Location
    Where you least expect me to be
    Posts
    1,375
    BTW guys, the CopyFile API is very simple to use, and it give you a few more options.

    Anyone know if its any faster?
    You just proved that sig advertisements work.

  33. #33
    Fanatic Member Vanguard-MnC's Avatar
    Join Date
    Apr 2002
    Location
    Inactive for like ever.
    Posts
    628
    this post still exists???? p.s. i dont know anything about api so dont ask me

  34. #34
    Hyperactive Member DovyWeiss's Avatar
    Join Date
    Feb 2002
    Location
    Miami Beach, FL
    Posts
    366
    Though your new Const is fine, without that, I would have said: why can't you use use the DOS copy command? it has a "/y" switch to stop over-write-confirm.
    VB Code:
    1. 'use any method to find the windows \command directory, API is useful for this
    2. Shell windowsCommandDir & "\copy.exe C:\myDir\*.* _
    3.   C:\Backup\ /y", vbHide
    Then again, i'm not sure, maybe you'd have to map the net drive! (because of DOS)

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