moving files into folders based on filename

Camus

Active member
Joined
Oct 4, 2007
Messages
28
Programming Experience
Beginner
Hey, can anyone give me a hand with some code that automatically moves files with a certain word within the filename to a specific folder?

So all files where the filename contains "LH" gets moved to the folder called "LH"...all files where the filename contains "BH" gets moved to the folder called "Bankh".

The directory where the filenames are to be searched remains constant, and the folders that i want to move the files to are also constant.

I've been trying to do this for a while via other means, but haven't got anywhere.
 
you could give this a try -- haven't tried it or tested it

VB.NET:
  Public Sub Searching()

        Dim diStart As New System.IO.DirectoryInfo("C:\MyFolder\MoveFrom")
        Dim aryFinds() As String = {"LH", "BH", "RT"}

        'loop through file names
        For Each fiFile As FileInfo In diStart.GetFiles
            'loop through the 'file name contains'
            For intLoop As Integer = 0 To 2
                'this will loop around each file name up to, in this case 3 times
                ' and check to see if the values are in the file name
                If fiFile.Name.ToUpper.Contains(aryFinds(intLoop)) Then
                    'moves the file to the folder specified
                    My.Computer.FileSystem.MoveFile(fiFile.FullName, "C:\MyFolder\MoveFrom\" & aryFinds(intLoop))
                    Exit For
                End If
            Next
        Next
    End Sub
 
Back
Top