---
title: Folder.Contents – แสดงรายการไฟล์และโฟลเดอร์ทั้งหมดใน folder
url: https://www.thepexcel.com/functions/power-query/accessing-data-functions/folder-contents/
type: function-explainer
program: Power Query
syntax: "Folder.Contents(path as text, optional options as nullable record) as table"
date: 2025-12-18
scores:
  popularity: 8
  difficulty: 3
  usefulness: 9
---

# Folder.Contents – แสดงรายการไฟล์และโฟลเดอร์ทั้งหมดใน folder

> ดึงรายการไฟล์และโฟลเดอร์ทั้งหมดจาก folder path พร้อม metadata และ content link

## คำอธิบาย

Folder.Contents เป็น function สำหรับดึงรายการไฟล์และโฟลเดอร์ทั้งหมดที่อยู่ใน folder path ที่กำหนดไว้ โดยจะคืนค่าเป็น table ที่มีข้อมูล metadata ครบถ้วน เช่น ชื่อไฟล์ วันที่สร้างและแก้ไข ขนาดไฟล์ และ binary content link สำหรับเข้าถึงเนื้อหาภายในไฟล์ เหมาะสำหรับงานที่ต้องการ import ข้อมูลจากหลายไฟล์พร้อมกันในโฟลเดอร์เดียว หรือการวิเคราะห์โครงสร้างไฟล์และคุณสมบัติต่างๆ ก่อนตัดสินใจนำเข้าข้อมูล

## Syntax

```excel
Folder.Contents(path as text, optional options as nullable record) as table
```

**Variant**

```excel
Folder.Contents(path)
```

รูปแบบพื้นฐาน ระบุเฉพาะ folder path ที่ต้องการดึงข้อมูล

**Variant**

```excel
Folder.Contents(path, options)
```

รูปแบบเต็มพร้อม optional options parameter (สำหรับ internal use)

## Arguments

| Name | Required | Type | Default | Description |
| --- | --- | --- | --- | --- |
| path | Yes | text |  | folder path ที่ต้องการดึงรายการไฟล์และโฟลเดอร์ ต้องเป็น absolute path เช่น "C:\\Data\\Files" (Windows) หรือ "/Users/Data/Files" (Mac/Linux) |
| options | No | nullable record | null | record parameter สำหรับ internal use ไม่แนะนำให้ใช้งานทั่วไป ปล่อยว่างได้ |

## เคสการใช้งาน

### รวมไฟล์ Excel หลายไฟล์จาก folder เดียวกัน

ใช้ Folder.Contents ดึงรายการไฟล์ทั้งหมด แล้ว filter เฉพาะไฟล์ .xlsx ก่อนรวมข้อมูล

_เหมาะกับ:_ data-consolidation

### ตรวจสอบ file attributes ก่อน import ข้อมูล

ใช้ Folder.Contents ตรวจสอบวันที่แก้ไข ขนาดไฟล์ หรือชื่อไฟล์ เพื่อกรองเฉพาะไฟล์ที่ต้องการนำเข้า

_เหมาะกับ:_ data-quality-check

### สร้าง file inventory หรือ file catalog

ใช้ Folder.Contents ดึงข้อมูล metadata ของไฟล์ทั้งหมดใน folder เพื่อสร้างรายงานการจัดการไฟล์

_เหมาะกับ:_ file-management

## ตัวอย่าง

### 1. ตัวอย่างที่ 1: ดึงรายการไฟล์และโฟลเดอร์ทั้งหมดจาก folder

```excel
let
    FolderPath = "C:\\Sales2024",
    AllContents = Folder.Contents(FolderPath)
in
    AllContents
```

**ผลลัพธ์:** `Table ที่มีทุกไฟล์และโฟลเดอร์ใน C:\Sales2024 พร้อม columns: Name, Extension, Date accessed, Date modified, Date created, Attributes, Folder Path, Content (binary link)`

Folder.Contents คืนค่า table ที่มีรายการทั้งหมดใน folder โดย column Name แสดงชื่อไฟล์/โฟลเดอร์ Extension แสดงนามสกุลไฟล์ (folder จะเป็นค่าว่าง) Date modified แสดงวันที่แก้ไขล่าสุด และ Content เป็น binary link สำหรับเข้าถึงข้อมูลภายในไฟล์
.
ใน Power Query Editor จะเห็นแต่ละ row แสดงรายละเอียดของไฟล์/โฟลเดอร์แยกกัน

