How can I delete files which already exists in a database table as Filename. Example On Drive C:\Data there are 100 Word documents and 70 of these documents will be found in the database DMS.Filename. If directory.filename=table.filename then the File should be deleted. In this case we have to delete 70 Word documents. The procedure should run as daily task an check new files against the database. How can I check and delete the files ?
Here new code: you can't delete in cmd Files with space or blanks in filename. I think this the msg what i get.
Could Not Find C:\Data\Integration
Could Not Find C:\Windows\system32\Lettre
DECLARE @image_files TABLE (file_path VARCHAR(MAX))
DECLARE @file_path VARCHAR(MAX), @cmd VARCHAR(MAX)
INSERT INTO @image_files (file_path)
EXEC xp_cmdshell 'C:\Data\*.doc /b /s /x'
DECLARE file_cursor CURSOR FOR
SELECT file_path FROM @image_files
WHERE file_path IN
(
select 'C:\Data\' + QC_DESCRIPTION +'.doc' from tbl_France where QC_DESCRIPTION is not null
)
OPEN file_cursor
FETCH NEXT FROM file_cursor INTO @file_path
WHILE (@@FETCH_STATUS = 0)
BEGIN
SET @cmd = 'EXEC xp_cmdshell ''del ' + @file_path + ''''
EXEC(@cmd)
FETCH NEXT FROM file_cursor INTO @file_path
END
CLOSE file_cursor
DEALLOCATE file_cursor