Get list of files inside folder

Posted on 2008-11-13
Last Modified: 2013-11-05
I need to write a store procedure in SQL 2005 to run every night.
This procedure should go to a specific folder and obtain the name of each an all files existing inside the folder and/or subfolders (get each name, size, format, etc and add it to a table in my database).
Question by:melinalt
    LVL 9

    Expert Comment

    It seems that  you are going to need SSIS or CLR Stored Procedures. Please let me know if you have CLR on in your DB?
    LVL 9

    Expert Comment

    or master.dbo.xp_cmdshell
    INSERT Table EXECUTE @Return = master.dbo.xp_cmdshell  'DIR C:\Program Files\'
    LVL 9

    Accepted Solution

    I will go with CLR Stored Procedures or SSIS but if all you have on is xp_cmdshell. This will give you a good start enjoy :)
    LVL 1

    Author Closing Comment

    Thanks a lot.. It did the trick
    LVL 1

    Author Comment

    Thanks Ernariash....

    Your help was great... I wrote a store procedure following your example and it worked perfectly.

    Here is the entire code in case someone else needs it:

    @Return int,
    @Work varchar(2000),
    @PCWrite varchar(2000)
    SET @PCWrite = '\\myserver\'
    SET @Work = 'DIR ' + '"' + @PCWrite + '"'
    CREATE TABLE #Files ([ID] int IDENTITY (1, 1) NOT NULL,MyFile nvarchar(200))
    INSERT #Files EXECUTE @Return = master.dbo.xp_cmdshell  @Work
    'Clean up to get only the files (no directories or others)
    DELETE #Files WHERE (MyFile is null) or (MyFile='') or (MyFile like '%Directory of%') or (MyFile like '%<DIR>%') or (MyFile like '%Volume%') or (MyFile like '%bytes free%') or (MyFile like '%file(s)%')
    'List only the file name and type (without date/size)
    SELECT SUBSTRING(MyFile,40,100)  as fName, (right(SUBSTRING(MyFile,40,100), (len(SUBSTRING(MyFile,40,100)) - charindex('.',SUBSTRING(MyFile,40,100))))) as fType FROM #Files order by myfile
    drop table #Files

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Suggested Solutions

    I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
    When writing XML code a very difficult part is when we like to remove all the elements or attributes from the XML that have no data. I would like to share a set of recursive MSSQL stored procedures that I have made to remove those elements from …
    This video discusses moving either the default database or any database to a new volume.
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now