### 2. ตัวอย่างที่ 2: Filter เฉพาะไฟล์ Excel จาก folder

```excel
let
    FolderPath = "C:\\Reports",
    AllContents = Folder.Contents(FolderPath),
    ExcelFilesOnly = Table.SelectRows(AllContents, each [Extension] = ".xlsx")
in
    ExcelFilesOnly
```

**ผลลัพธ์:** `Table ที่มีเฉพาะไฟล์ .xlsx ใน folder C:\Reports`

หลังจากใช้ Folder.Contents ดึงรายการทั้งหมดแล้ว เราใช้ Table.SelectRows กับ each keyword เพื่อ filter เฉพาะ row ที่มี Extension column = ".xlsx"
.
เทคนิคนี้มีประโยชน์เมื่อมีไฟล์หลายประเภทใน folder เดียวกัน และต้องการ import เฉพาะ Excel files เท่านั้น สามารถเปลี่ยน condition เป็น [Extension] = ".csv" หรือ Text.Contains([Name], "Sales") เพื่อ filter ตามเงื่อนไขอื่นได้ครับ

### 3. ตัวอย่างที่ 3: Filter ไฟล์ที่แก้ไขภายใน 7 วันล่าสุด

```excel
let
    FolderPath = "C:\\Data",
    AllContents = Folder.Contents(FolderPath),
    Today = DateTime.LocalNow(),
    SevenDaysAgo = Date.AddDays(Date.From(Today), -7),
    RecentFiles = Table.SelectRows(AllContents, each Date.From([Date modified]) >= SevenDaysAgo and [Extension] <> "")
in
    RecentFiles
```

**ผลลัพธ์:** `Table ของไฟล์ทั้งหมด (ไม่รวม folder) ที่แก้ไขภายใน 7 วันล่าสุด`

ตัวอย่างนี้แสดงการใช้ Folder.Contents ร่วมกับ Date functions เพื่อ filter ไฟล์ตามวันที่ โดย DateTime.LocalNow() ดึงวันเวลาปัจจุบัน Date.AddDays(..., -7) คำนวณย้อนหลัง 7 วัน จากนั้นใช้ each [Date modified] >= SevenDaysAgo กรองเฉพาะไฟล์ที่แก้ไขใหม่กว่า 7 วัน
.
และ [Extension]  "" ใช้ filter โฟลเดอร์ออก (เพราะโฟลเดอร์มี Extension เป็นค่าว่าง) เหมาะกับการ import เฉพาะไฟล์ใหม่ๆ ครับ

### 4. ตัวอย่างที่ 4: รวมข้อมูลจากหลาย Excel files ใน folder

```excel
let
    FolderPath = "C:\\MonthlySales",
    AllContents = Folder.Contents(FolderPath),
    ExcelFiles = Table.SelectRows(AllContents, each [Extension] = ".xlsx"),
    ImportFiles = Table.AddColumn(ExcelFiles, "Data", each Excel.Workbook([Content])),
    ExpandTables = Table.ExpandTableColumn(ImportFiles, "Data", {"Name", "Data"}, {"SheetName", "SheetData"}),
    FilterSheet = Table.SelectRows(ExpandTables, each [SheetName] = "Sales"),
    ExpandData = Table.ExpandTableColumn(FilterSheet, "SheetData", {"Product", "Amount"}, {"Product", "Amount"}),
    FinalData = Table.SelectColumns(ExpandData, {"Name", "Product", "Amount"})
in
    FinalData
```

**ผลลัพธ์:** `Table รวมข้อมูลจากทุกไฟล์ Excel ใน folder พร้อม column Name (ชื่อไฟล์), Product, Amount`

ตัวอย่างขั้นสูงนี้แสดงวิธีรวมข้อมูลจากหลายไฟล์ Excel โดย Folder.Contents ดึงรายการไฟล์ทั้งหมด Table.SelectRows filter เฉพาะ .xlsx Table.AddColumn สร้าง column ใหม่ชื่อ "Data" ที่เก็บผลลัพธ์จาก Excel.Workbook([Content]) ซึ่งอ่านข้อมูลจาก binary content ของแต่ละไฟล์
.
จากนั้นใช้ Table.ExpandTableColumn ขยาย nested tables ออกมา filter เฉพาะ sheet ชื่อ "Sales" และขยาย columns ที่ต้องการ (Product, Amount) สุดท้าย Table.SelectColumns เลือกเฉพาะ columns ที่จำเป็น
.
เทคนิคนี้ใช้บ่อยมากในการรวมไฟล์รายเดือนหรือรายสาขา ส่วนตัวผมใช้วิธีนี้ตลอดเลยครับ 😎

