?
Solved

Using xp_dirtree in a while loop

Posted on 2010-01-07
1
Medium Priority
?
1,967 Views
Last Modified: 2012-05-08
Hi,
I am trying to use the procedure xp_dirtree with an insert statement that is inside a while loop.

The sql below starts of by populating a table called dirtemp with the subdirectories for the given path 'C:\Temp'
it also adds a RowID to the table.

The sql then creates another table dirtemp2, which is designed to hold the subdirectory and their files.
The aim is to use the table dirtemp to loop through each subdirectory in the given path and populate dirtemp2 with the files.

At the moment the table dirtemp2 doesn't populate.
Is it possible to use the xp_dirtree in a while statement and if so how. Can you pass other variables to insert statement within the loop.

Thank you


drop table dirtmp
go
drop table dirtmp2
go

create table dirtmp ( RowID int identity(1, 1),[Subdirectory] nvarchar(1000), [Depth] int
)
go

insert into dirtmp(Subdirectory,Depth) exec master..xp_dirtree 'C:\Temp',1
go

create table dirtmp2 ([Files] nvarchar(1000),[Subdirectory] nvarchar(1000)
)
go

declare @NumberRecords int, @RowCount int
declare @path nvarchar(1000),@filepath nvarchar(1000), @subdirectory nvarchar(1000)

-- Get the number of records in the temporary table
set @NumberRecords = @@ROWCOUNT
set @RowCount = 1

set @path = 'C:\Temp\'

-- loop through all records in the temporary table
-- using the WHILE loop construct
while @RowCount <= @NumberRecords
begin
select @subdirectory = Subdirectory, @filepath = @path + Subdirectory
 from dirtmp
 where RowID = @RowCount

	insert into dirtmp2(Subdirectory,Files)
	@subdirectory, exec master..xp_dirtree @filepath

 set @RowCount = @RowCount + 1
end

select * from dirtmp
select * from dirtmp2

Open in new window

0
Comment
Question by:crompnk
1 Comment
 
LVL 43

Accepted Solution

by:
pcelba earned 2000 total points
ID: 26201375
It seems you need one more temp table and xp_dirtree parameters update:
drop table dirtmp 
go 
drop table dirtmp2 
go 
drop table dirtmp3
go 
 
create table dirtmp ( RowID int identity(1, 1),[Subdirectory] nvarchar(1000), [Depth] int 
) 
go 
 
insert into dirtmp(Subdirectory,Depth) exec master..xp_dirtree 'c:\temp',1 
go 
 
create table dirtmp2 ([Files] nvarchar(1000),[Subdirectory] nvarchar(1000)
) 
go 

create table dirtmp3 ( RowID int identity(1, 1),[Subdirectory] nvarchar(1000), [Depth] int, [File] int 
) 
go 
 

 
declare @NumberRecords int, @RowCount int 
declare @path nvarchar(1000),@filepath nvarchar(1000), @subdirectory nvarchar(1000) 
 
-- Get the number of records in the temporary table 
set @NumberRecords = (SELECT COUNT(*) FROM dirtmp)
print @NumberRecords
set @RowCount = 1
 
set @path = 'C:\temp\' 
 
-- loop through all records in the temporary table 
-- using the WHILE loop construct 
while @RowCount <= @NumberRecords 
begin 
select @subdirectory = Subdirectory, @filepath = @path + Subdirectory 
 from dirtmp 
 where RowID = @RowCount 
 
        insert into dirtmp3(Subdirectory,depth,[file]) 
          exec master..xp_dirtree @filepath,1,1 

        insert into dirtmp2(Subdirectory,[files]) 
          SELECT @subdirectory, Subdirectory FROM dirtmp3
        
        truncate table dirtmp3
 
 set @RowCount = @RowCount + 1 
end 
 
select * from dirtmp 
select * from dirtmp2

Open in new window

0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Suggested Courses

864 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