December 11, 2014 at 12:37 am
Jeff, thank you for pointing perfomance issue out. I would also note that it would be so simple for MS to add parent reference to the sp_dirtree output thus eliminating half of the problem.
December 11, 2014 at 7:46 am
Still, it's a brilliantly simple solution and it does solve a very common problem without a trip to the Command Prompt. Well done, again!
And, yeah, I agree. How difficult would it have been for them to include the full parent path, the size, and the 3 dates associated with each file? Not everyone in the world wants to use SSIS for ETL. 😀
--Jeff Moden
Change is inevitable... Change for the better is not.
June 21, 2017 at 3:11 am
serg-52 - Monday, December 8, 2014 4:55 AMThis batch will show all objects (directories, files) and their full paths--parameterdeclare @myPath nvarchar(4000) = 'E:\ElectronicArchieve';IF OBJECT_ID('tempdb..#DirectoryTree') IS NOT NULLDROP TABLE #DirectoryTree;CREATE TABLE #DirectoryTree (id int IDENTITY(1,1),subdirectory nvarchar(512),depth int,isfile bit, ParentDirectory int,flag tinyint default(0));-- top level directoryINSERT #DirectoryTree (subdirectory,depth,isfile)VALUES (@myPath,0,0);-- all the rest under top levelINSERT #DirectoryTree (subdirectory,depth,isfile)EXEC master.sys.xp_dirtree @myPath,0,1;UPDATE #DirectoryTreeSET ParentDirectory = (SELECT MAX(Id) FROM #DirectoryTreeWHERE Depth = d.Depth - 1 AND Id < d.Id)FROM #DirectoryTree d;-- SEE all with full pathsWITH dirs AS ( SELECT Id,subdirectory,depth,isfile,ParentDirectory,flag , CAST (null AS NVARCHAR(MAX)) AS container , CAST([subdirectory] AS NVARCHAR(MAX)) AS dpath FROM #DirectoryTree WHERE ParentDirectory IS NULL UNION ALL SELECT d.Id,d.subdirectory,d.depth,d.isfile,d.ParentDirectory,d.flag , dpath as container , dpath +'\'+d.[subdirectory] FROM #DirectoryTree AS d INNER JOIN dirs ON d.ParentDirectory = dirs.id)SELECT * FROM dirs -- Dir style orderingORDER BY container, isfile, subdirectory
hope it helps.
Hi serg-52, just asking if you could point me in the right direction with the code you posted for the above thread. I've managed to use the code and create a list of files in shared drive with associated paths and works great thanks. What I need is to set up in SQL is a table that is constantly updated and reflects any new files added to the shared area [immediately]. I am not sure how to take your code [with it being a batch of commands] to do this in the most effective / efficient way; stored procedure, view, in the master DB or the usual database I use, if it can be done / bad idea? Apologies I am only use to creating Tables / views and read / writing from excel [vba]. I have a document management system in excel that reads the directory using VBA but it takes too long to update, and your solution is very fast. Many thanks in anticipation. Andy Johnston
June 21, 2017 at 2:33 pm
andy.johnston - Wednesday, June 21, 2017 3:11 AMHi serg-52, just asking if you could point me in the right direction with the code you posted for the above thread. I've managed to use the code and create a list of files in shared drive with associated paths and works great thanks. What I need is to set up in SQL is a table that is constantly updated and reflects any new files added to the shared area [immediately]. I am not sure how to take your code [with it being a batch of commands] to do this in the most effective / efficient way; stored procedure, view, in the master DB or the usual database I use, if it can be done / bad idea? Apologies I am only use to creating Tables / views and read / writing from excel [vba]. I have a document management system in excel that reads the directory using VBA but it takes too long to update, and your solution is very fast. Many thanks in anticipation. Andy Johnston
Andy, your problem is known as a Folder Watcher and it's rather operating system related. Sql Server has no tools to watch file system activity.
See this http://devproconnections.com/net-framework/how-build-folder-watcher-service-c for c# example, this could be really fast and the table in question can be updated in real-time . Alternatively you can schedule the above script with Sql Server Agent to run say every 10 minutes, provided 10 min delay is OK. See https://docs.microsoft.com/en-us/sql/ssms/agent/schedule-a-job.
June 21, 2017 at 6:26 pm
serg-52 - Wednesday, June 21, 2017 2:33 PMandy.johnston - Wednesday, June 21, 2017 3:11 AMHi serg-52, just asking if you could point me in the right direction with the code you posted for the above thread. I've managed to use the code and create a list of files in shared drive with associated paths and works great thanks. What I need is to set up in SQL is a table that is constantly updated and reflects any new files added to the shared area [immediately]. I am not sure how to take your code [with it being a batch of commands] to do this in the most effective / efficient way; stored procedure, view, in the master DB or the usual database I use, if it can be done / bad idea? Apologies I am only use to creating Tables / views and read / writing from excel [vba]. I have a document management system in excel that reads the directory using VBA but it takes too long to update, and your solution is very fast. Many thanks in anticipation. Andy Johnston
Andy, your problem is known as a Folder Watcher and it's rather operating system related. Sql Server has no tools to watch file system activity.
See this http://devproconnections.com/net-framework/how-build-folder-watcher-service-c for c# example, this could be really fast and the table in question can be updated in real-time . Alternatively you can schedule the above script with Sql Server Agent to run say every 10 minutes, provided 10 min delay is OK. See https://docs.microsoft.com/en-us/sql/ssms/agent/schedule-a-job.
It sure does look like this was made simpler in .NET than it used to be. I wrote one years ago in VB 5 and had to use the Win32 API for nearly everything that mattered. I think it's good that they're exposing more OS functionality through libraries. I guess the only downside is that we don't need to have as thorough of an understanding to make it work. On the other side of the coin, we can't commit so many errors. 😛
June 22, 2017 at 2:40 am
Many thanks for pointing me in the right direction and helping me solved my problem.
Solution I used: Created an "SQL Stored Procedure" to update a table [not temp table] with the basic file information in the database. Then created an "SQL View" that used this data to add / created the path and path with file name. Then triggered the "SQL Stored procedure" from Excel VBA; user can updated when they want / on certain events in the spread sheet. ODBC link to the "SQL View" to keep the spread sheet up to date with the files in the shared folder.
This now updates the file list in about 4-5 seconds as opposed to 100 seconds+ using the original VBA.
Used your code as a great starting point serge-52. Will have a look at the Folder-Watcher option going forward; but may be a bit too "techy" for me.
Thanks again. Andy
Viewing 6 posts - 16 through 20 (of 20 total)
You must be logged in to reply to this topic. Login to reply