연구개발/CLR

파일리스트 불러오기

알 수 없는 사용자 2009. 6. 22. 21:10


김종열

MS SQL Server MVP

 


다른 커뮤너티에서 폴더의 파일들을 어떻게 불러오는가에 대한 질문이 곧잘 올라오더군요.
해서 간단하게 모듈하나 만들어봤습니다. (물론 xp_cmdshell등과 함께 써도 되겠지만... )

폴더와 파일타입 두개 정도만 받고 이를 Table형식으로 리턴해주는 함수입니다.


using System;

using System.Data;

using System.Data.SqlClient;

using System.Data.SqlTypes;

using Microsoft.SqlServer.Server;

using System.IO;

using System.Collections;

 

public partial class UserDefinedFunctions

{

    //[Microsoft.SqlServer.Server.SqlFunction]

    [Microsoft.SqlServer.Server.SqlFunction(FillRowMethodName = "f_fileList", TableDefinition = "fName nvarchar(50)")]

    public static IEnumerable UDF_FILE_LIST(SqlString dName, SqlString fType)

    {

        string strPath = dName.ToString();

        string strType = fType.ToString();

        string sItem = "";

 

        DirectoryInfo _dir = new DirectoryInfo(strPath);

 

        FileInfo[] f_info = _dir.GetFiles(strType);

 

        foreach (FileInfo f in f_info)

        {

            sItem += f.Name + "|";

        }

        sItem = Microsoft.VisualBasic.Strings.Left(sItem, sItem.Length - 1);

        string[] arrS = sItem.Split('|');

 

        return (arrS);

 

    }

 

    private static void f_fileList(Object obj, out SqlString fName)

    {

        string sTemp = Convert.ToString(obj);

        try

        {

            fName = sTemp;

        }

        catch

        {

            fName = null;

        }

 

    }

};

 

 

 

실행하는 샘플들입니다.

select * from dbo.UDF_FILE_LIST( 'd:\', 'copy*.*') a

select * from dbo.UDF_FILE_LIST( 'c:\windows\', '*.*') a