Click here to Skip to main content
16,022,924 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I have to write stored procedure which will file name,file name without extension from given path.I got following code from code project.The Problem is I have to get all files name in a given folder.The below code is working for given path. Can you please help me.
SQL
ALTER PROCEDURE [dbo].[ReadFileName]
    -- Add the parameters for the stored procedure here
    @FILE_PATH   VARCHAR(MAX),
    @NAME        VARCHAR(MAX) OUTPUT
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    DECLARE @FILE_LENGTH INT
    DECLARE @FILE_NAME VARCHAR(MAX)

    SET @FILE_LENGTH = LEN(@FILE_PATH)
    WHILE (@FILE_LENGTH <> 0)
    BEGIN
        IF SUBSTRING(@FILE_PATH, @FILE_LENGTH, 1) = '\' 
        BEGIN
            SET @FILE_NAME = RIGHT(@FILE_PATH, LEN(@FILE_PATH) - @FILE_LENGTH)
            BREAK
        END
        SET @FILE_LENGTH = @FILE_LENGTH - 1
    END
   
    SET @NAME = @FILE_NAME
    INSERT INTO TABLE_NAME(COL1,COL2,COL3) values(@FILE_NAME,@FILE_PATH,@FILE_NAME)
    UPDATE TABLE_NAME
        SET COL1 = REVERSE(SUBSTRING(REVERSE(COL1), 
        CHARINDEX('.', REVERSE(COL1)) + 1, 999))

    RETURN 
END
Posted
Updated 25-Jun-13 23:51pm
v2
Comments
Maciej Los 26-Jun-13 5:59am    
Stored procedure (SP) does not read files or their file names. Above SP inserts into Table_name in cols 1-3: file name, file path and file name.

Please, be more specific and describe your problem correctly. Where are you stuck? What have you done till now?
xyz from Bangalore 26-Jun-13 6:11am    
In stored procedure it will read file name without extension and with extension. And I am inserting those two into a table along with file path.I want same process should loop through all files in a given folder path.
Maciej Los 26-Jun-13 6:39am    
I think you're completly wrong...
If you want to save the name of files from given folder path you need to loop through the collection of files and call SP with the name of file as a parameter.
xyz from Bangalore 26-Jun-13 8:33am    
Can you explain me briefly.I need to store file name with extension and without extension and path of each file inside a folder.
RedDk 26-Jun-13 14:19pm    
I think doing the responsible thing would be to use:
[code]

EXECUTE xp_cmdshell 'dir /s /b c:\*.xyz > c:\users\XB\dirdir.txt'
[end code]

And then use some BULK INSERT logic up there in this big old stored procedure.

Hello XYZ,

You can use master.sys.xp_dirtree stored procedure to achieve this. This procedure takes following parameters

  1. Directory - The full path of the directory to be scanned
  2. depth - Number of how many subfolder levels to traverse. The default of 0 will display all subfolders.
  3. file - A value to enable a file or folder display. The default of 0 will not display any files.

Please have a look at this[^] or this[^] articles related to this procedure.

Regards,
 
Share this answer
 
Comments
xyz from Bangalore 26-Jun-13 6:06am    
Hi Prasad,
I tried that also but I need to insert file name and path into another table.By above procedure I will get only single file foe specified path.This should happen for entire files in folder.
Prasad Khandekar 26-Jun-13 8:52am    
Hello Xyz,

May be you forgot to look at the links given in article. It shows how to do this. The full path can be obtained by concatenating the base path which you will be supplying as an argument to this procedure.

Regards,
xyz from Bangalore 27-Jun-13 1:46am    
Hi Prasad,
I saw links and tried to concatenate base path with file name but I am getting error saying that Subquery returned more than 1 value.I concatenated base path with file name selecting from table.
Can you please help me.
SQL
declare @files table (ID int IDENTITY, FileName varchar(100))
insert into @files execute xp_cmdshell 'dir c:\ /b'
select * from @files

Original answer : How to read list of files in folder using T-SQL ...[^]
 
Share this answer
 
Comments
Maciej Los 26-Jun-13 6:37am    
It's good but xp_cmdshell[^] command has many restrictions.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900