### 5. ตัวอย่างที่ 5: แยกไฟล์และโฟลเดอร์ด้วย Attributes column

```excel
let
    FolderPath = "C:\\Projects",
    AllContents = Folder.Contents(FolderPath),
    FilesOnly = Table.SelectRows(AllContents, each [Extension] <> ""),
    FoldersOnly = Table.SelectRows(AllContents, each [Extension] = "")
in
    FilesOnly
```

**ผลลัพธ์:** `Table ที่มีเฉพาะไฟล์ (ไม่รวมโฟลเดอร์)`

เทคนิคง่ายๆ ในการแยกไฟล์กับโฟลเดอร์ คือการตรวจสอบ Extension column โดยไฟล์จะมีค่า Extension (เช่น ".xlsx", ".csv", ".txt") แต่โฟลเดอร์จะมีค่าว่าง ("")
.
ดังนั้น [Extension]  "" จะ filter เฉพาะไฟล์ ส่วน [Extension] = "" จะ filter เฉพาะโฟลเดอร์ หรือสามารถใช้ Record.HasFields([Attributes], "Directory") ตรวจสอบจาก Attributes column ก็ได้ แต่การใช้ Extension นั้นง่ายกว่าและอ่านโค้ดเข้าใจง่ายกว่าครับ

## หมายเหตุเพิ่มเติม

💡 **Tip**: Folder.Contents เป็น function พื้นฐานแต่มีความทรงพลังมากในการจัดการกับไฟล์หลายไฟล์พร้อมกันใน Power Query
.
เหมาะอย่างยิ่งสำหรับสถานการณ์ที่มีไฟล์ข้อมูลจัดเก็บแยกตามช่วงเวลา เช่น รายเดือน รายไตรมาส หรือแยกตามหน่วยงาน เช่น สาขา แผนก และต้องการนำข้อมูลทั้งหมดมารวมกันเพื่อวิเคราะห์แบบรวม
.
⚠️ **ข้อสำคัญที่ต้องระวัง:** folder path จะต้องเป็น absolute path เสมอ และหากใช้งานบน Windows จะต้อง escape เครื่องหมาย backslash โดยใช้ \\\\ แทนการใช้ \\ เดี่ยว (ผมเคยลืม escape แล้วเจอ error บ่อยมากครับ 😅)
.
ส่วนตัวผมชอบนำ Folder.Contents มาใช้ร่วมกับ Table.SelectRows สำหรับกรองไฟล์ Table.AddColumn สำหรับเพิ่มข้อมูล และ Table.ExpandTableColumn สำหรับขยาย nested tables เพราะมันช่วยให้เราสร้าง data pipeline ที่มีประสิทธิภาพสูงสำหรับการรวมและประมวลผลข้อมูลจากหลายแหล่งได้อย่างเป็นระบบครับ 😎

## คำถามที่พบบ่อย

**Q: Folder.Contents แตกต่างจาก Folder.Files อย่างไร?**

Folder.Contents คืนค่าทั้งไฟล์และโฟลเดอร์ในระดับเดียว (ไม่ลงไปใน subfolder) เหมาะกับการควบคุม folder structure ได้แม่นยำ
.
ส่วน Folder.Files คืนค่าเฉพาะไฟล์เท่านั้นและค้นหาลงไปใน subfolder ทั้งหมดอัตโนมัติ เหมาะกับการ import ไฟล์ทั้งหมดใน folder tree โดยไม่สนใจ structure ครับ

**Q: จะแยกไฟล์กับโฟลเดอร์จาก Folder.Contents ได้อย่างไร?**

วิธีที่ง่ายที่สุดคือใช้ Extension column โดยไฟล์จะมีค่า Extension (เช่น ".xlsx") แต่โฟลเดอร์จะมีค่าว่าง ใช้ Table.SelectRows(..., each [Extension]  "") เพื่อ filter เฉพาะไฟล์ หรือ each [Extension] = "" เพื่อ filter เฉพาะโฟลเดอร์

