We help IT Professionals succeed at work.

Identifying non gif files and move to different directory vb.net

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?
Comment
Watch Question

SILVER EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
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.

Bob

Author

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?
SILVER EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
Are looking for something more than a simple If block?

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

Bob

Author

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.
SILVER EXPERT
Most Valuable Expert 2012
Top Expert 2008
Commented:
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)
		list.AddRange(directory.GetFiles(pattern))
 
		' 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

SILVER EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
Example usage:

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

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

Bob

Commented:
Use System.IO.Directory.GetFiles Method (String, String, SearchOption)

Author

Commented:
Thank you for the solution

Explore More ContentExplore courses, solutions, and other research materials related to this topic.