Back to blog

How to Automatically Sort Files Into Folders Based on File Name

Sort files into folders based on file name, automatically, on Windows and Mac: keyword, prefix and regex rules, a free script, and folders built from the name.

September 19, 2026
File Arbor Team

To automatically sort files into folders based on file name, you need a rule that looks at the name — a keyword such as invoice, a prefix such as IMG_, or a pattern such as INV-2026 — and moves the file to the folder that matches. Windows and Mac can do a one-time version of this with built-in search. For files that keep arriving, a script on a timer or a rule-based file organizer does it without you.

This guide covers all three, from a two-minute search to a rule that fires the moment a file lands. It also covers the two things most guides skip: what happens when several rules could match the same file, and how to build the destination folder out of the file name itself, so Acme_Invoice_0917.pdf files itself under Acme without you creating that folder first.

Why the File Name Is the Best Sorting Key You Already Have

Sorting by file type tells you a file is a PDF. It does not tell you whether that PDF is an invoice, a contract, a boarding pass, or a recipe. Every one of those arrives with the same extension, and sending them all to a folder called PDFs moves the mess rather than ending it.

The name usually knows more. Banks export Statement_2026-08.pdf. Cameras write DSC_0412.JPG and drones write DJI_0098.MP4. Design tools export logo-final-v3.png. Clients send Acme_brief_v2.docx. Universities publish CS101_Week3_Slides.pdf. None of these names were chosen for you, yet each one carries a word or a prefix that says where the file belongs. A name rule reads that piece of text and acts on it.

Names are also inconsistent, which is the part that breaks naive scripts. The same supplier sends Invoice, invoice, and INV in the same month. Two things fix this: matching that ignores case, and a rule that accepts several spellings at once. Both are covered in Method 3.

When the name says nothing at all — scan0043.pdf, download (3).pdf, Untitled document.docx — a name rule cannot help, and you want a rule that reads the text inside the file instead. That is a separate guide: sorting files into folders based on content. For everything with a usable name, which is most files, the name is faster and needs no file to be opened.

Three Ways to Sort Files by Name

  1. Search and move by hand. Free, built into both operating systems, and the right tool for clearing a backlog once.
  2. A script on a timer. Free, handles new files, and fails quietly when a name does not match the way you expected.
  3. A name rule in a file organizer. Watches the folder, matches on contains, starts with, ends with or a regex, moves the file on arrival, and logs what it did.

They are ordered by effort. Start at the top if the folder is a one-off mess. Start at the bottom if the same kinds of files arrive every week.

Method 1: Search by Name, Then Move the Matches

On Windows, open the folder in File Explorer and type the keyword into the search box in the top right. invoice finds every file with that word anywhere in the name; name:invoice restricts the search to the name only, and adding ext:pdf narrows it to PDFs. Press Ctrl+A to select every result, cut with Ctrl+X, open the destination folder, and paste with Ctrl+V.

On Mac, open the folder in Finder and press Cmd+F. Set the first criterion to Name, choose "contains", and type the keyword. Click Save to keep it as a Smart Folder if you expect to repeat the search. Select all results with Cmd+A and drag them to the destination folder. Dragging between two folders on the same drive moves the files; hold Cmd while dragging to force a move onto another drive.

This method has one limit, and it is a big one. It handles the files that exist right now. The three invoices that arrive next Tuesday will sit in Downloads until you search again. A Smart Folder shows you the matches but never moves anything. For a folder that fills up continuously, keep reading.

Method 2: A Script That Moves Files Matching a Name Pattern

A script does the same search and move on a schedule. On Windows, save this as sort-by-name.ps1:

$src = "$HOME\Downloads"
$dst = "$HOME\Documents\Invoices"
New-Item -ItemType Directory -Force $dst | Out-Null

Get-ChildItem $src -File |
  Where-Object { $_.Name -match 'invoice.*\.pdf$' } |
  Move-Item -Destination $dst

Then open Task Scheduler, create a basic task that starts powershell.exe with the arguments -ExecutionPolicy Bypass -File C:\Scripts\sort-by-name.ps1, and set the trigger to repeat every 10 minutes. PowerShell's -match operator ignores case, so Invoice and INVOICE both match.

On Mac or Linux, the equivalent is one line:

find ~/Downloads -maxdepth 1 -type f -iname '*invoice*.pdf' -exec mv -n {} ~/Documents/Invoices/ \;