**Q: Column Content ใน Folder.Contents ใช้ทำอะไร?**

Content column เป็น binary link ที่ชี้ไปยังเนื้อหาภายในไฟล์ สามารถนำไปใช้กับ functions อื่นๆ เช่น Excel.Workbook([Content]) เพื่ออ่านไฟล์ Excel, Csv.Document([Content]) สำหรับ CSV หรือ Json.Document([Content]) สำหรับ JSON
.
สะดวกมากครับ ไม่ต้องระบุ path ซ้ำอีก 😎

**Q: ทำไม Folder.Contents ถึงมี options parameter แต่ไม่ได้ใช้?**

options parameter เป็น nullable record ที่ Microsoft จองไว้สำหรับ internal use ในอนาคตหรือการใช้งานพิเศษเฉพาะ ผู้ใช้ทั่วไปไม่จำเป็นต้องส่งค่านี้ และควรปล่อยให้เป็น null (ค่า default) ไป Microsoft ไม่ได้ document การใช้งาน options นี้ใน official documentation

**Q: สามารถใช้ Folder.Contents กับ relative path ได้ไหม?**

ไม่แนะนำครับ เพราะ Power Query ต้องการ absolute path เสมอเพื่อความชัดเจนและป้องกันข้อผิดพลาด ควรใช้ full path เช่น "C:\\Data\\Files" (Windows) หรือ "/Users/Data/Files" (Mac/Linux)
.
หากต้องการความยืดหยุ่น สามารถใช้ parameter ใน Power Query เพื่อกำหนด path แบบ dynamic ได้ครับ

**Q: จะ filter ไฟล์ตามชื่อหรือวันที่ได้อย่างไร?**

ใช้ Table.SelectRows ร่วมกับ each keyword เช่น each Text.Contains([Name], "Sales") สำหรับ filter ตามชื่อ หรือ each Date.From([Date modified]) >= #date(2024,1,1) สำหรับ filter ตามวันที่ สามารถใช้ logical operators (and, or) รวมหลายเงื่อนไขได้

**Q: Folder.Contents รองรับ network path หรือ UNC path ไหม?**

รองรับครับ สามารถใช้ UNC path เช่น "\\\\Server\\Share\\Folder" หรือ mapped network drive เช่น "Z:\\Data" ได้ตามปกติ
.
แต่ต้องแน่ใจว่า Power Query service (Desktop หรือ Service) มีสิทธิ์เข้าถึง network location นั้นได้นะครับ ไม่งั้นจะ error ทันที 😅

## ฟังก์ชันที่เกี่ยวข้อง

- [Folder.Files – อ่านไฟล์ทั้งหมดในโฟลเดอร์](https://www.thepexcel.com/functions/power-query/accessing-data-functions/folder-files/)
- [File.Contents – อ่านข้อมูลจากไฟล์](https://www.thepexcel.com/functions/power-query/accessing-data-functions/file-contents/)
- [Excel.Workbook – อ่านและแยกเนื้อหาไฟล์ Excel ทั้งหมด](https://www.thepexcel.com/functions/power-query/accessing-data-functions/excel-workbook/)
- [Table.SelectRows – กรองแถวตามเงื่อนไขใน Power Query](https://www.thepexcel.com/functions/power-query/table-functions/table-selectrows/)
- [Table.AddColumn – เพิ่มคอลัมน์ใหม่ด้วย Calculated Values](https://www.thepexcel.com/functions/power-query/table-functions/table-addcolumn/)

## แหล่งข้อมูลเพิ่มเติม

- [Microsoft Learn: Folder.Contents](https://learn.microsoft.com/en-us/powerquery-m/folder-contents) _(documentation)_
- [Microsoft Learn: Folder.Files](https://learn.microsoft.com/en-us/powerquery-m/folder-files) _(documentation)_
- [Microsoft Learn: File.Contents](https://learn.microsoft.com/en-us/powerquery-m/file-contents) _(documentation)_
- [Microsoft Learn: Excel.Workbook](https://learn.microsoft.com/en-us/powerquery-m/excel-workbook) _(documentation)_

---

_Source: [https://www.thepexcel.com/functions/power-query/accessing-data-functions/folder-contents/](https://www.thepexcel.com/functions/power-query/accessing-data-functions/folder-contents/)_
