fnGetDrives

SQL Server CLR IO Utility

Warning: the use of TRUSTWORTHY described in this article can be a potential security risk so you might want to consider alternative options.

Introduction

Using CLR (.NET Framework) code in SQL Server is a very powerful way of extending the built-in features and functionality. The .NET framework provides access to a library of useful code, allowing you to accomplish useful tasks with a minimal amount of code.  The SQL Server IO project does just that; providing access to some useful IO utilities from within SQL Server by leveraging the power of the .NET Framework.

Installation Instructions

You need to ensure that CLR is enabled for the server and the trustworthy database setting is turned on.  The trustworthy database setting is required to allow CLR code to have external access.

To enable CLR, run the following code:

To enable the trustworthy setting for the database, run the following code:

Warning: the use of TRUSTWORTHY can be a potential security risk.

Next, download the “T-SQL Code” and run it in SQL Server Management Studio to install the CLR functions and stored procedures in the current database.

Note: Consider the security implecations before running any of the code in this article.

Code Download

DownloadT-SQL Code

DownloadVisual Studio Source Code

CLR Functions

SQLIO_fnGetDrives

Returns information about the drives installed on your computer/server. SELECT * FROM dbo.SQLIO_fnGetDrives()

SQLIO_fnGetDrives function output

SQLIO_fnGetFiles

Returns a list of files in the specified folder.  The function takes 3 parameters:

@path – The folder from which to start the search
@searchPattern – The search string to match against the names of files in path
@includeSubfolders – Specifies whether the search operation should include all subdirectories or only the current directory.

SQLIO_fnGetFiles function output

SQLIO_fnGetFolders

Returns a list of subfolders within the specified folder.  The function takes 3 parameters:

@path – The folder from which to start the search
@searchPattern – The search string to match against the names of files in path
@includeSubfolders – Specifies whether the search operation should include all subdirectories or only the current directory.

SQLIO_fnFileExists

Takes a file path as a parameter and returns the bit value “1” if the file exists.

SQLIO_fnFolderExists

Takes a folder path as a parameter and returns the bit value “1” if the folder exists.

SQLIO_fnReadFile

Takes the path of a file as a parameter and returns the file data in varbinary format.

SQLIO_fnReadTextFile

Takes the path of a file as an parameter and returns the file data in nvarchar format.

CLR Stored Procedures

SQLIO_DeleteFile

Takes the path of a file as an parameter and deletes the specified file. 

SQLIO_DeleteFolder

Takes the path of a folder as a parameter and deletes the specified folder.

SQLIO_DeleteFiles

Takes the path of a folder, a search pattern and an include subfolders parameter, similar to the dbo.SQLIO_fnGetFiles function and deletes all the files matched from the search.

SQLIO_DeleteFilesOlderThan

This stored procedure is the same as the dbo.SQLIO_DeleteFiles procedure, but with an additional maxAge date parameter.  Only files older than the maxAge date specified will be deleted.

SQLIO_CreateFolder

Takes a path as a parameter and creates the new folder on the file system.

SQLIO_CreateFile

Creates a new file, by taking a file path and varbinary data value.

SQLIO_CreateTextFile

Creates a new text file, by specifying a file path and string value.

SQLIO_AppendTextFile

Appends text to the end of a file, by specifying a file path and text to append.

SQLIO_MoveFile

Takes source and destination parameters, moving a file from one location to the other.

SQLIO_CopyFile

This stored procedure takes source and destination parameters, copying a file from one location to the other.

SQLIO_MoveFolder

Takes source and destination parameters, copying a folder from one location to the other.

Posted in T-SQL, T-SQL CLR and tagged .