I created a table that lists all the files in a directory using xp_dirtree. Now how do I keep this table updated when files are added or removed from this directory?
Here's some sample code of my table. Feel free to suggest simpler code as well.
create table ListDirectory
(
id int IDENTITY(1,1) PRIMARY KEY,
aFileName char(255),
extension char(10),
aFile char(255),
depth int,
isFile bit
)
insert ListDirectory(aFile, depth, isFile)
EXEC xp_dirtree 'c:\folder', 10, 1
update ListDirectory
SET aFileName = REVERSE(SUBSTRING(REVERSE(aFile),
CHARINDEX('.', REVERSE(aFile)) + 1, 999))
update ListDirectory
SET extension = REVERSE(
left(REVERSE(aFile),
case when CHARINDEX('.', REVERSE(aFile) ) = 0 then LEN(REVERSE(aFile))
else CHARINDEX('.', REVERSE(aFile))-1 end)
)