Move files to subdirectories based on file name

Hi Experts! I'm new to VB.NET and need some help.
I need a windows service that will look at the file names in a shared folder, create sub folders based on those file names and then move the files into the appropriate folder. Ex.: File name 200601.jpg needs to go into the folder called 200601, File name 200602.jpg needs to go into the directory called 200602.

Here is what I have so far. It looks at the files, creates the subdirectories and moves the files, but I need help with moving the files into the correct folder.
TIA!
================================================
        Dim SourcePath As String = "Y:\SigFiles\"
        Dim DestPath As String = "C:\SigFiles\"
        Dim FileName As String
        Dim Directory As New System.IO.DirectoryInfo(SourcePath)
        Dim file As System.IO.FileInfo
        For Each file In Directory.GetFiles
            'check file name
            FileName = Right(file.Name, 16)
            FileName = Left(FileName, 6)
            'create subdirectory
            Dim DirInfo As New System.IO.DirectoryInfo(DestPath + FileName)
            'check if the subdirectory already exists, if not, create it
            If Not DirInfo.Exists Then System.IO.Directory.CreateDirectory(DestPath + FileName)
            'move files to subdirectory based on name
             file.MoveTo(DestPath + file.Name)
         Next
=============================================
LVL 1
jayknightAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

tpwellsCommented:
This should do it. Modified your code a little:

Dim SourcePath As String = "Y:\SigFiles\"
        Dim DestPath As String = "C:\SigFiles\"
        'Dim FileName As String
        'Dim Directory As New System.IO.DirectoryInfo(SourcePath)
        Dim file As System.IO.FileInfo
        For Each sFile As String In System.IO.Directory.GetFiles(SourcePath)
            file = New FileInfo(sFile)
            If Not System.IO.Directory.Exists(DestPath & "\" & file.Name) Then System.IO.Directory.CreateDirectory(DestPath & "\" & file.Name)
            'move files to subdirectory based on name
            file.MoveTo(DestPath & "\" & file.Name)
        Next

Also if you are moving from a networked drive you may need to delete it from the source drive on successful move
jereteCommented:
shouldn't it be

  file.MoveTo(DestPath + FileName) '<-- instead of file.Name

?

(not sure if you need the "\" as well but that'd be file.MoveTo(DestPath + FileName + "\")
tpwellsCommented:
jerete

In most cases I have noticed in .NET the extra "\" is needed. I removed the FileName variable above and am using the Name property of FileInfo. using variable name "file" for a FileInfo is a little confusing though since it could be confused with System.IO.File.

 So jayknight, I would recommend using a different variable name for the FileInfo variable.

Side Note:
You can also move the file using System.IO.File.Move() method as well that takes the source file and the destination file.

OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

jayknightAuthor Commented:
jerete, that's not working. It's only creating 1 folder (even though there are several files with different names in the shared folder) and it's not moving the files.
tpwellsCommented:
try this:

Dim SourcePath As String = "Y:\SigFiles\"
        Dim DestPath As String = "C:\SigFiles\"
        'Dim FileName As String
        'Dim Directory As New System.IO.DirectoryInfo(SourcePath)
        Dim fi As System.IO.FileInfo
        For Each sFile As String In System.IO.Directory.GetFiles(SourcePath)
            fi = New FileInfo(sFile)
            If Not System.IO.Directory.Exists(DestPath & fi.Name) Then System.IO.Directory.CreateDirectory(DestPath & fi.Name)
            'move files to subdirectory based on name
            File.Copy(sFile, DestPath & fi.Name)
        Next

I see what you were saying jerete: the "\" was already defined in the initlization of the path variables
jayknightAuthor Commented:
Oops, I meant tpwells (not jerete) the first time. Sorry about that. It's still not working though. Doing the same thing, it's creating a subfolder but not moving (or copying any files).
tpwellsCommented:
is it casting an error of any kind
tpwellsCommented:
change:

 File.Copy(sFile, DestPath & fi.Name)

To:
 File.Copy(sFile, DestPath & fi.Name & "\" & fi.Name)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jayknightAuthor Commented:
tpwells, that works great! Thanks!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.