Thep Excel

Folder.Files – อ่านไฟล์ทั้งหมดในโฟลเดอร์

Folder.Files ใช้สำหรับอ่านรายชื่อไฟล์ทั้งหมดในโฟลเดอร์ที่กำหนด รวมถึงโฟลเดอร์ย่อยด้วย คืนค่าเป็น table ที่มีข้อมูลไฟล์เช่น ชื่อ วันที่สร้าง ขนาด และลิงก์เข้าถึงไฟล์

=Folder.Files(path as text, optional options as nullable record) as table

By ThepExcel AI Agent
12 December 2025

Function Metrics


Popularity
8/10

Difficulty
3/10

Usefulness
8/10

Syntax & Arguments

=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

Examples

อ่านไฟล์ทั้งหมดในโฟลเดอร์
Folder.Files("C:\Users\MyUser\Documents\Reports")
ดึงรายชื่อและข้อมูลของไฟล์ทั้งหมดในโฟลเดอร์ Reports รวมถึงโฟลเดอร์ย่อย จะได้ table ที่มีข้อมูลแต่ละไฟล์
Power Query Formula:

= Folder.Files("C:\Users\MyUser\Documents\Reports")

Result:

Table ที่มีคอลัมน์: Name, Folder Path, Attributes, Date Accessed, Date Created, Date Modified, Size, Content (สำหรับไฟล์ทั้งหมด)

ใช้ let…in เพื่อ filter ไฟล์ Excel เท่านั้น
let AllFiles = Folder.Files("C:\Data\Reports"), ExcelOnly = Table.SelectRows(AllFiles, each Text.EndsWith([Name], ".xlsx") or Text.EndsWith([Name], ".xls")) in…
อ่านไฟล์ทั้งหมด แล้ว filter เอาแค่ไฟล์ที่ลงท้ายด้วย .xlsx หรือ .xls โดยตรวจสอบจาก Name column
Power Query Formula:

let
    AllFiles = Folder.Files("C:\Data\Reports"),
    ExcelOnly = Table.SelectRows(AllFiles, each Text.EndsWith([Name], ".xlsx") or Text.EndsWith([Name], ".xls"))
in
    ExcelOnly

Result:

Table ที่เหลือแต่ไฟล์ Excel (.xlsx, .xls) เท่านั้น

อ่านไฟล์ CSV และ combine เข้าด้วยกัน
let AllFiles = Folder.Files("C:\Data\Imports"), CSVFiles = Table.SelectRows(AllFiles, each Text.EndsWith([Name], ".csv")), ImportedData = Table.AddColumn(CSVFil…
ใช้ Folder.Files หาไฟล์ CSV ทั้งหมด แล้วสร้าง column ใหม่ที่มีข้อมูล CSV ที่ parse มาแล้ว จากนั้น expand ออกมาได้ตามความต้องการ
Power Query Formula:

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

Result:

Table ของไฟล์ CSV ทั้งหมดพร้อมกับข้อมูลที่ import มาจากแต่ละไฟล์

รับรูป่วงจริง: ค้นหาไฟล์ตามวันที่สร้าง
let AllFiles = Folder.Files("C:\Documents"), Today = DateTime.LocalNow(), RecentFiles = Table.SelectRows(AllFiles, each [Date Created] >= DateTime.AddDays(Today…
ใช้ Folder.Files อ่านไฟล์ทั้งหมด แล้ว filter เอาแค่ไฟล์ที่สร้างภายใน 7 วันที่ผ่านมา โดยเช็ค Date Created column
Power Query Formula:

let
    AllFiles = Folder.Files("C:\Documents"),
    Today = DateTime.LocalNow(),
    RecentFiles = Table.SelectRows(AllFiles, each [Date Created] >= DateTime.AddDays(Today, -7))
in
    RecentFiles

Result:

Table ของไฟล์ที่สร้างมาเพียง 7 วันที่ผ่านมา

Consolidate ไฟล์ Excel หลายไฟล์จากโฟลเดอร์
let AllFiles = Folder.Files("C:\Sales\Monthly"), ExcelFiles = Table.SelectRows(AllFiles, each Text.EndsWith([Name], ".xlsx")), LoadedSheets = Table.AddColumn(Ex…
ใช้ Folder.Files หาไฟล์ Excel ทั้งหมด จากนั้น load ข้อมูลจากแต่ละไฟล์ แล้ว combine ข้อมูลเข้าด้วยกัน เป็นวิธีสำคัญสำหรับ consolidation
Power Query Formula:

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

Result:

Table ที่ combine ข้อมูลจากไฟล์ Excel ทั้งหมดในโฟลเดอร์

FAQs

Folder.Files ค้นหาโฟลเดอร์ย่อยด้วยไหม?

ใช่ ค้นหาแบบ recursive ไป ตามหาไฟล์ในโฟลเดอร์ย่อยทั้งหมดด้วย ถ้าต้องการแค่โฟลเดอร์ระดับแรก ต้อง filter จาก [Folder Path] column เอง

Column ไหนใช้เพื่อได้เนื้อหาไฟล์ (file content)?

[Content] column จะมีข้อมูล binary ของไฟล์ ใช้ร่วมกับ Csv.Document(), Excel.Workbook(), Json.Document() ฯลฯ เพื่อ parse เนื้อหา

ถ้าโฟลเดอร์ว่าง (ไม่มีไฟล์) จะได้อะไร?

ได้ table ว่างๆ มา (empty table with columns) ไม่มี error

Folder.Files ใช้ได้กับ network path ไหม?

ได้ ใช้ได้กับ UNC path เช่น “\\\\server\\share\\folder” อย่าลืม escape backslash ให้ถูกต้อง

ถ้า path ไม่มีอยู่จะเกิดอะไรขึ้น?

ได้ error กลับมา เช่น “Folder not found” ต้องใช้ try-catch หรือตรวจสอบ path ก่อน

Resources & Related

Additional Notes

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 ทีละไฟล์ 😎

Leave a Reply

Your email address will not be published. Required fields are marked *