Run it every 10 minutes from cron or a launchd agent. Use -iname rather than -name, or a file called invoice.pdf will slip past a pattern that says Invoice. Recent macOS versions will also ask you to grant Terminal or cron access to Downloads the first time a scheduled job touches it.

Three things go wrong with scripts, and they go wrong silently:

  • Half-finished downloads. Browsers write to a temporary file first — .crdownload in Chrome, .part in Firefox — and rename it when the download completes. A pattern that says *invoice* matches the temporary file and moves it mid-download. Always include the final extension in the pattern, as both scripts above do. The same problem sinks Automator folder actions, which is covered in moving files from one folder to another automatically.
  • Name collisions. Move-Item stops with an error when a file with the same name already exists in the destination. mv -n skips it without saying a word. Either way the file stays in Downloads and nothing tells you.
  • No log. When a file goes missing, there is no record of whether the script moved it, where it put it, or why. You reconstruct it from folder timestamps.

Scripts are the right choice for one folder, one pattern, and an owner who is comfortable editing them when a supplier changes its naming. Beyond that, the maintenance cost climbs quickly.

Method 3: A Name Rule in a File Organizer

A rule-based file organizer replaces the script with a form: watch this folder, match names like this, move matches there. Hazel does this on Mac, File Juggler and the free DropIt do it on Windows. The examples below use File Arbor because it runs on both, and because the rule model is simple enough to describe in a sentence.

File Arbor has four name conditions: name contains, name starts with, name ends with, and name matches a regex. All four ignore case. A single condition accepts several values, and the condition is satisfied when any of them matches — so invoice, inv-, rechnung is one condition, not three rules. A rule can hold up to five conditions, and every condition in the rule must be true, which is how you say "the name contains invoice AND the extension is pdf". When a rule matches, the file moves to the destination folder and the move is written to a History view where it can be undone.

Here is a rule set that sorts a typical Downloads folder by name. Each rule is one sentence you could say out loud.

Rule 1 — Invoices and receipts, every spelling, PDFs only.

  • Watch: Downloads
  • Match: name contains invoice, inv-, receipt, rechnung AND extension is pdf
  • Move to: Documents/Finance/<Year>/Invoices

The extension condition keeps an email draft called invoice-questions.docx out of the finance folder. The <Year> token is read from the file's date, so next January's folder creates itself.

Rule 2 — Camera and phone files by prefix.

  • Watch: Downloads
  • Match: name starts with IMG_, DSC_, DJI_, PXL_
  • Move to: Pictures/Camera/<Date:yyyy-mm>

Every camera maker uses a fixed prefix, which makes "starts with" the right condition. A file called report-IMG_2024.pdf is not caught, because the prefix is not at the start.

Rule 3 — Deliverables by suffix.

  • Watch: Work/Exports
  • Match: name ends with -final, _final, -approved
  • Move to: Work/Deliverables

"Ends with" compares the name without its extension, so -final matches logo-final.png and brief-final.docx alike.

Rule 4 — Client files by the first word of the name.

  • Watch: Downloads
  • Match: name matches regex ^[a-z]+_.+_
  • Move to: Clients/<NamePart:1>/<NamePart:2>

This is the rule that builds the folder from the name, and it gets its own section below.

Rule 5 — Everything else that is a document.

  • Watch: Downloads
  • Match: extension is pdf, docx, xlsx
  • Move to: Documents/_Inbox

The catch-all goes last and has no name condition. It exists so that a document with an unrecognised name still leaves Downloads and lands somewhere you look at once a week.

On the free plan File Arbor matches on extension only, with one watched folder, two rules, up to 20 files a day, manual runs, which covers Rule 5 and one type-based rule. Name conditions, several conditions per rule, and Auto Mode are part of Pro, which is $24.99 once with lifetime updates — the split is on the pricing page.

Which Name Condition Should You Use?

ConditionUse it whenExample valueCatches
Name containsThe keyword can sit anywhere in the nameinvoiceAcme_invoice_0917.pdf, INVOICE-2231.pdf
Name starts withA tool or camera adds a fixed prefixScreenshotScreenshot 2026-09-12 at 10.14.03.png
Name ends withYou or your team add a status suffix-finallogo-final.png, deck-final.pptx
Name matches regexThe name follows a pattern, not a fixed word^INV-\d{4,}INV-2231.pdf, inv-20260912.pdf

