SQL Server xp_cmdshell

后端 未结 4 1495
慢半拍i
慢半拍i 2021-01-26 04:33

Is there any other way to get a list of file names via T-SQL other than

INSERT INTO @backups(filename)
EXEC master.sys.xp_cmdshell \'DIR /b c:\\som         


        
4条回答
  •  自闭症患者
    2021-01-26 05:07

    I do realize that this thread is 5 years old but I thought I'd post another non-xpCmdShell, non-CLR alternative. Details are in the following code and it's pretty simple stuff.

    --===== Define the path and populate it.
         -- This could be a parameter in a proc
    DECLARE @pPath VARCHAR(512);
     SELECT @pPath = 'C:\Temp';
    
    --===== Create a table to store the directory information in
     CREATE TABLE #DIR
            (
            RowNum     INT IDENTITY(1,1),
            ObjectName VARCHAR(512),
            Depth      TINYINT,
            IsFile     BIT,
            Extension AS RIGHT(ObjectName,CHARINDEX('.',REVERSE(ObjectName))) PERSISTED
            )
    ;
    --===== Get the directory information and remember it
     INSERT INTO #DIR
            (ObjectName,Depth,IsFile)
       EXEC xp_DirTree 'C:\Temp',1,1
    ;
    --===== Now do whatever it is you need to do with it
     SELECT * FROM #DIR;
    

提交回复
热议问题