MIS
MIS

Reputation: 1

How to loop through and get sub directories in a directory in SQL Server?

I am using Sql Server 2008. I can load the files in a directory; usning following code:

Set @Path = 'C:\Test\'
Set @param = 'dir /B ' + @Path + '*.txt'

What I want is to get the the files that are in the subdirectories of the Test directory. say Test/temp and the temp folder contains multiple folders say 1,2,3(each contining multiple files). I want to loop through all the folders in the temp and get the files in each of its subdirectories(1,2,3). Then load the files in Sql tables.

Kindly advice

Upvotes: 0

Views: 885

Answers (2)

Mikael Eriksson
Mikael Eriksson

Reputation: 138980

Use /S switch on dir to get content in subdirectories.

Upvotes: 1

Wouter Simons
Wouter Simons

Reputation: 2906

The best answer to this question is probably to not use SQL at all.

If you want to do something with subdirs you can experiment with tree /F, but as you will see that is harder to actually parse.

Perhaps you can try and look into a scripting language such as python or vbscript to help you out. You could scan the file, and perhaps even get more information about the files to put in your DB, like file size and last modified dates?

Upvotes: 1

Related Questions