[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

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

Posted on 2008-01-29
8
Medium Priority
?
271 Views
Last Modified: 2013-11-26
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?
0
Comment
Question by:netsol07
  • 4
  • 3
8 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20771873
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
0
 

Author Comment

by:netsol07
ID: 20772053
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?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20772296
Are looking for something more than a simple If block?

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

Bob
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 

Author Comment

by:netsol07
ID: 20772382
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.
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 2000 total points
ID: 20772535
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

0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20772550
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
0
 
LVL 4

Expert Comment

by:J1H1W1
ID: 20773427
Use System.IO.Directory.GetFiles Method (String, String, SearchOption)
0
 

Author Closing Comment

by:netsol07
ID: 31426069
Thank you for the solution
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
This video tutorial shows you the steps to go through to set up what I believe to be the best email app on the android platform to read Exchange mail.  Get the app on your phone: The first step is to make sure you have the Samsung Email app on your …
Suggested Courses
Course of the Month9 days, 13 hours left to enroll

591 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question