brianparow (9/19/2010)
What I'm trying to do is grab the filenames from a directory and see if there are 2 files with the same name and different extensions..ie:
123.txt and 123.pdf
I want it to check and find all txt files that do not have a corresponding pdf file and output those txt filenames to the results of the query.
Heh... jeez Brian... why didn't you just say so at the beginning??? Problem solved... read the comments in the code that follows
--===== Conditionally drop temp tables to make reruns easier
IF OBJECT_ID('TempDB..#FileList','U') IS NOT NULL
DROP TABLE #FileList
--===== Create the working temp table with a couple of calculated columns to isolate the extention
-- and just the file name.
CREATE TABLE #FileList
(
FullFileName SYSNAME,
Level TINYINT,
IsFile TINYINT,
FileName AS SUBSTRING(FullFileName,1,LEN(FullFileName)-CHARINDEX('.',REVERSE(FullFileName))),
Extension AS RIGHT(FullFileName,CHARINDEX('.',REVERSE(FullFileName)))
)
--===== Populate the table with file names from the given directory or UNC
-- (YES, you CAN convert this section to dynamic SQL if you need to)
INSERT INTO #FileList
(FullFileName, Level, IsFile)
EXEC xp_DirTree 'C:\Temp',1,1 --<<<<Change to the desired directory here
--===== Ok... let see what that has in it. Not part of the problem but thought you should see it.
SELECT * FROM #FileList
--===== And finally, list all filenames that have a .txt extension but DON'T have a .PDF extension
SELECT FileName FROM #FileList WHERE Extension = '.txt' AND IsFile = 1
EXCEPT
SELECT FileName FROM #FileList WHERE Extension = '.pdf' AND IsFile = 1
--Jeff Moden
Change is inevitable... Change for the better is not.