当前文件夹中是否存在使用SQL Server的文件?

当前文件夹中是否存在使用SQL Server的文件?,sql,sql-server,tsql,file-exists,database-cursor,Sql,Sql Server,Tsql,File Exists,Database Cursor,我正在创建一个存储过程,它将检查作为参数传入的文件是否位于其中一个硬编码文件夹中 例如,如果传入一个名为test2019.txt的文件,则存储过程应检查该文件是否存在于文件夹中。如果是,则返回true并返回文件路径 因此,本质上我只想检查当前目录中是否存在文件,如果是,请返回完整路径 现在我可以使用光标动态获取文件夹路径。现在只需要一种方法来检查文件是否存在于文件夹路径中,并返回完整路径 请看代码。我希望这是有道理的。谢谢你的帮助 我正在使用SQL Server 2017 我有另一个解决方案给你

我正在创建一个存储过程,它将检查作为参数传入的文件是否位于其中一个硬编码文件夹中

例如,如果传入一个名为test2019.txt的文件,则存储过程应检查该文件是否存在于文件夹中。如果是,则返回true并返回文件路径

因此,本质上我只想检查当前目录中是否存在文件,如果是,请返回完整路径

现在我可以使用光标动态获取文件夹路径。现在只需要一种方法来检查文件是否存在于文件夹路径中,并返回完整路径

请看代码。我希望这是有道理的。谢谢你的帮助


我正在使用SQL Server 2017

我有另一个解决方案给你 它使用xp_cmdshell命令在给定文件夹中检索并存储所有文件及其完整路径

请将répertoire de替换为的英文翻译文件夹,我使用的是Windows的法文版
**

三个字母。。。。。在纯t-sql中执行此操作既痛苦又缓慢。为什么首先要使用sql Server?当然,在应用程序中这样做将是一种更简单、更好的方法。XY问题?谢谢你的反馈。CLR听起来是个好主意。由于所有者的请求,我无法在应用程序中构建它。查看xp\u fileexisti正在寻找一种与xp\u fileexist不同的解决方案,尽管它确实有效,但正在寻找更具动态性的解决方案。如果没有,我会用这个谢谢,这是工作。必须调整“like”以接受变量,但效果很好。谢谢你的帮助!
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
**

--Kamel Gazzah
    --19/03/2019
    --Script to retrieve all the files in a a folder, inside all the sub 
     directoris

declare @folder as varchar(100)
-----------------------------------------
set @folder='d:\'
-----------------------------------------
declare @script as varchar(2000)
set @script='exec master..xp_cmdshell "dir '+@folder+'  /N /s"'
declare @mytab as table(id int identity(1,1),date_time datetime,folder int,filename varchar(1000),parent_folder varchar(200))
insert into @mytab(filename) exec(@script)
update @mytab set date_time= substring(filename,1,18) where date_time is null and isdate(substring(filename,1,18))=1
update @mytab set folder=1 where filename like '%répertoire de%' and folder is null
update @mytab set folder=0 where filename not like '%<DIR>%' and folder is null and date_time is not null
update @mytab set filename=replace(filename,'répertoire de ','') where folder=1
delete from @mytab where folder is null
update @mytab set parent_folder=t2.filename
--select t1.id,t1.folder,t1.filename,t2.filename
 from @mytab t1
outer apply (select top 1 filename from @mytab where id<t1.id  and folder=1 order by id desc) t2
where t1.folder=0
UPDATE @mytab SET FILENAME=substring(filename,37,len(filename)) WHERE FOLDER=0 
select id,replace(replace(parent_folder,'\',''),':',':\')+'\'+filename [Fullpath] from @mytab where folder=0