List directory to a table
List directory to a table.
Last updated:
Warning: Review and test in a non-production environment before running.
declare @cmd varchar(2000),
@FileNameMask varchar(1000),
@FilePath varchar(1000)
set @FilePath = 'C:\'
set @FileNameMask = '*.txt'
select @cmd = 'dir /B ' + @FilePath + @FileNameMask
--drop table #zTmpDir
create table #zTmpDir(FN varchar(250))
insert #zTmpDir exec master..xp_cmdshell @cmd
delete #zTmpDir
where FN is null
or FN like '%not found%'
or FN like 'OLD'
select * from #zTmpDir