Sub topic 5
Choosing which files to load with PATTERN and FILES
By default, COPY INTO considers every file under the source path. This chapter covers the two clauses that narrow that set: PATTERN, which matches file names with a glob, and FILES, which lists exact file names. It assumes you can run a basic COPY INTO statement and have a Unity Catalog volume or external location to read from.
Why narrow the file set
A landing folder often holds more than the files you want to load. It might contain several days of exports, a README, a rejected-records subfolder, or files in a second format. Without a filter, COPY INTO tries to parse all of them with the format you declared. A stray .txt file read as CSV can fail the whole statement or load junk rows.
Filtering helps in three ways:
- It keeps unwanted files out of the table.
- It shortens the file listing for large folders.
- It lets you load a controlled slice, such as one month or one retry list.
The two clauses
| Clause | You provide | Best for |
|---|---|---|
| `PATTERN` | A glob string matched against file paths | Regular naming schemes such as dates or prefixes |
| `FILES` | A parenthesized list of exact file names | A known, short list, such as a retry after a failure |
You can use only one of them in a statement. Supplying both returns an error.
Both clauses go after FILEFORMAT (and after VALIDATE, if you use it) and before FORMAT_OPTIONS. Both are applied relative to the source path in the FROM clause. You never repeat the folder in the pattern or in the file names.
The diagram shows that selection comes first and the already-loaded check second. A file chosen by your filter can still be skipped if an earlier run loaded it. The idempotency chapter covers that tracking in detail.
Selecting files with PATTERN
PATTERN takes a single quoted glob string. A glob is a short wildcard expression, like the ones in a shell.
COPY INTO main.sales.orders
FROM '/Volumes/main/sales/landing/orders/'
FILEFORMAT = CSV
PATTERN = '2026-12-*.csv'
FORMAT_OPTIONS ('header' = 'true', 'delimiter' = ',');This loads files such as 2026-12-01.csv and 2026-12-04.csv. It ignores 2026-11-30.csv and 2026-12-01.csv.bak, because the pattern must match the whole name.
Glob syntax
| Token | Meaning | Example | Matches |
|---|---|---|---|
| `*` | Zero or more characters | `orders_*.csv` | `orders_1.csv`, `orders_final.csv` |
| `?` | Exactly one character | `day_?.csv` | `day_1.csv`, not `day_10.csv` |
| `[abc]` | One character from the set | `part-[ab].csv` | `part-a.csv`, `part-b.csv` |
| `[a-z]` | One character in the range | `file_[0-9].csv` | `file_7.csv` |
| `[^a]` | One character not in the set | `part-[^a].csv` | `part-b.csv`, not `part-a.csv` |
| `{ab,cd}` | Either alternative | `*.{csv,tsv}` | `a.csv`, `a.tsv` |
Patterns across subfolders
If files sit in subfolders under the source path, include the subfolder in the pattern. The pattern is still relative to the FROM path.
COPY INTO main.sales.orders
FROM '/Volumes/main/sales/landing/orders/'
FILEFORMAT = CSV
PATTERN = '{eu,us}/2026-12-*.csv'
FORMAT_OPTIONS ('header' = 'true');This reads eu/2026-12-01.csv and us/2026-12-02.csv, but nothing under apac/. A * is not guaranteed to cross a folder boundary, so write the folder levels explicitly instead of expecting *.csv to find files several levels down.
Selecting files with FILES
FILES takes a parenthesized list of quoted names. Use it when you already know exactly which files you want.
COPY INTO main.sales.orders
FROM '/Volumes/main/sales/landing/orders/'
FILEFORMAT = CSV
FILES = ('2026-12-01.csv', '2026-12-02.csv', 'late/2026-11-30.csv')
FORMAT_OPTIONS ('header' = 'true');Names are relative to the FROM path, so late/2026-11-30.csv means a late subfolder under the orders folder. The list can hold at most 1000 file names. If you need more than that, use PATTERN or split the load into several statements.
Unlike a pattern, a name in FILES is not a wildcard. If a listed file does not exist, expect an error rather than a quiet no-op. That makes FILES stricter and easier to trust when you want a guarantee that a specific file was considered.
Choosing between them
Use PATTERN when the naming scheme describes what you want and new files should be picked up automatically on the next run. This is the usual choice for a scheduled job. A date prefix, a source-system prefix, or an extension filter all fit.
Use FILES when an operator or an orchestration task supplies the list. A typical case is reloading the two files that failed yesterday after you fix the table. FILES is also a good fit when file names are unpredictable but a manifest tells you which ones are ready.
Neither clause changes how the file is parsed. Format settings still come from FILEFORMAT and FORMAT_OPTIONS. If your pattern admits two different formats, split it into two statements, one per format.
Interaction with already-loaded files
Selection and idempotency are separate steps. PATTERN and FILES decide the candidate set. COPY INTO then drops any candidate it has already loaded into this table. So a wide pattern like *.csv is safe to re-run. Only new files load.
This has a practical consequence for FILES. Naming a file that was loaded successfully before does nothing, and the statement still succeeds. If you really want to load it again, you must set the force option, which a later chapter explains. The tracking is per target table, so the same files can still load into a different table.
Other ways to filter
Some filters are not clauses of COPY INTO at all but format options passed in FORMAT_OPTIONS. These include modifiedAfter and modifiedBefore, which filter by file modification time, and pathGlobFilter, which filters by file name. They are handy for "only files that arrived since this timestamp" logic that a name pattern cannot express. Prefer PATTERN for name-based selection, because it is the documented clause and reads clearly. Use the options for time-based cases.
Common mistakes
- Repeating the folder in the pattern. Writing
PATTERN = 'orders/2026-12-*.csv'whenFROMalready ends inorders/searches a nonexistent subfolder and loads nothing. - Forgetting the extension.
PATTERN = '2026-12-*'also matches.csv.bakand.crcfiles, which then fail to parse. - Using regular expressions.
PATTERNtakes globs.2026-12-.*\.csvis not a valid way to say "any CSV in December." - Combining both clauses. Pick one. Move the extra condition into the glob or into a longer
FILESlist. - Expecting a listed file to reload. An already-loaded file is skipped even when you name it in
FILES.
Exercise
A folder holds sales_2026-12-01.csv, sales_2026-12-02.csv, sales_2026-12-02.csv.bak, and sales_2026-11-30.csv. Write the clause that loads only the December CSV files. Then say what happens if you run the same statement a second day later after one new December file arrives.
Show answer
Use PATTERN = 'sales_2026-12-*.csv'. The .bak file is excluded because the pattern must match the whole name, which ends in .csv. On the second run, COPY INTO selects all December CSV files again, skips the ones it already loaded, and loads only the new file.
Exercise
You want to retry exactly two files that failed last night, a.csv and b.csv, in the landing folder. Which clause fits better, PATTERN or FILES, and why can't you add the other clause too?
Show answer
FILES = ('a.csv', 'b.csv') fits better because you know the exact names, and a missing file raises an error instead of silently loading nothing. You cannot add PATTERN because COPY INTO allows only one of the two clauses in a statement.