How to copy/move files of certain extensions from one directory to another?

jsutterf

New member
Joined
May 25, 2004
Messages
4
Location
Pensacola, FL
I am almost finished a small project and all that is left is figuring out how to move/copy files of certain extensions (.mov, .jpeg, etc) from Directory1 to Directory2... I have both directories, and the file extensions prepped already, but just need to know how I go about grabbing the appropriate files and moving/copying them to the new directory.

What Im looking for is how to iterate through all files in a directory, store them in a collection, and then copy/move them to the destination folder. Im looking at creating a FileSystemObject, a File object, and a Collection object, then creating a loop where I can iterate through all files in the folder, select the ones I want, and then store them in my collection where Ill be able to copy/move them...

Am I on the right track?? Any help/code snippets will be greatly appreciated...

Thanks!
 
some quick code that will find and copy all .bmp files from the windows folder - should give you the idea of what you need. you would just have to call this code passing in differrent wildcards for all the file types. (could probably turn it into a sub of its own)

Code:
        Dim files() As String
        files = System.IO.Directory.GetFiles("C:\windows", "*.bmp")

        For Each file As String In files
            System.IO.File.Copy(file, "c:\destination\" & System.IO.Path.GetFileName(file))
        Next
    End Sub
 
You rock!

As I lay down to sleep last night, I mumbled to my wife "you know, directories probably know about which files they contain", to which she said "What are you talking about!" ha ha... This is perfect! Havent tried it yet, but just wanted to say, "Thanks for responding so quickly and with such an eloquent solution."

Cheers! ;)


PlausiblyDamp said:
some quick code that will find and copy all .bmp files from the windows folder - should give you the idea of what you need. you would just have to call this code passing in differrent wildcards for all the file types. (could probably turn it into a sub of its own)

Code:
        Dim files() As String
        files = System.IO.Directory.GetFiles("C:\windows", "*.bmp")

        For Each file As String In files
            System.IO.File.Copy(file, "c:\destination\" & System.IO.Path.GetFileName(file))
        Next
    End Sub
 
the As String in the For Each file As String In files is giving me a syntax error... Can we declare a variable on the fly in a loop like this? Isnt the return file variable of type scripting, system.io???

Thanks!
 
What version of visual studio are you using? The above syntax only works with Vs 2003 - try the following change and see if that helps

Code:
Dim files() As String

        files = System.IO.Directory.GetFiles("C:\windows", "*.bmp")


         Dim file as string
        For Each file In files

            System.IO.File.Copy(file, "c:\destination\" & System.IO.Path.GetFileName(file))

        Next
 
Oh NO!! I just did something very bad, I think... I was in my form.vb looking at the code and was thinking that I had a reference that I didnt need (from Project/Add Reference menu) so I went to that menu and selected Exclude From Project)!!! Now, I have no idea what I excluded and cannot find my windows form (GUI layout)... when I compile I get the error "Sub Main was not found in windows application!!! What have I done? What can I do??? Please help!
 
On the top of the solution explorer there is a yellow-ish button (tooltip is Show all files), click this and the excluded file (something.vb) will appear but be greyed out. right click it and select include in project (or something similar).
 
Back
Top