Folder.Files ใช้สำหรับอ่านรายชื่อไฟล์ทั้งหมดในโฟลเดอร์ที่กำหนด รวมถึงโฟลเดอร์ย่อยด้วย คืนค่าเป็น table ที่มีข้อมูลไฟล์เช่น ชื่อ วันที่สร้าง ขนาด และลิงก์เข้าถึงไฟล์
=Folder.Files(path as text, optional options as nullable record) as table
=Folder.Files(path as text, optional options as nullable record) as table
| Argument | Type | Required | Default | Description |
|---|---|---|---|---|
| path | text | Yes | เส้นทางไปยังโฟลเดอร์ที่ต้องการอ่านไฟล์ ต้องเป็น absolute path เช่น “C:\Users\Data\Files” หรือ “\\server\share\folder” | |
| options | nullable record | Optional | null | ตัวเลือกเพิ่มเติม (ปกติไม่ใช้) ใช้สำหรับการตั้งค่าเพิ่มเติมของ function |
Folder.Files("C:\Users\MyUser\Documents\Reports")= Folder.Files("C:\Users\MyUser\Documents\Reports")
Table ที่มีคอลัมน์: Name, Folder Path, Attributes, Date Accessed, Date Created, Date Modified, Size, Content (สำหรับไฟล์ทั้งหมด)
let AllFiles = Folder.Files("C:\Data\Reports"), ExcelOnly = Table.SelectRows(AllFiles, each Text.EndsWith([Name], ".xlsx") or Text.EndsWith([Name], ".xls")) in…let
AllFiles = Folder.Files("C:\Data\Reports"),
ExcelOnly = Table.SelectRows(AllFiles, each Text.EndsWith([Name], ".xlsx") or Text.EndsWith([Name], ".xls"))
in
ExcelOnly
Table ที่เหลือแต่ไฟล์ Excel (.xlsx, .xls) เท่านั้น
let AllFiles = Folder.Files("C:\Data\Imports"), CSVFiles = Table.SelectRows(AllFiles, each Text.EndsWith([Name], ".csv")), ImportedData = Table.AddColumn(CSVFil…let
AllFiles = Folder.Files("C:\Data\Imports"),
CSVFiles = Table.SelectRows(AllFiles, each Text.EndsWith([Name], ".csv")),
ImportedData = Table.AddColumn(CSVFiles, "Data", each Csv.Document([Content], [Delimiter=","]))
in
ImportedData
Table ของไฟล์ CSV ทั้งหมดพร้อมกับข้อมูลที่ import มาจากแต่ละไฟล์
let AllFiles = Folder.Files("C:\Documents"), Today = DateTime.LocalNow(), RecentFiles = Table.SelectRows(AllFiles, each [Date Created] >= DateTime.AddDays(Today…let
AllFiles = Folder.Files("C:\Documents"),
Today = DateTime.LocalNow(),
RecentFiles = Table.SelectRows(AllFiles, each [Date Created] >= DateTime.AddDays(Today, -7))
in
RecentFiles
Table ของไฟล์ที่สร้างมาเพียง 7 วันที่ผ่านมา
let AllFiles = Folder.Files("C:\Sales\Monthly"), ExcelFiles = Table.SelectRows(AllFiles, each Text.EndsWith([Name], ".xlsx")), LoadedSheets = Table.AddColumn(Ex…let
AllFiles = Folder.Files("C:\Sales\Monthly"),
ExcelFiles = Table.SelectRows(AllFiles, each Text.EndsWith([Name], ".xlsx")),
LoadedSheets = Table.AddColumn(ExcelFiles, "Sheet1", each Excel.Workbook([Content], true){0}[Data]),
ExpandedData = Table.ExpandTableColumn(LoadedSheets, "Sheet1", Table.ColumnNames(LoadedSheets{0}[Sheet1])),
Consolidated = Table.Combine(Table.ToList(ExpandedData[[Sheet1]]))
in
Consolidated
Table ที่ combine ข้อมูลจากไฟล์ Excel ทั้งหมดในโฟลเดอร์
ใช่ ค้นหาแบบ recursive ไป ตามหาไฟล์ในโฟลเดอร์ย่อยทั้งหมดด้วย ถ้าต้องการแค่โฟลเดอร์ระดับแรก ต้อง filter จาก [Folder Path] column เอง
[Content] column จะมีข้อมูล binary ของไฟล์ ใช้ร่วมกับ Csv.Document(), Excel.Workbook(), Json.Document() ฯลฯ เพื่อ parse เนื้อหา
ได้ table ว่างๆ มา (empty table with columns) ไม่มี error
ได้ ใช้ได้กับ UNC path เช่น “\\\\server\\share\\folder” อย่าลืม escape backslash ให้ถูกต้อง
ได้ error กลับมา เช่น “Folder not found” ต้องใช้ try-catch หรือตรวจสอบ path ก่อน
Folder.Files ใช้สำหรับดึงรายชื่อไฟล์ทั้งหมดที่อยู่ในโฟลเดอร์ที่ระบุ โดยจะค้นหาแบบซ้ำได้ (recursive) เข้าไปในโฟลเดอร์ย่อยด้วย
ที่เจ๋งคือ function นี้ใช้ได้กับ path ใดๆ ก็ได้ ไม่ว่าจะ Windows หรือ network path ก็ยังใช้ได้ ผลลัพธ์จะเป็น table ที่มีคอลัมน์อย่างเช่น Name, Folder Path, Attributes, Date Accessed, Date Created, Date Modified, Size และ Content ซึ่ง Content column มีลิงก์ให้เรา binary file ได้
ส่วนตัวผม ใช้ Folder.Files เวลาต้อง consolidate ไฟล์หลายๆ ไฟล์เข้าด้วยกัน เช่น อ่านไฟล์ Excel ทั้งหมดในโฟลเดอร์ แล้ว loop ผ่านแต่ละไฟล์เพื่อ combine ข้อมูล ประหยัดเวลามากเลย ไม่ต้องไป load ทีละไฟล์ 😎