Examples

Current Articles | Categories | Search

By David Dye @ Sunday, May 04, 2008 4:20 AM :: 2368 Views :: 0 Comments

Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.SqlTypes
Imports Microsoft.SqlServer.Server
Imports System.IO
 
Partial Public Class UserDefinedFunctions
    <Microsoft.SqlServer.Server.SqlFunction(DataAccess:=DataAccessKind.Read)> _
    Public Shared Function clr_fn_recentfile(ByVal Filepath As SqlString, _
                                             ByVal Criteria As SqlString, _
                                             ByVal ext As SqlString) As SqlString
        Dim strFile As String
        Dim maxDate As Date
        Dim fil As String = ""
        Dim fpath As String
        Dim extnsn As String
        extnsn = ext.ToString
        'assign the input parameter of Filepath to a string variable to better work with the value
        fpath = Filepath.ToString
        Try
            'Due to issues that arise if the "\" is left off of the path this appends
            'the character to assure consistency of results
            If fpath.Substring(fpath.Length - 1, 1) <> "\" Then
                fpath = fpath.ToString + "\"
            End If
            'Evaluate the second parameter in order to return the appropriate file name
            'passing "created" will return the file with the most recent "Date Created"
            'file attribute
            Select Case Criteria.ToString
                Case "created"
                    'Assure that the directory exists before searching the file objects
                    If Directory.Exists(fpath) Then
                        'Use For Each to iterate through each file in the specified path
                        For Each strFile In Directory.GetFiles(fpath)
                            Dim fi As New FileInfo(strFile)
                            'If the maxDate variable has no value assigned to it then assign
                            'the files creationtime attribute and assign the filename to the
                            'fil variable
                            If maxDate = Nothing Then
                                If extnsn = "*" Then
                                    maxDate = fi.CreationTime
                                    fil = fi.FullName.ToString
                                Else
                                    If fi.Extension.ToString = extnsn Then
                                        maxDate = fi.CreationTime
                                        fil = fi.FullName.ToString
                                    End If
                                End If
                                'If the maxDate already has a value assigned then compare the
                                'new creationtime attribute to the maxDate variable and if
                                'this value is more recent assign the creationtime attribute
                                'to the maxdate variable and filename to the fil variable
                            Else
                                If extnsn = "*" Then
                                    If maxDate < fi.CreationTime Then
                                        maxDate = fi.CreationTime
                                        fil = Path.GetFileName(strFile)
                                    End If
                                Else
                                    If fi.Extension.ToString = extnsn Then
                                        maxDate = fi.CreationTime
                                        fil = fi.FullName.ToString
                                    End If
                                End If
                            End If
                        Next
                        'If the directory does not exists then return NULL
                    Else
                        Return "NULL"
                        Exit Function
                    End If
                    'If no file was found return NULL
                    If fil <> Nothing Then
                        Return fil
                    Else
                        Return "NULL"
                    End If
                    'Evaluate the second parameter in order to return the appropriate file name
                    'passing "modified" will return the file with the most recent "Date Modified"
                    'file attribute
                Case "modified"
                    'Assure that the directory exists before searching the file objects
                    If Directory.Exists(fpath) Then
                        'Use For Each to iterate through each file in the specified path
                        For Each strFile In Directory.GetFiles(fpath)
                            Dim fi As New FileInfo(strFile)
                            'If the maxDate variable has no value assigned to it then assign
                            'the files creationtime attribute and assign the filename to the
                            'fil variable
                            If maxDate = Nothing Then
                               If extnsn = "*" Then
                                    maxDate = fi.CreationTime
                                    fil = fi.FullName.ToString
                                Else
                                    If fi.Extension.ToString = extnsn Then
                                        maxDate = fi.CreationTime
                                        fil = fi.FullName.ToString
                                    End If
                                End If
                                'If the maxDate already has a value assigned then compare the
                                'new creationtime attribute to the maxDate variable and if
                                'this value is more recent assign the creationtime attribute
                                'to the maxdate variable and filename to the fil variable
                            Else
                                If extnsn = "*" Then
                                    If maxDate < fi.CreationTime Then
                                        maxDate = fi.CreationTime
                                        fil = Path.GetFileName(strFile)
                                    End If
                                Else
                                    If fi.Extension.ToString = extnsn Then
                                        maxDate = fi.CreationTime
                                        fil = fi.FullName.ToString
                                    End If
                                End If
                            End If
                        Next
                        'If the directory does not exists then return NULL
                    Else
                        Return "NULL"
                        Exit Function
                    End If
                    'If no file was found return NULL
                    If fil <> Nothing Then
                        Return fil
                    Else
                        Return "NULL"
                    End If
                    'Evaluate the second parameter in order to return the appropriate file name
                    'passing "name" will return the file using the filename attribute
            End Select
        Catch ex As Exception
        End Try
    End Function

The following scripts demonstrate this functions usage.

/*Declare varaible and assign the value of the most recent file based on date created file 
attribute*/
DECLARE @file AS VARCHAR(100) 
SET @file = (SELECT dbo.clr_fn_recentfile('c:\', 'created', '.xls'))
SELECT @file
GO 

 /*Declare varaible and assign the value of the most recent file based on modified date
file attribute. Test to see if the folder exists or the criteria exists*/ 

DECLARE @file AS VARCHAR(70)
SET @file = (SELECT dbo.clr_fn_recentfile('\\computer\c$\folder\', 'modified', '.trn'))
IF @file = 'NULL'
BEGIN
SELECT @file
END
 
GO
Posted @ Sunday, May 04, 2008 4:20 AM by David Dye
Previous Page | Next Page
Comments
Currently, there are no comments. Be the first to post one!
You must be logged in to post a comment. You can login here

Survey

Which of the following CLR objects are you currently using in SQL Server?




Submit Survey  View Results

Links

  Search

What's New

 Subscribe in a reader

Q&A with Adam Machanic - Who's Afraid of SQLCLR by Jonathan Kehayias

You could almost hear the gasps of horror when Microsoft integrated the .NET Common Language Runtime (CLR) into SQL Server 2005. Run C# or VB.NET code inside SQL Server? For many database developers and administrators, it was like an alien had just burst through SQL Server’s midsection.

But SQL Server MVP Adam Machanic says that more and more developers and DBAs are discovering that far from destroying SQL Server performance and security, SQLCLR is actually a powerful ally in solving complex business and technical problems.

Read More on the PASS website..

SQLCLR String Splitting Part 2: Even Faster, Even More Scalable by Site Administrator

SQL Server MVP Adam Machanic shows a new way of parsing strings in SQL Server using SQLCLR that outperforms all conventional TSQL methods, as well as the most common SQLCLR implemenations, using a custom split function that allows streaming of the results.

Trading in xp_cmdshell for SQLCLR (Part 1) - List Directory Contents by Jonathan Kehayias

Learn how to use SQLCLR to get file system information instead of using xp_cmdshell on your SQL Servers.

Recent Examples


Copyright 2007 by SQLCLR.net Terms Of Use Privacy Statement
Website graphics provided by Matt Green Designs
Page generated in 0.2184056 seconds.

All information and example code on this site is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from its use.

This site is in no way affiliated with Microsoft. Unless specifically stated otherwise, nothing should be construed to represent the official positions or opinions of Microsoft and/or its Employees.