Windows service to copy files from one shared folder to another, creating folders if not existing

I have to write an application (actually it has to be a windows service) that will look at a shared folder on one machine and copy the files over to another machine. The file names contain a date and year, based on those they need to be placed in subfolders in the new directory. Ex.: File name: J200601.jpg needs to be moved to Signatures/Jan2006/. It needs to check if the directory exists and if it doesn't, it needs to create it.

My experience with VB .NET is VERY limited so any help in pointing me in the right direction would be very much appreciated.
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.

jayknightAuthor Commented:
Jake, Thanks! I had actually found the links above was able to successfully create and install a windows service.

Now the question I have (and this will be a stupid question to those of you who know what they're doing) is where do I put the code to do the file copying in my application? Do I add a function with the code on another page and then call that function? If so, where do I call it from?
It really depends on how you want your service to work.

If your service should continually try to look at the shared folder, and move files, then you probably want to add a timer.  Then, in the Timer1_Elapsed, you put your code for handling the files...  I think that is what you want to do, so just add a timer, set the interval to a suitable value, and put your code in the Elapsed event, and it will run every XX000 where XX is seconds (the interval is in milliseconds).

Hope that helps,


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
Fahad MukhtarDistinguished EngineerCommented:
you need to look into the classes and functions provided in System.IO Namespace like

System.IO.Directory.Exist("c:\TEST") ' returns true if Directory exists , false otherwise

System.IO.Directory.CreateDirectory ("c:\TEST") ' creates directory

etc for copy , move and delete files

also see the FileSystemWatcher object to watch a folder instead of going with the timer option
heres a nice article that explains it more
[Watching Folder Activity in VB.NET]

jayknightAuthor Commented:
O.k., I used a little bit of both of your suggestions. I now have this code:
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
              FileName = Right(file.Name, 16)
            FileName = Left(FileName, 6)
            Dim DirInfo As New System.IO.DirectoryInfo(DestPath + FileName)
            'check if the subdirectory already exists
            'if yes, move files
            If DirInfo.Exists Then
                file.MoveTo(DestPath + file.Name)
            'if not, create it, then move files
                'create the subdirectory
                System.IO.Directory.CreateDirectory(DestPath + FileName)
                'move files to appropriate subdirectory
                file.MoveTo(DestPath + file.Name)
            End If
This is working great, it's creating the sub folders and moving the files, however, I need the files in the NEW sub directories based on the file name. 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. Make sense?

Also, I will raise the points and then split them between the 2 of you (if that's possible).
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.