Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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 I'm 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. I'm 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 I'll be able to copy/move them...

 

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

 

Thanks!

  • Administrators
Posted

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 it's own)

 

       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

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

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! Haven't tried it yet, but just wanted to say, "Thanks for responding so quickly and with such an eloquent solution."

 

Cheers! ;)

 

 

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 it's own)

 

       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

Posted

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? Isn't the return 'file' variable of type scripting, system.io???

 

Thanks!

  • Administrators
Posted

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

 

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

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted
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 didn't 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!
  • Administrators
Posted
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).

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...