Results 1 to 8 of 8

Thread: [RESOLVED] Copy Files using wildcards

  1. #1

    Thread Starter
    Fanatic Member Emcrank's Avatar
    Join Date
    Jan 2009
    Posts
    566

    Resolved [RESOLVED] Copy Files using wildcards

    Ok i want to copy all the files in a directory with an extension e.g .exe or maybe all them files *.* how do i do this?
    I searched on google and found this on MSDN But it doesn't work when i type *. or *.* or *.exe into the textbox and there is files in that folder
    VB.NET Code:
    1. For Each foundFile As String In My.Computer.FileSystem.GetFiles(Application.StartupPath, Microsoft.VisualBasic.FileIO.SearchOption.SearchTopLevelOnly, TextBox8.Text)                    
    2. My.Computer.FileSystem.CopyFile(foundFile, IO.Path.Combine(FullPath, foundFile))
    3. Next

  2. #2
    Hyperactive Member mbutler755's Avatar
    Join Date
    May 2008
    Location
    Peoria, AZ
    Posts
    417

    Re: Copy Files using wildcards

    Set the filter. In the example below, I am searching for TDX files. Then, I can perform whichever operations I want (e.g. copy, delete, rename).

    vb Code:
    1. For Each filetdx As String In IO.Directory.GetFiles("MyDirectory", "*.TDX")
    2.      ' perform your operation here
    3. Next
    Last edited by mbutler755; Jul 24th, 2010 at 03:39 PM. Reason: changed something ;-)
    Regards,

    Matt Butler, MBA, BSIT/SE, MCBP
    Owner, Intense IT, LLC
    Find us on Facebook
    Follow us on Twitter
    Link up on LinkedIn
    mb (at) i2t.us

    CODE BANK SUBMISSIONS: Converting Images to Base64 and Back Again

  3. #3

    Thread Starter
    Fanatic Member Emcrank's Avatar
    Join Date
    Jan 2009
    Posts
    566

    Re: Copy Files using wildcards

    Can you help me on my code, its still not working. In Textbox 8 i am typing *.XML and it still doesn't copy anything. And there is a .XML file in the app startup directory.
    Whats wrong with it?
    VB.NET Code:
    1. For Each foundFile As String In IO.Directory.GetFiles(Application.StartupPath, TextBox8.Text)
    2.                     My.Computer.FileSystem.CopyFile(IO.Path.Combine(Application.StartupPath, foundFile), IO.Path.Combine(Fullpath, foundFile))
    3.                 Next

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Copy Files using wildcards

    This is one of those cases where you don't actually know what your code is doing because you haven't actually checked. You're using data and assuming that it's correct without actually checking whether it's correct. If the code isn't working then something isn't right, so the first thing you should do is check the data. Look at your code:
    Code:
    My.Computer.FileSystem.CopyFile(IO.Path.Combine(Application.StartupPath, foundFile), IO.Path.Combine(Fullpath, foundFile))
    Do you know what those two highlighted parts actually evaluate to? I'll wager not, because they are undoubtedly not producing what you think they are. You could have found that out for yourself in several easy ways, including this:
    vb Code:
    1. MessageBox.Show(IO.Path.Combine(Application.StartupPath, foundFile))
    2. MessageBox.Show(IO.Path.Combine(Fullpath, foundFile))
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Fanatic Member Emcrank's Avatar
    Join Date
    Jan 2009
    Posts
    566

    Re: Copy Files using wildcards

    the fact is that my code wont even get to that part,
    the full section of code to do with it
    vb Code:
    1. If TextBox8.Text.Contains("*") Then
    2.             Try
    3.                 For Each foundFile As String In IO.Directory.GetFiles(Application.StartupPath, TextBox8.Text)
    4.                     MsgBox("App Startup + Textbox" & IO.Path.Combine(Application.StartupPath, TextBox8.Text))
    5.                 Next
    6.             Catch ex As Exception
    7.                 MsgBox("Couldn't pack files from 7", MsgBoxStyle.Critical)
    8.             End Try
    9.         End If

    It just says Couldn't pack files from 7 which is meaning its throwing an exception.

  6. #6

    Thread Starter
    Fanatic Member Emcrank's Avatar
    Join Date
    Jan 2009
    Posts
    566

    Re: Copy Files using wildcards

    Ok i found out that it is finding the files and it is the correct path to be copied, so now cna you help me please.
    vb.net Code:
    1. If TextBox8.Text.Contains("*") Then
    2.             Try
    3.                 For Each foundFile As String In IO.Directory.GetFiles(Application.StartupPath, TextBox8.Text)
    4.                     MsgBox(IO.Path.Combine(Application.StartupPath, foundFile))
    5.                 Next
    6.             Catch ex As Exception
    7.                 MsgBox("Couldn't pack files from 7", MsgBoxStyle.Critical)
    8.             End Try
    9.         End If

    There is 1 .xml file in the app startup directory and it only shows that msgbox once so i know its finding it. Also path it gave was C:\Users\Aaron T\Documents\Visual Studio 2010\Projects\Project Packer\Project Packer\bin\Debug\*.xml
    And from when ive been searching on the web to find out how to do this im pretty sure that you just add the *.xml (filetype) to use wildcards.

  7. #7

    Thread Starter
    Fanatic Member Emcrank's Avatar
    Join Date
    Jan 2009
    Posts
    566

    Re: Copy Files using wildcards

    Ok ive fixed that part, it was because for some reason in the IO.Path.Combine(Fullpath, foundFile) it was giving the full path of foundFile, so i used a little function
    vb.net Code:
    1. For Each foundFile As String In IO.Directory.GetFiles(Application.StartupPath, TextBox8.Text)
    2.                     FileIO.FileSystem.CopyFile(IO.Path.Combine(Application.StartupPath, foundFile), IO.Path.Combine(Fullpath, GetFileNameFromPath(foundFile)))
    3.                 Next
    4.  
    5.  Public Function GetFileNameFromPath(ByVal Path As String) As String
    6.         Try
    7.             Return Path.Substring(Path.LastIndexOf("\") + 1)
    8.         Catch ex As Exception
    9.             Return Path
    10.         End Try
    11.     End Function

    BUT---------------

    its not copying the file for some reason,
    and the path given from this:
    Code:
    MsgBox(IO.Path.Combine(Fullpath, GetFileNameFromPath(foundFile)))
    is:
    Code:
    C:\Users\Aaron T\AppData\Roaming\Project_Packer\Project Packer\1.0.0.0\Packed Files - 234h1\Project Packer.xml

  8. #8

    Thread Starter
    Fanatic Member Emcrank's Avatar
    Join Date
    Jan 2009
    Posts
    566

    Re: Copy Files using wildcards

    Fixed it,
    Full code:
    VB.NET Code:
    1. For Each foundFile As String In IO.Directory.GetFiles(Application.StartupPath, TextBox8.Text)
    2.                     FileIO.FileSystem.CopyFile(IO.Path.Combine(Application.StartupPath, GetFileNameFromPath(foundFile)), IO.Path.Combine(Fullpath, GetFileNameFromPath(foundFile)))
    3.                 Next

    FOR FUTURE REFERENCE:
    Code Snippet

    Code:
    For Each foundFile As String In IO.Directory.GetFiles("DIRECTORY", "WILDCARD")
                        FileIO.FileSystem.CopyFile(IO.Path.Combine("PATH-TO-COPY-FROM", GetFileNameFromPath(foundFile)), IO.Path.Combine("PATH-TO-COPY-TO", GetFileNameFromPath(foundFile)))
                    Next

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