I have written this script to get the names of files, their dates and the directories they came from, I need this to find out if any changes have been made to any of the files.It all works Ok up to the last part where I try to write the files to the table 'Directory Contents' where i only seem to be getting the last directory name and the last file found in that directory?Can someone pleas advise as to where I am going wrong?Thanks.--create the base tablesCREATE TABLE [dbo].[Directory_Contents_Stage] ( [dir_output] varchar(255))GOCREATE TABLE [dbo].[Directory_Contents_Stage2] ( [dir_output] varchar(255))GOCREATE TABLE [dbo].[Directory_Contents_Directories] ( [dir_name] varchar(255) , [dir_date] datetime)GOCREATE TABLE [dbo].[Directory_Contents] ( [dirname] varchar(255) , [File_Name] varchar(255) , [Create_Time] datetime , [File_Size] int)GO--set the pathDECLARE @basepath varchar(40)SET @basepath = 'dir C:\drivers\'INSERT INTO Directory_Contents_Stage(dir_output) EXEC master..xp_cmdshell @basepathgo--first get all directory names (except where they = . or .. )INSERT INTO Directory_Contents_Directories ([dir_name], [dir_date]) SELECT SUBSTRING(dir_output,37,(LEN(dir_output)-36)) AS [dir_name] , CONVERT(datetime,SUBSTRING(dir_output,1,17),103) AS [dir_date] FROM Directory_Contents_Stage WHERE (SUBSTRING(dir_output,1,1) <> ' ' AND (IsNumeric(SUBSTRING(dir_output, 1, 2))=1) AND (IsNumeric(SUBSTRING(dir_output, 16, 1))=1) AND SUBSTRING(dir_output,37,1) <> '.' AND SUBSTRING(dir_output,38,1) <> '.' AND SUBSTRING(dir_output,22,5) = '<DIR>')go--now use all the directories above as a new path to searchDECLARE @dirnm varchar(40)DECLARE @dirnm2 varchar(40)--for each @dirnm in (SELECT [dir_name] FROM Directory_Contents_Directories)DECLARE dirname_cursor CURSOR FORSELECT dir_name FROM Directory_Contents_DirectoriesOPEN dirname_cursor-- Perform the first fetch.FETCH NEXT FROM dirname_cursorINTO @dirnm-- Check @@FETCH_STATUS to see if there are any more rows to fetch.WHILE @@FETCH_STATUS = 0BEGINSET @dirnm2 = 'dir C:\drivers\' + @dirnmINSERT INTO Directory_Contents_Stage2 (dir_output) EXEC master..xp_cmdshell @dirnm2 -- This is executed as long as the previous fetch succeeds. FETCH NEXT FROM dirname_cursor INTO @dirnmENDCLOSE dirname_cursorDEALLOCATE dirname_cursorGO--------------- THIS PART NOT WORKING PROPERLY! ---------------now all filenames from all directories found within @basepath have been copied--to the table Directory_Contents_Stage2, so parse further.-- [dirname] varchar(255)-- [File_Name] varchar(255)-- [Create_Time] datetime-- [File_Size] intDECLARE @dirlongnm varchar(400)DECLARE @dirlongnm2 varchar(4000)DECLARE @dirlongnm1 varchar(12)DECLARE @dirlongnm1a varchar(400)DECLARE @dirnma varchar(12)DECLARE @numfiles intDECLARE @fileflag int SET @dirlongnm1 = 'Directory of'SET @fileflag = 0SET @dirlongnm1a = '?'DECLARE dirname2_cursor CURSOR FORSELECT dir_output FROM Directory_Contents_Stage2OPEN dirname2_cursor-- Perform the first fetch.FETCH NEXT FROM dirname2_cursorINTO @dirlongnm2----WHILE @@FETCH_STATUS = 0BEGIN--get the part of the string that may read 'Directory of' as we need this to know--where the files came from.SET @dirnma = SUBSTRING(@dirlongnm2,2,13)--check to see if it = 'Directory of' IF (@dirnma = @dirlongnm1) BEGIN -- it does, so get the directory name SET @dirlongnm = SUBSTRING(@dirlongnm2,14,(LEN(@dirlongnm2)-12)) END --check the string we are reading out is correct format IF (SUBSTRING(@dirlongnm2,1,1) <> ' ' AND IsNumeric(SUBSTRING(@dirlongnm2, 1, 2))=1 AND IsNumeric(SUBSTRING(@dirlongnm2, 16, 1))=1 AND SUBSTRING(@dirlongnm2,22,5) <> '<DIR>') BEGIN --string is OK to parse and write to table --create some dummy values to overwrite, one for each entry found. INSERT Directory_Contents VALUES ('?', '??',getdate(),0) --write the parts of the string to the fields of the table UPDATE Directory_Contents SET [File_Name] = SUBSTRING(@dirlongnm2,37,(LEN(@dirlongnm2)-36)), [Create_Time] = CONVERT(datetime,SUBSTRING(@dirlongnm2,1,17),103), [dirname] = @dirlongnm --[File_Size] = LTRIM(SUBSTRING(@dirlongnm2,19,36)) END -- This is executed as long as the previous fetch succeeds. FETCH NEXT FROM dirname2_cursor INTO @dirlongnm2 ENDGOCLOSE dirname2_cursorDEALLOCATE dirname2_cursorGO--select * from Directory_Contents_Directories--select * from Directory_Contents_Stageselect * from Directory_Contents_Stage2select * from Directory_Contents