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

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

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

## คำอธิบาย

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

## Syntax

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

## Arguments

| Name | Required | Type | Default | Description |
| --- | --- | --- | --- | --- |
| path | Yes | text |  | เส้นทางไปยังโฟลเดอร์ที่ต้องการอ่านไฟล์ ต้องเป็น absolute path เช่น "C:\Users\Data\Files" หรือ "\\server\share\folder" |
| options | No | nullable record | null | ตัวเลือกเพิ่มเติม (ปกติไม่ใช้) ใช้สำหรับการตั้งค่าเพิ่มเติมของ function |

## ตัวอย่าง

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

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

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

ดึงรายชื่อและข้อมูลของไฟล์ทั้งหมดในโฟลเดอร์ Reports รวมถึงโฟลเดอร์ย่อย จะได้ table ที่มีข้อมูลแต่ละไฟล์

### 2. ใช้ let...in เพื่อ filter ไฟล์ Excel เท่านั้น

```excel
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) เท่านั้น`

อ่านไฟล์ทั้งหมด แล้ว filter เอาแค่ไฟล์ที่ลงท้ายด้วย .xlsx หรือ .xls โดยตรวจสอบจาก Name column

### 3. อ่านไฟล์ CSV และ combine เข้าด้วยกัน

```excel
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 มาจากแต่ละไฟล์`

ใช้ Folder.Files หาไฟล์ CSV ทั้งหมด แล้วสร้าง column ใหม่ที่มีข้อมูล CSV ที่ parse มาแล้ว จากนั้น expand ออกมาได้ตามความต้องการ

### 4. รับรูป่วงจริง: ค้นหาไฟล์ตามวันที่สร้าง

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

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

ใช้ Folder.Files อ่านไฟล์ทั้งหมด แล้ว filter เอาแค่ไฟล์ที่สร้างภายใน 7 วันที่ผ่านมา โดยเช็ค Date Created column

### 5. Consolidate ไฟล์ Excel หลายไฟล์จากโฟลเดอร์

```excel
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 ทั้งหมดในโฟลเดอร์`

ใช้ Folder.Files หาไฟล์ Excel ทั้งหมด จากนั้น load ข้อมูลจากแต่ละไฟล์ แล้ว combine ข้อมูลเข้าด้วยกัน เป็นวิธีสำคัญสำหรับ consolidation

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

- ใช้ [Name] column เพื่อ filter ไฟล์ตามชื่อหรือ extension

- ใช้ [Date Modified] หรือ [Date Created] เพื่อ filter ไฟล์ตามเวลา

- ใช้ [Size] column เพื่อหาไฟล์ที่ใหญ่ที่สุด หรือ filter ไฟล์ขนาดเล็ก

- ใช้ [Folder Path] เพื่อ filter โฟลเดอร์ที่ต้องการเท่านั้น ถ้าไม่ต้องการให้ recursive

- ใช้ try() เมื่อ path อาจไม่มีอยู่เพื่อหลีกเลี่ยง error

- [Content] column ใช้ได้กับ Csv.Document(), Excel.Workbook(), Json.Document(), Text.FromBinary()

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

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

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

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

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

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

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

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

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

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

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

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

- [File.Contents – อ่านข้อมูลจากไฟล์](https://www.thepexcel.com/functions/power-query/accessing-data-functions/file-contents/)
- folder-combine-files
- [Csv.Document – แปลงข้อมูล CSV เป็น Table ใน Power Query](https://www.thepexcel.com/functions/power-query/accessing-data-functions/csv-document/)
- [Excel.Workbook – อ่านและแยกเนื้อหาไฟล์ Excel ทั้งหมด](https://www.thepexcel.com/functions/power-query/accessing-data-functions/excel-workbook/)
- text-from-binary

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

- [Microsoft Learn: Folder.Files](https://learn.microsoft.com/en-us/powerquery-m/folder-files) _(official)_
- [Microsoft Learn: File Functions Overview](https://learn.microsoft.com/en-us/powerquery-m/file-functions) _(official)_

---

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