Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
How i can copy a file to some subfolders?
Posted

1 solution

Exactly the same way as the solution to vb.net how to copy file from one Directory to another directory by create the folder if that folder is not exists[^] where you posted this question as a solution

[EDIT - after receiving further information from OP]
This code snippet will list all of the sub-folders of C:\Develop (substitute whichever folder you need for that)
VB
Dim dr As String() = System.IO.Directory.GetDirectories("C:\Develop")
For Each s As String In dr
    Debug.Print(s) 'Replace this bit with your file copy
Next


[EDIT #2 - How to find/skip folders that contain a particular type of file]
This is essentially my entire test form
Imports System.IO
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim dr As String() = Directory.GetDirectories("d:\Gry\OMSI 2\Vehicles")
        Dim filter As String = "*.BUS"    'case CAPS or lower doesn't matter
        For Each s As String In dr
            Dim di As New DirectoryInfo(s)          'Get information about the sub-folder
            Dim fi As FileInfo() = di.GetFiles(filter) 'Look for the files you want consider
            If fi.GetUpperBound(0) >= 0 Then
                Debug.Print("Found folder " + s + " containing file like " + filter)
            End If
        Next
    End Sub
End Class

NB - If your sub-folders have sub-folders of their own, and you do/don't want to look in those sub-folders for *.bus then you can use
Dim fi As FileInfo() = di.GetFiles(filter, SearchOption.TopDirectoryOnly)
and
Dim fi As FileInfo() = di.GetFiles(filter, SearchOption.AllDirectories)
to specify exactly what you want.
 
Share this answer
 
v3
Comments
TehiroGoo 12-Apr-14 7:59am    
I going to copy a file
example:
I have a file 1.exe
I going to copy this file to all folders in folder
CHill60 12-Apr-14 8:27am    
I guess you are asking "How do I identify all of the sub-folders of a folder" - I've updated my solution
TehiroGoo 12-Apr-14 8:47am    
Didn't working.

http://i.imgur.com/5y8GanU.png

What am I doing wrong?
CHill60 12-Apr-14 9:00am    
File.Copy takes SourceFileName and DestinationFileName. "C:\Cos.txt\" is not a valid filename and "s" is a foldername not a filename
Try System.IO.File.Copy("C:\Cos.txt", s + "\cos.txt")
TehiroGoo 12-Apr-14 9:07am    
Wow! Working! Thank You very much :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900