Reach for "contains" first. It is the most forgiving and covers most real names. Use "starts with" when the keyword also appears in the middle of unrelated names — a rule that says "contains IMG" would catch reimagined-plan.pdf, while "starts with IMG_" would not. Use "ends with" for suffixes your own team controls. Use regex only when a word will not do.

Four regex patterns cover most of what people actually need:

  • ^INV-\d{4,} — an invoice number: the text INV-, then at least four digits, at the start of the name.
  • ^[A-Z]{2,4}\d{3} — a course code such as CS101 or MATH221 at the start of the name.
  • \d{4}-\d{2}-\d{2} — a date written as 2026-09-12 anywhere in the name.
  • (v|version)\d+ — a version marker such as v3 or version12.

Matching is case-insensitive, so [A-Z] also matches lowercase letters. File Arbor checks each pattern for constructs that could hang the app and refuses those, which is why a pattern that is legal elsewhere is occasionally rejected here. If regex is new to you, the beginner's guide to regex for file organization walks through the syntax with file names as the examples.

How Do You Build the Folder From the File Name?

The destination path in a File Arbor rule accepts tokens, and two of them read the file name.

<NamePart:n:delimiter> takes the n-th piece of the name after splitting it on a delimiter. The delimiter defaults to an underscore. For Acme_Invoice_0917.pdf, <NamePart:1> is Acme and <NamePart:2> is Invoice. A destination of Clients/<NamePart:1>/<NamePart:2> therefore resolves to Clients/Acme/Invoice/, and both folders are created if they do not exist. For hyphenated names such as acme-invoice-0917.pdf, write <NamePart:1:-> to split on the hyphen instead.

<Name:start:length> takes a fixed slice of the name. <Name:1:4> on 2026-09-12_report.pdf gives 2026, which is a quick way to build a year folder from names that begin with a date.

Two details keep this from misfiring:

  • The file name stays the same. Tokens only shape the folder path. Acme_Invoice_0917.pdf arrives in Clients/Acme/Invoice/ still called Acme_Invoice_0917.pdf. If you want a new name as well, give the rule one: since 1.3.0, File Arbor can move a file and rename it in one step, from parts such as the file date and the original name, as Hazel and File Juggler can.
  • Guard the rule with a pattern. If a name has no underscore, <NamePart:2> is empty and the empty piece is dropped from the path, so the file lands one level higher than you intended. Rule 4 above pairs the token with the regex ^[a-z]+_.+_, which only matches names that have at least two underscores. Files without that structure fall through to the catch-all instead.

The token approach rewards a naming habit: put the sorting key first. Acme_Invoice_0917.pdf sorts itself; Invoice 0917 (Acme).pdf does not. If you can influence the names — your own exports, a client at project kickoff, a colleague's scanner settings — the file naming conventions guide has templates that make every one of these rules simpler.

What If Two Rules Match the Same File?

The first matching rule in the list wins, and each file is handled once per pass. Acme_Invoice_0917.pdf matches Rule 1 (contains invoice, is a PDF), Rule 4 (has two underscores), and Rule 5 (is a PDF). It goes wherever the highest of those three rules sends it.

That makes order a design decision rather than an afterthought:

  1. Rules with two or more conditions go above rules with one. "Contains invoice AND contains acme" sits above "contains invoice".
  2. Rules with a narrow condition go above rules with a wide one. "Starts with IMG_" sits above "contains IMG".
  3. The catch-all goes last and has no name condition at all.

Before you switch on Auto Mode, run the rules once by hand and open the History view. Every file is listed with the rule that moved it. A file that went to the wrong folder is a rule in the wrong position, and dragging the rule up or down fixes it. Undo puts the file back while you reorder.

What Name Rules Cannot Do

A name rule reads the name and nothing else. It does not open the file, so a PDF called scan0043.pdf is invisible to it no matter what the PDF says inside; that is what a content rule is for, and the two kinds of rule can live in the same list. It does not detect duplicates by comparing contents — two identical photos with different names are two files to a name rule. And a name condition does not fix the name: a rule that only moves IMG_0412.JPG into a dated folder leaves it called IMG_0412.JPG when it gets there, unless you give the rule a new name for it — which File Arbor can do since 1.3.0, for example from the photo's capture date.

One smaller detail trips people up. "Ends with" compares the name without the extension, so a value of .pdf never matches anything. To match on the extension, use the extension condition, which is also the only condition on the free plan.

