- [DEVEXPRESS] Hướng dẫn bật tính năng Scroll Pixcel in Touch trên GridView
- [DEVEXPRESS] Hướng dẫn sử dụng TileBar viết ứng dụng duyệt hình ảnh Winform
- [DEVEXPRESS] Tô màu border TextEdit trên Winform
- [C#] Lấy dữ liệu từ Console Write hiển thị lên textbox Winform
- [C#] Hiển thị Progress bar trên Window Console
- [C#] Di chuyển control Runtime và lưu layout trên winform
- [SQLSERVER] Sử dụng hàm NULL IF
- [C#] Chia sẽ source code mã đi tuần bằng giao diện Winform
- [C#] Flash Window in Taskbar Winform
- Download và Giải nén tập tin File sử dụng Powershell
- [C#] Hướng dẫn cách lấy thông tin đăng nhập tài khoản và mật khẩu web browser trên windows
- [VB.NET] CRUD Thêm xóa sửa tìm kiếm Realtime FireBase
- [C#] Hiển thị thông báo Toast Message trong lập trình Winform
- [C#] Cấu hình định dạng ngày tháng, thời gian trên Windows cho ứng dụng Winform
- [C#] Rút gọn đường dẫn link url với TinyURL API
- [C#] Hướng dẫn cách bo tròn winform with Radius
- [C#] Chia sẽ class BackGroundOverlay Show Modal cho Winform
- [C#] Hướng dẫn Flip Image Winform
- [C#] Invoke là gì? cách sử dụng phương thức Invoke()
- [C#] Hướng dẫn chia sẽ file, folder từ ứng dụng sang Zalo Chat
[SQLSERVER] Sử dụng Store Procedure xp_dirtree để duyệt thư mục và tập tin trên hệ thống
Xin chào các bạn bài viết hôm nay mình sẽ hướng dẫn các bạn sử dụng Store procedure Xp_dirtree trong sqlserver để liệt kê thư mục và tập tin trong hệ thống.
[SQLSERVER] Sử dụng Store Procedure xp_dirtree liệt kê thư mục và tập tin
Cú pháp sử dụng xp_dirtree sql:
DECLARE @folderpath nvarchar(4000) = 'C:\Program Files\Microsoft SQL Server\'
EXEC master..xp_dirtree @folderpath
-- OR EXEC master..xp_dirtree 'C:\Program Files\Microsoft SQL Server\'
Kết quả khi thực hiện:
Tiếp theo mình sẽ chia sẽ cho các bạn đoạn script sql, để khi truy vấn chúng ta sẽ biết được tập tin này thuộc thư mục nào.
Qua hai trường: Id
và parentID
DECLARE @myPath VARCHAR(1000) = 'F:\xampp\htdocs\HOBWEB\IMG_loi_hangbosung'
DECLARE @DirectoryTree as TABLE (
id int IDENTITY(1,1)
,subdirectory nvarchar(512)
,depth int
,isfile bit
, ParentDirectory int
,flag tinyint default(0));
-- top level directory
INSERT @DirectoryTree (subdirectory,depth,isfile)
VALUES (@myPath,0,0);
-- all the rest under top level
INSERT INTO @DirectoryTree (subdirectory,depth,isfile)
EXEC master.sys.xp_dirtree @myPath,0,1
UPDATE @DirectoryTree
SET ParentDirectory = (
SELECT MAX(id) FROM @DirectoryTree
WHERE Depth = d.Depth - 1 AND Id < d.Id )
FROM @DirectoryTree d;
-- SEE all with full paths
WITH dirs AS (
SELECT
Id,subdirectory,depth,isfile,ParentDirectory,flag
, CAST (null AS NVARCHAR(MAX)) AS container
, CAST([subdirectory] AS NVARCHAR(MAX)) AS dpath
FROM @DirectoryTree
WHERE ParentDirectory IS NULL
UNION ALL
SELECT
d.Id,d.subdirectory,d.depth,d.isfile,d.ParentDirectory,d.flag
, dpath as container
, dpath +'\'+d.[subdirectory]
FROM @DirectoryTree AS d
INNER JOIN dirs ON d.ParentDirectory = dirs.id
)
SELECT * FROM dirs
Các bạn, có thể truyền đường dẫn folder của các bạn muốn liệt kê qua tham số @myPath.
Kết quả khi chúng ta chạy câu lệnh trên:
Thank for watching!