使用 SQL 服务器的当前文件夹中是否存在文件?

Does file exist in current Folder using SQL Server?

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'), ('\basketballfolder17_Images'),
        ('\basketballfolder17_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

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

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

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

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

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

我正在使用 SQL Server 2017。

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

请将"répertoire de"替换为英文翻译"folder of ",我使用的是Windows

的法文版
**

--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 

**