Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Other Forums
 Other Topics
 Reading contents of a directory in SQL Server

Author  Topic 

owen912
Starting Member

31 Posts

Posted - 2004-10-04 : 10:09:26
Is there a quick and dirty way to read the contents of a directory and place it in something like a temp table?

I am setting up a process where a stored procedure will read a text file, and then import that data into a table. After the text file data is read and inserted into a table, I then move the file to an archive directory using the xp_cmdshell extend stored procedure.

I am learning now there may me multiple files dumped to the import directory. I want read the contents of the directory to populate a drop down list from which the user selects. Normally I would write this out in the application logic (ASP, etc.), but I am using a report builder GUI that lets me use SQL only (either stored procedures or ad hoc queries).

I did something like the example below and was able to get the directory information, but it’s going to require more massaging before I can use it in a drop down list?

exec master..xp_cmdshell 'Dir c:\ImportData\'

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2004-10-04 : 10:20:44
try this out...


Declare @cmdStr nvarchar(1050)
Set @cmdStr = 'DIR /a-d ' + 'c:\importData\'

--Drop table #WebPages
Create Table #dirOutput (Id [int] IDENTITY (1, 1) NOT NULL, Diroutput nvarchar(4000))
INSERT #dirOutput EXEC master.dbo.xp_cmdshell @cmdStr

Create table #FileList
(
Id int,
CreatedDate datetime,
FileSize int,
Name nvarchar(1000)
Primary Key (Id)
)

Insert Into #FileList
Select Id,
CreatedDate = convert(Datetime,left(DirOutPut,17) + 'm'),
FileSize_kb = round(convert(float,Replace(substring(dirOutPut,18,charindex(' ',dirOutPut,19)-18),',',''))/1024.0,0),
Name = substring(dirOutPut,charindex(' ',dirOutPut,19)+1,len(dirOutPut)-charindex(' ',dirOutPut,19))
From
(
Select Id, dirOutPut=Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(dirOutPut,' ',' '),' ',' '),' ',' '),' ',' '),' ',' '),' ',' '),' ',' '),' ',' ')
from #dirOutput
Where dirOutPut not like '%Volume in Drive%'
and dirOutPut not like '%Volume Serial Number is%'
and dirOutPut not like '%Directory of%'
and dirOutPut not like '%Total Files Listed:%'
and dirOutPut not like '%file(s)%'
and dirOutPut not like '%dir(s)%'
and dirOutPut is not null
) as Files

-- Select * From #dirOutput
Select * From #filelist

Drop table #filelist
Drop table #dirOutPut


Corey
Go to Top of Page

owen912
Starting Member

31 Posts

Posted - 2004-10-04 : 10:29:40
Thanks, that work quite nicely.

I was thinking I would have to write something like this, but you already did. This may be hyperbole, but I feel like I'm standing on the shoulders of giants.

Mike
Go to Top of Page

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2004-10-04 : 11:04:30
well, I figured, I've done this many times ... why not help a guy out

Corey
Go to Top of Page
   

- Advertisement -