Results 1 to 6 of 6

Thread: [2005] Copy all files of a specific type

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    9

    [2005] Copy all files of a specific type

    Hi.
    Im learning vb.net 2005 and i am trying to create a backup program for myself

    I would like to beable to copy all files on the local hard drives that have the *.txt and *.doc file extensions.

    I have sucessfully managed to use teh getfiles method to find the files but i cant figure out how to copy them to a usb drive (E:\)

    Can you help?

  2. #2
    Lively Member
    Join Date
    Oct 2004
    Posts
    66

    Re: [2005] Copy all files of a specific type

    Quote Originally Posted by murrayf1
    Hi.
    Im learning vb.net 2005 and i am trying to create a backup program for myself

    I would like to beable to copy all files on the local hard drives that have the *.txt and *.doc file extensions.

    I have sucessfully managed to use teh getfiles method to find the files but i cant figure out how to copy them to a usb drive (E:\)

    Can you help?
    you can use the file.copy

    something like this

    VB Code:
    1. Dim file As String
    2. Dim foundfiles As String() 'This is an array containing all filenames, you need to fill this first
    3.  
    4. For Each file In foundFiles
    5.     File.copy(file, "e:/")
    6. Next

    you need to figure out is if getFiles method also get the complete directory path, or only the filename.

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    9

    Re: [2005] Copy all files of a specific type

    Dim foundfiles As ReadOnlyCollection(Of String)
    foundfiles = My.Computer.FileSystem.GetFiles("C:\", True, "*.txt")
    Thats the code im using to get the files for txt

    I cant seem to get the Dim foundfiles As String() to work because foundfiles is already declared but if i take it out i get the following errors

    Too many arguments to 'Public Shared Function Copy(str As String) As String'.

    Access of shared member, constant member, enum member or nested type through an instance; qualifying expression will not be evaluated.

  4. #4
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    Re: [2005] Copy all files of a specific type

    I think you're going to run into a problem when using that method for C:\ because eventually it will hit System Volume Information and throw an UnauthorizedAccessException. However, if that's not the case, a simple for each loop should still work, you just have to keep in mind what you're doing:
    VB Code:
    1. For Each str As String In foundfiles
    2.             IO.File.Copy(str, "E:\" & str.Substring(str.LastIndexOf("\") + 1))
    3. Next

    The second argument in File.Copy has to be the path including the filename. not just where to dump it off. Intellisense will tell you this as you type the second portion.

    You may have to use a recursive search if you want to start out in C:\ that is enclosed in a try / catch block to catch the System Volume Information's UnauthorizedAccessException..

    Bill
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    9

    Re: [2005] Copy all files of a specific type

    AHH Bangs head aganst a wall!
    System.ComponentModel.InvalidEnumArgumentException: The value of argument 'searchType' (-1) is invalid for Enum type 'SearchOption'.

    Any ideas?
    Code is:
    Dim foundfiles As ReadOnlyCollection(Of String)
    foundfiles = My.Computer.FileSystem.GetFiles("C:\", True, "*.ptf")

    For Each str As String In foundfiles
    IO.File.Copy(str, "F:\" & str.Substring(str.LastIndexOf("\") + 1))

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,487

    Re: [2005] Copy all files of a specific type

    You relly need to read what Intellisense is telling you as you're typing. There is no overload that takes a Boolean as the second argument. There are only two valid values and Intellisense tells you what they are. Also, if you're getting every PTF file on C: and copying them to one folder then you have no guarantee that you won't be trying to create two files with the same path. Without error checking for that, here's basically what you want:
    VB Code:
    1. 'With 'My'
    2.         For Each filePath As String In My.Computer.FileSystem.GetFiles("C:\", _
    3.                                                                        FileIO.SearchOption.SearchAllSubDirectories, _
    4.                                                                        "*.ptf")
    5.             My.Computer.FileSystem.CopyFile(filePath, _
    6.                                             My.Computer.FileSystem.CombinePath("F:", _
    7.                                                                                My.Computer.FileSystem.GetName(filePath)), _
    8.                                             True)
    9.  
    10.         Next filePath
    11.  
    12.         'Without 'My'
    13.         For Each filePath As String In IO.Directory.GetFiles("C:\", _
    14.                                                              "*.ptf", _
    15.                                                              IO.SearchOption.AllDirectories)
    16.             IO.File.Copy(filePath, _
    17.                          "F:\" & IO.Path.GetFileName(filePath), _
    18.                          True)
    19.  
    20.         Next filePath

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