Using xp_dirtree in a while loop

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

crompnkData Management SpecialistAsked:
Who is Participating?
 
pcelbaCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.