Which Method Should You Choose?

"The folder is a mess right now, and not much new arrives." Method 1. Search, select all, move. Ten minutes, no software.

"One folder, one pattern, and I am comfortable editing a script." Method 2. The PowerShell or find one-liner above, scheduled every ten minutes. Include the extension in the pattern and check the folder once a week for files the script skipped.

"Several kinds of files arrive every week from different sources." Method 3. Five name rules ordered specific to general, a catch-all at the bottom, Auto Mode on. Download File Arbor and build Rule 1 first; the rest follow the same shape.

"The names are useless." Neither. Use a content rule for PDFs and documents — since 1.3.0, File Arbor Pro also reads scans and can rename a file from a value inside it, such as an invoice date — and a naming convention for everything you or your team produce.

"I am on a Mac and already own Hazel." Hazel does everything in Method 3, renaming included. There is no reason to switch for this one job.

"I am on Windows with no budget." DropIt matches names with wildcard patterns and is free. Expect to spend an evening learning its interface, or use the script.

Keeping Name-Based Folders Organized Over Time

Name rules stay accurate for exactly as long as the names stay predictable, so most of the upkeep is on the naming side.

Put the sorting key first in any name you control, and ask the two or three people who send you the most files to do the same. One sentence at kickoff — "please start file names with the project code" — buys a year of automatic filing.

When a new spelling shows up, add it as a value to the existing rule rather than writing a new rule. Rule 1 started with invoice and grew inv-, receipt and rechnung over a few months; it is still one rule.

Keep the catch-all inbox small. Five minutes on Friday clearing Documents/_Inbox tells you which names the rules missed, and each miss is usually a new value for an existing rule.

Look at the History view once after the first week. Then forget about it until a file is not where you expected, which is rare once the rule order is right.

FAQ

Can I automatically move files based on the file name?

Yes. Windows and Mac can find files by name with built-in search, but they will not move new files for you. A script scheduled every few minutes can move anything whose name matches a pattern. A rule-based file organizer such as File Arbor watches the folder and moves each file on arrival when its name contains, starts with, ends with, or matches a pattern you set.

How do I move files containing a certain word in the name to a folder on Windows?

For a one-time cleanup, type the word into the File Explorer search box, select all results, and cut and paste them into the target folder. To keep doing it automatically, run a PowerShell script through Task Scheduler that moves every file whose name matches the word, or create a name contains rule in a file organizer so the move happens the moment the file lands.

Does sorting files by name rename them?

Only if the rule renames them. A name rule decides which folder the file goes to, and the file keeps its original name; if a file with the same name already exists in the destination, the new one is renamed with a suffix rather than overwriting anything. Since 1.3.0, File Arbor rules can rename too, as Hazel on Mac and File Juggler on Windows can.

Is file name matching case-sensitive?

No, not in File Arbor. Name contains, starts with, ends with, and regex conditions all ignore case, so a value of invoice matches Invoice, INVOICE, and invoice. PowerShell's match operator is also case-insensitive by default. On Mac and Linux, use the find command's iname option instead of name, or a lowercase file will slip past a capitalized pattern.

Can a rule create the destination folder from part of the file name?

Yes. File Arbor's destination path accepts tokens that read the file name. NamePart takes the n-th piece of the name split on a delimiter, so Acme_Invoice_0917.pdf can be routed to Clients/Acme/Invoice without creating those folders first. Name takes a fixed number of characters from a given position. Folders are created on demand, and the file name itself stays the same unless the rule also renames the file.

What happens when two rules match the same file?

The first matching rule in the list wins and the file is handled once per pass. Put specific rules above general ones: a rule that requires both the client name and the word invoice should sit above the rule that only requires invoice. Keep a catch-all rule at the very bottom so anything unmatched still lands in an inbox folder instead of staying put.

The Takeaway

Most files arrive with a name that already says where they belong. A one-time search clears the backlog. A scheduled script keeps one folder clean if you are willing to maintain it. A name rule does the same job for every folder, every spelling, and every file that arrives while you are not looking, and writes down what it did.

Start with the one keyword that shows up most in your Downloads folder. Download File Arbor, add a rule that says "name contains that word, move it there", run it once, and check the History view. If that works — and it will — the other four rules take a few minutes each. The free tier is enough to see the extension rules run; see pricing for what name matching and Auto Mode add.