CREATE PROCEDURE SearchFile_InAllDirectories
@SearchFile VARCHAR(100)
DECLARE @BasePath VARCHAR(1000),
@Path VARCHAR(1000),
@FullPath VARCHAR(2000),
@Id INT;
SET @SearchFile = 'test2019.txt'
CREATE TABLE tmp_BasePath
(
basePath VARCHAR(100)
);
INSERT INTO tmp_BasePath (basePath)
VALUES ('\\Path1'), ('\\Path1\Images_5'),
('\\Path3\Images_4'), ('\\basketballfolder\2017_Images'),
('\\basketballfolder\2017_Images')
CREATE TABLE tmp_DirectoryTree
(
id INT IDENTITY(1,1),
subdirectory VARCHAR(512),
depth INT,
isfile BIT,
fullpath VARCHAR(500)
);
DECLARE basePath_results CURSOR FOR
SELECT bp.basePath
OPEN basePath_results
FETCH NEXT FROM basePath_results into @BasePath
WHILE @@FETCH_STATUS = 0
BEGIN
INSERT INTO tmp_DirectoryTree (subdirectory, depth, isfile)
EXEC master.sys.xp_dirtree @BasePath, 0, 1;
FETCH NEXT FROM basePath_results INTO @Basepath
END
CLOSE basePath_results;
DEALLOCATE basePath_results;
END
I am creating a stored procedure that will check to see if the file passed in as a parameter, is located in one of the hard coded folders.
For example, if I pass in a file named "test2019.txt", the stored procedure should then check to see if that file exist in the folder. If yes, return true and return file path.
So essentially I just want to check if a file exist in current directory if yes give me back the full path.
Right now I am able to use a cursor to dynamically get the folder paths. Now just need a way to check to see if the file exist in the folder path, and return full path.
Please see code. I hope this makes sense. Thanks for help.
I am using SQL Server 2017.