Identifying non gif files and move to different directory

I need a program which does the following:

1. Watch for any non gif files under \\myfileshare(include sub directories) and move them to \\newfileshare(different file share).

I'm trying to do it with Filesystemwatcher but couldn't find a way to ignore files of type gif.

is there any better/easy way of doing this? Can some one provide a code snippet for this?
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.

Bob LearnedCommented:
Are you trying to ignore by file content, or by file extension?  

If you are looking to ignore by file extension, then you can use System.IO.Path.GetExtension(fileName) to test the extension, and skip those with the .gif extension.

netsol07Author Commented:
I'm loking by file extension only. Doing it with filewatcher seems bit unacceptable in my team. So I'm looking to run this as a scheduled task.

Can you provide me the code snippet for this?
Bob LearnedCommented:
Are looking for something more than a simple If block?

If System.IO.Path.GetExtension(fileName) <> ".gif" Then
    ' Process the file
End If

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

netsol07Author Commented:
Thanks for that but I'm looking for the code block which traverse through the directories under by base directory and if finds any files with 'gif' extension move them to a different location.

I'm actually struck on the drilldown part.
Bob LearnedCommented:
Here is one way to accomplish that task:
Imports System.Collections.Generic
Imports System.IO
Public Class FileFinder
	''' <summary>
	''' Find files that match a specific pattern.  Return a list of FileInfo instances
	''' that describe each file (FileInfo.FullName).
	''' <paramref name="path">Full path file specification.</paramref>
	''' <paramref name="pattern">Wild-card file matching (i.e. *.gif).</paramref>
	''' </summary>
	Public Shared Function FindFiles(ByVal path As String, ByVal pattern As String) As List(Of FileInfo)
		Dim list As New List(Of FileInfo)
		SearchFolder(path, pattern, list)
		Return list
	End Function
	Private Shared Sub SearchFolder(ByVal path As String, ByVal pattern As String, ByVal list As List(Of FileInfo))
		' Add the matching files for the current directory.
		Dim directory As New DirectoryInfo(path)
		' Recursively search each sub-directory.
		For Each subDirectory As DirectoryInfo In directory.GetDirectories()
			SearchFolder(subDirectory.FullName, pattern, list)
		Next subDirectory
	End Sub
End Class

Open in new window


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
Bob LearnedCommented:
Example usage:

Dim fileList As List(Of IO.FileInfo) = FileFinder("C:\Temp", "*.gif")

For Each file As FileInfo In fileList
   ' Move file.
Next file

Use System.IO.Directory.GetFiles Method (String, String, SearchOption)
netsol07Author Commented:
Thank you for the solution
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
.NET Programming

From novice to tech pro — start learning today.