Results 1 to 19 of 19

Thread: [RESOLVED] USB drive backup program

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Posts
    146

    Resolved [RESOLVED] USB drive backup program

    I'm trying to write a program that backs up USB drives.

    The program should copy all the files/folders on the USB drive to my harddisk. The most important part is that, the program should still let other processes run while copying the files..



    I'm totally blur on this.
    Can you please enlighten me





    Questions
    1. How do I copy files/folders (including subfolders)
    2. How do I copy files/folders while still letting other processes run smoothly as usual. (How long it takes to copy is not important)
    Last edited by winterslam; Jan 10th, 2008 at 10:08 AM.

  2. #2
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: USB drive backup program

    1. Usb drives register with the system as any other harddrive. So copying to and from usb drives is just the same as you would do from any other drive.
    2. Call DoEvents in your code to allow the system to run other processes. e.g. Do copying in loop and call DoEvents after one file/folder is copied.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Posts
    146

    Re: USB drive backup program

    How do I go about copying the contents of the drive?


    Do I have to use FileSystemObject's CopyFolder method?

  4. #4
    Hyperactive Member Davadvice's Avatar
    Join Date
    Apr 2007
    Location
    Glasgow (Scotland)
    Posts
    440

    Re: USB drive backup program

    !!!!
    Last edited by Davadvice; Jan 15th, 2008 at 04:41 AM.
    This is Blank

  5. #5
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: USB drive backup program

    There are many ways. But what you are thinking seems to be good enough.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

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

    Re: USB drive backup program

    This might help in copying folders.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Posts
    146

    Re: USB drive backup program

    Thanks for the replies


    The program will be copying several Gigs. The most important part is that the program should give some breathing time for other processes to run simultaneously.

    It doesn't matter if it takes a few hours to copy the entire drive byte by byte.. slowly..

    It is at least better than copying in 10mins while stopping all the other processes.


    I have never tried FileSystemObject though.
    Can it handle that?

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Posts
    146

    Re: USB drive backup program

    I've just found a similar project on planetsourcecode.com

    http://www.planetsourcecode.com/vb/s...00810433943032


    The only problem is that I can't seem to run other processes smoothly while copying.

  9. #9
    Hyperactive Member Davadvice's Avatar
    Join Date
    Apr 2007
    Location
    Glasgow (Scotland)
    Posts
    440

    Re: USB drive backup program

    !!!!
    Last edited by Davadvice; Jan 15th, 2008 at 04:41 AM.
    This is Blank

  10. #10
    Frenzied Member
    Join Date
    Sep 2006
    Location
    Scotland
    Posts
    1,054

    Re: USB drive backup program

    I doubt it will be more than 4 GB... If I remember right (although i often don't) then 4GB is the largest file size you can have on a FAT formatted disk. Not sure how it works in this case though.

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Posts
    146

    Re: USB drive backup program

    Quote Originally Posted by Davadvice
    what is the maximum size of an individule file?

    you may need to itterate through the folders and recreate the directory structure on the HDD then itterate throgh each file and use Doevents after a certain amount of files have been copied to the HDD.

    you have to bear in mind that the FSO is just recreating the copy and paste of explorer (i think)
    The files will mostly be mp3s and documents.. so roughly around 4mb on average..


    How do I go about making it iterate through each folder and sub-folder as you mentioned?

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Posts
    146

    Re: USB drive backup program

    I'm almost there..


    The program iterates each folder/subfolder and copies file by file.



    Should I use DoEvents or Sleep function to give breathing time for other processes?


    Code:
    Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    Sleep 2000 ' sleep 2 seconds for every 5 files
    Last edited by winterslam; Jan 9th, 2008 at 10:37 PM.

  13. #13
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    Re: USB drive backup program

    Quote Originally Posted by 03myersd
    I doubt it will be more than 4 GB... If I remember right (although i often don't) then 4GB is the largest file size you can have on a FAT formatted disk. Not sure how it works in this case though.
    you don't remember correctly. 2GB is the largest size a FAT16 partition can be in 3.1, 95, 98, 2k up to sp2(?). 4GB is the limit for a FAT16 partition from then on. HOWEVER, the fact is that you aren't limited to using FAT on a removable drive. I have NTFS on mine. 2K can format removable media as ntfs. XP can only format them as fat16 and fat32, but you can then run the command-line convert.exe program on them, which is used to convert filesystems to NTFS.
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Posts
    146

    Re: USB drive backup program

    This is my code

    Code:
    Option Explicit
    Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    Dim fso As New FileSystemObject
    
    
    Public Sub FolderCopy(ByVal source As String, ByVal destination As String)
              Dim objfile As File
              Dim objfolder As Folder       
                     
                     
              DoEvents
                  
              For Each objfile In fso.GetFolder(source).Files
                      DoEvents
                      
                      On Error Resume Next
                      If objfile.Name <> "" Then
                            objfile.Copy destination + "\" + objfile.Name, True
                            Sleep 100
                      End If
                     
    
             Next
             For Each objfolder In fso.GetFolder(source).SubFolders
                            DoEvents
                            On Error Resume Next
                            If Dir(destination + "\" + objfolder.Name) = "" Then MkDir destination + "\" + objfolder.Name
                            FolderCopy objfolder.Path, destination + "\" + objfolder.Name
        
             Next      
          
    
    End Sub
    
         
    Private Sub Command1_Click()
    FolderCopy "E:", "C:\Rescued"
    
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
     Set fso = Nothing
    End Sub
    What exactly does the 'sleep' command do?
    Does it pause my program for some milliseconds while allowing other processes to run smoothly?


    But I got great results from this code.


    The last step is to get the drive letters of the inserted removable drives.
    Last edited by winterslam; Jan 10th, 2008 at 12:11 AM.

  15. #15
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: USB drive backup program

    Quote Originally Posted by winterslam
    This is my code

    ...

    What exactly does the 'sleep' command do?
    Does it pause my program for some milliseconds while allowing other processes to run smoothly?


    But I got great results from this code.


    The last step is to get the drive letters of the inserted removable drives.
    The sleep command pauses your application for specified time interval. (May not necessarily give time to other processes to run)
    The DoEvents command iteriates through the system process queue and executes them one by one. So ensures that all other processes are running simultaneously.

    I think in this situation you should use DoEvents because
    1. you don't want your application to sleep even if there are no other processes to run.
    2. DoEvents ensures that the queue is cleared once before it gives time to your application again.
    3. You don't mind your task taking even 1 hour instead of 10 minutes as long as it gives time to other processes on the system. i.e. Processing time is irrelevent.

    Pradeep
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  16. #16
    Hyperactive Member Davadvice's Avatar
    Join Date
    Apr 2007
    Location
    Glasgow (Scotland)
    Posts
    440

    Re: USB drive backup program

    !!!
    !!!
    Last edited by Davadvice; Jan 15th, 2008 at 04:41 AM.
    This is Blank

  17. #17
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    Re: [RESOLVED] USB drive backup program

    On Topic: This API call will help you determine if it's a removable drive or not:
    Code:
    Declare Function GetDriveType& Lib "kernel32" Alias "GetDriveTypeA" (ByVal _
    nDrive As String)
    msgbox getdrivetype("E:\")
    Removable drives have their own return code, although you wouldn't be able to tell a jump drive from a usb hard drive.

    Completely off topic, but responding to Davadvice's off-topic:
    I totally disagree with that. I find it hard to read when you use an ampersand to concantenate. An ampersand is used in dozens of different ways and this is only one of them. Implicitly declaring a variable as long, listing a number as hexidecimal, etc..
    Code:
    dim a as long, b as long, c as long, d as long, e as long, f as long, g as long, h as long ...
    shortens to:
    dim a&, b&, c&, d&, e&, f&, g&, h&... and actually fits on one line.
    
    It also forces variables to be a Long
     1& + 2&. is faster than 1 + 2 since the values are considered longs.
    The only good reason to use it on a string is concantenating a number onto a string. However, if you manage to somehow mis-declare your variable as a string, you won't get an errror! Plus you will end up using it in the wrong place (adding numeric values) and it will give you the wrong result but not give you an error!
    a = 1, b = 2
    n = a + b 'n = 3
    n = a & b' n = 12

    the "+" on the other hand can add numbers or strings, but it actually lets you know if you mix them together by generating an error.

    But since we aren't allowed to use mathematical operators on strings, what would you suggest we replace the equals sign with? Or the greater/less than? yes, they work on strings too!.
    msgbox "a" > "b" will return false
    msgbox "a" < "b" will return true
    In short, learn to read code written by professional programmers, not shortcut users.
    Last edited by Lord Orwell; Jan 10th, 2008 at 12:33 PM.
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  18. #18
    Hyperactive Member Davadvice's Avatar
    Join Date
    Apr 2007
    Location
    Glasgow (Scotland)
    Posts
    440

    Re: [RESOLVED] USB drive backup program

    this total post is a waste. as the mod edited it.

    cheers PEN!!

    Personal comments not allowed for certain users but ok for others !!
    Last edited by Davadvice; Jan 15th, 2008 at 04:40 AM.
    This is Blank

  19. #19
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    Re: [RESOLVED] USB drive backup program

    um, if you were to enclose them in quotes, making them a string, they would be concantenated when using the "+" symbol. I was pointing out that if you make a coding error somewhere else the & symbol will not catch it where a + will. The & converts everything to strings and the + will let you know if one of them isn't a string in the first place. And i personally have 24 years in basic programming. And visual basic is the only one that lets you use the & symbol. I don't see where you said i was being condenscending. I was pointing out that some of us who have been programming for years (but FYI i have only been earning ratings on my posts for the last 6 months because the ratings thing is very new and i was gone for a while but i must be doing SOMETHING right because i was sent a free vbforums button-down shirt that came in the mail today) But i don't expect you to understand that because you gave me a negative rating for disagreeing with you. And you aren't even the thread starter. I explained in detail why plus should be used, and you merely stated "&" should be used did not give a reason besides "i was told it was better". Personal attacks on me and my marriage are not considered good form and are in fact grounds for banning. Please keep this in mind in the future.

    I do admit the final line was a little harsh, but you will probably get worse treatment from other members of the forum than one line of text telling you to work on your skills as a code-reader. You recommend a course they don't agree with, and you will find your eyeballs bleeding... Yeah i am a "regular" and i don't consider myself to be professional. Professionals have nothing to do with time in programming. It has everything to do with whether or not you are paid for it, which i am not.

    And i didn't even list the best part: + is over four times faster than & at putting strings together (at least on my system) probably due to the fact it converts everything to a string, even things that are strings already.(my system for comparison basis: xp64 multi-core running 64-bit windows xp)
    Code:
    Private Sub Form_Load()
    
    a = Timer
    For cl = 1 To 1000000
    jl = x + x + x + x + x + x + x
    Next cl
    MsgBox " + is " & Timer - a
    a = Timer
    For cl = 1 To 1000000
    jl = x & x & x & x & x & x & x
    Next cl
    MsgBox " & is " & Timer - a
    End Sub
    Last edited by Lord Orwell; Jan 11th, 2008 at 10:07 PM.
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

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