设为首页收藏本站

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 1008|回复: 0

Use T-SQL to list all files in a directory

[复制链接]
发表于 2012-5-18 13:29:42 | 显示全部楼层 |阅读模式
本帖最后由 demo 于 2012-5-19 05:40 编辑

Execute the following T-SQL example scripts in Microsoft SQL Server Management Studio Query Editor to list all files and all directories in a directory using xp_cmdshell extended system stored procedure.
              Warning article:
              What   are the security risks using cmdexec?
  1. -- List all files in a directory - T-SQL parse string for date and filename
  2. -- Microsoft SQL Server command shell statement - xp_cmdshell
  3. DECLARE  @PathName       VARCHAR(256) ,
  4.          @CMD            VARCHAR(512)

  5. CREATE TABLE #CommandShell ( Line VARCHAR(512))

  6. SET @PathName = 'F:\data\download\microsoft\'

  7. SET @CMD = 'DIR ' + @PathName + ' /TC'

  8. PRINT @CMD -- test & debug
  9. -- DIR F:\data\download\microsoft /TC

  10. -- MSSQL insert exec - insert table from stored procedure execution
  11. INSERT INTO #CommandShell
  12. EXEC MASTER..xp_cmdshell   @CMD

  13. -- Delete lines not containing filename
  14. DELETE
  15. FROM   #CommandShell
  16. WHERE  Line NOT LIKE '[0-9][0-9]/[0-9][0-9]/[0-9][0-9][0-9][0-9] %'
  17. OR Line LIKE '%<DIR>%'
  18. OR Line is null  

  19. -- SQL reverse string function - charindex string function

  20. SELECT
  21.   FileName = REVERSE( LEFT(REVERSE(Line),CHARINDEX(' ',REVERSE(line))-1 ) ),
  22.   CreateDate = LEFT(Line,10)
  23. FROM #CommandShell

  24. ORDER BY FileName

  25. /* Results
  26. FileName                            CreateDate

  27. AdventureWorks2008.pdf              09/07/2018

  28. AdventureWorks2008_Conceptual.pdf   09/07/2018

  29. AdventureWorksDW.pdf                09/07/2018

  30. AdventureWorksDW2008.pdf            09/07/2018

  31. microsoft.lnk                       03/03/2019

  32. */
  33. DROP TABLE #CommandShell
  34. GO

  35. ------------
  36. -- Get file list in specified directory (folder)

  37. -- T-SQL command shell - insert exec

  38. CREATE TABLE #FileList (

  39.   Line VARCHAR(512))

  40. DECLARE @Path varchar(256) = 'dir f:\data\'

  41. DECLARE @Command varchar(1024) =  @Path+' /A-D  /B'

  42. PRINT @Command

  43. INSERT #FileList

  44. EXEC MASTER.dbo.xp_cmdshell @Command

  45. DELETE #FileList WHERE  Line IS NULL
  46. SELECT * FROM   #FileList

  47. GO

  48. DROP TABLE #FileList

  49. GO
  50. -- Get directory (subdirectory, folder) list in specified directory (folder)

  51. CREATE TABLE #DirectoryList (

  52.   Line VARCHAR(512))

  53. DECLARE @Path varchar(256) = 'dir f:\data\'

  54. DECLARE @Command varchar(1024) =  @Path+' /A-A  /B'

  55. PRINT @Command

  56. INSERT #DirectoryList

  57. EXEC MASTER.dbo.xp_cmdshell @Command

  58. DELETE #DirectoryList WHERE  Line IS NULL
  59. SELECT * FROM   #DirectoryList

  60. GO

  61. DROP TABLE #DirectoryList

  62. GO

  63. ------------
复制代码



Related articles:



您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|BC Morning Website ( Best Deal Inc. 001 )  

GMT-8, 2025-12-12 06:53 , Processed in 0.013556 second(s), 16 queries .

Supported by Best Deal Online X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表