Using xp_dirtree to pass filenames to a loop

Hi there,

Im working on building a file list from a directory I have and wish to loop through the filenames.

Ive worked out I can get the list using EXEC xp_dirtree @path, 10, 1

Im just not sure out I pass this to my loop 1 element at a time.

e.g. Lets say there are 10 files in my folder, I wish to loop through each name using my @Filename parameter

Thanks,

Bassmann

DECLARE @Path varchar(50), @FileName varchar(20)

SET @Path = 'C:\myFiles\'

EXEC xp_dirtree @path, 10, 1

              BEGIN

                     SELECT TOP(1)

                            @FileName = 'pass each file name into here'

                            print @path + @filename

                     BEGIN TRY

 

                            print 'OK: ' + @FileName

 

                     END TRY

                     BEGIN CATCH

                            print ' '

                            print 'Error: ' + @FileName

 

                     END CATCH

              END

August 27th, 2015 4:26pm

Good day,

In this blog you can see example how to use xp_dirtree in order to insert all the information to a table, and then loop through all the files.

http://www.patrickkeisler.com/2012/12/how-to-use-xpdirtree-to-list-all-files-part2.html

In this blog you can see the same idea using xp_cmdshell instead of xp_dirtree 

http://ariely.info/Blog/tabid/83/EntryId/123/Dynamically-Bulk-Insert-All-Files-In-Folder.aspx

* The second blog use the information without any loop. This approach is much better. you can create single dynamic query from all the files in the folder and execute it directly. Both blogs first insert the files names to a table, the different is in the second step, using the information.

I hope this helpful :-)

Free Windows Admin Tool Kit Click here and download it now
August 27th, 2015 4:40pm


DECLARE @Path varchar(50), @FileName varchar(20)
SET @Path = 'C:\myfiles\'
Declare @files table(filenames varchar(255),depth int,isfile bit)
insert into @files
EXEC xp_dirtree  @path, 10, 1
Declare cur Cursor for
Select top 10 filenames from @files where isfile=1
open cur
fetch next from cur into @filename
while @@FETCH_STATUS=0
              BEGIN
                   
                            print @path + @filename

                     BEGIN TRY
                            print 'OK: ' + @FileName

                     END TRY

                     BEGIN CATCH

                            print ' '

                            print 'Error: ' + @FileName

                      END CATCH
fetch next from cur into @filename
              END

			  close cur
			  deallocate cur


  • Edited by pIdrA 7 hours 24 minutes ago
August 27th, 2015 7:46pm


DECLARE @Path varchar(50), @FileName varchar(20)
SET @Path = 'C:\myfiles\'
Declare @files table(filenames varchar(255),depth int,isfile bit)
insert into @files
EXEC xp_dirtree  @path, 10, 1
Declare cur Cursor for
Select top 10 filenames from @files where isfile=1
open cur
fetch next from cur into @filename
while @@FETCH_STATUS=0
              BEGIN
                   
                            print @path + @filename

                     BEGIN TRY
                            print 'OK: ' + @FileName

                     END TRY

                     BEGIN CATCH

                            print ' '

                            print 'Error: ' + @FileName

                      END CATCH
fetch next from cur into @filename
              END

			  close cur
			  deallocate cur


  • Edited by Idr_p Thursday, August 27, 2015 11:43 PM
  • Marked as answer by bassmann2 6 hours 19 minutes ago
Free Windows Admin Tool Kit Click here and download it now
August 27th, 2015 11:42pm


DECLARE @Path varchar(50), @FileName varchar(20)
SET @Path = 'C:\myfiles\'
Declare @files table(filenames varchar(255),depth int,isfile bit)
insert into @files
EXEC xp_dirtree  @path, 10, 1
Declare cur Cursor for
Select top 10 filenames from @files where isfile=1
open cur
fetch next from cur into @filename
while @@FETCH_STATUS=0
              BEGIN
                   
                            print @path + @filename

                     BEGIN TRY
                            print 'OK: ' + @FileName

                     END TRY

                     BEGIN CATCH

                            print ' '

                            print 'Error: ' + @FileName

                      END CATCH
fetch next from cur into @filename
              END

			  close cur
			  deallocate cur


  • Edited by Idr_p Thursday, August 27, 2015 11:43 PM
  • Marked as answer by bassmann2 Saturday, August 29, 2015 12:49 AM
August 27th, 2015 11:42pm

Thanks all, I've marked both response as correct. I went with Idr_p's suggestion in the end. I haven't typically been in the habit of using temp tables as solutions but to be honest don't have a great understanding of this approach.

Off the top of my head, would I be correct that Idr_p's approach would work in memory and the temp options need to write to disk? If so, does working in memory have a performance boost?

Thanks,

Bassmann

Free Windows Admin Tool Kit Click here and download it now
August 28th, 2015 8:54pm

Hi,

>> I've marked both response as correct

That is great, this is the right way in my opinion if a question get several right responses. Unlike a chat, a forum thread is here to stay, and future users (especially those that come from searching engine) might see the responses and choose a different solution. Therefore all currect answer should be marked.

1. With that was said, I think that your solution is not a one that I recommends for most cases. There is no reason to do any looping in this case. SQL Server is a Tabular database, which based on SET, and we should avoid looping through records and working with it record by record, as much as we can!

2. You do not have to use the temporary table in order to avoid looping. In both links that posted the job is done in two steps: firstly we get the information, and secondly we use the data from step 1. You can avoid using temporary table and work in one step, but using the approach in the second link that I posted: create one dynamic query using "for xml" hint, and execute it directly.

>> Off the top of my head, would I be correct that Idr_p's approach would work in memory

basically SQL Server always work in memory. Even when we read data (select) or write it (insert/update) to table, which is on disk, SQL Server will firstly read the data to the memory and work in memory. Once in a while, SQL Server do a CHECKPOINT operation which writes the current in-memory modified pages from memory to disk.

The big disadvantage (which is one of the advantage as well, depending on the case) of using temporary table is that the code execute in two steps, therefore the SQL Server Query engine built an execution plan for each step separately which mean that this plan is optimized for the specific step and not for the entire process that we want to do. When you use inline code the SQL Server query engine can parse the entire text and build a combine execution plan which is optinize for the whole process. In order to chose what fit your need you need to examine it case by case.

** For more information regarding "Temp table", "Table-Valued Parameters", and "Table Variables" check this link:
https://www.simple-talk.com/sql/t-sql-programming/temporary-tables-in-sql-server/

** I recommend to search Google for: SQL Server temp table vs inlin

August 29th, 2015 6:19am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics